Skip to content

Commit

Permalink
bugfix/node-listing (#86)
Browse files Browse the repository at this point in the history
Merge pull request #86 from insolite-dev/bugfix/node-listing
  • Loading branch information
theiskaa authored Dec 5, 2022
2 parents 0c25069 + 4000bc8 commit 9dc2838
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func runCopyCommand(cmd *cobra.Command, args []string) {

loading.Start()
// Generate array of all node names.
_, nodeNames, err := service.GetAll("", models.NotyaIgnoreFiles)
_, nodeNames, err := service.GetAll("", "file", models.NotyaIgnoreFiles)
loading.Stop()
if err != nil {
pkg.Alert(pkg.ErrorL, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func runCutCommand(cmd *cobra.Command, args []string) {

loading.Start()
// Generate array of all node names.
_, nodeNames, err := service.GetAll("", models.NotyaIgnoreFiles)
_, nodeNames, err := service.GetAll("", "file", models.NotyaIgnoreFiles)
loading.Stop()
if err != nil {
pkg.Alert(pkg.ErrorL, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func runEditCommand(cmd *cobra.Command, args []string) {

// Generate all node names.
loading.Start()
_, nodeNames, err := service.GetAll("", models.NotyaIgnoreFiles)
_, nodeNames, err := service.GetAll("", "file", models.NotyaIgnoreFiles)
loading.Stop()
if err != nil {
pkg.Alert(pkg.ErrorL, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
loading.Start()

// Generate a list of nodes.
nodes, _, err := service.GetAll(additional, models.NotyaIgnoreFiles)
nodes, _, err := service.GetAll(additional, "", models.NotyaIgnoreFiles)

loading.Stop()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func runRemoveCommand(cmd *cobra.Command, args []string) {
loading.Start()

// Generate array of all node names.
_, nodeNames, err := service.GetAll("", models.NotyaIgnoreFiles)
_, nodeNames, err := service.GetAll("", "", models.NotyaIgnoreFiles)

loading.Stop()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func runRenameCommand(cmd *cobra.Command, args []string) {
loading.Start()

// Generate array of all node names.
_, nodeNames, err := service.GetAll("", models.NotyaIgnoreFiles)
_, nodeNames, err := service.GetAll("", "", models.NotyaIgnoreFiles)

loading.Stop()

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func runViewCommand(cmd *cobra.Command, args []string) {
}

// Generate array of all note names.
nodes, noteNames, err := service.GetAll("", models.NotyaIgnoreFiles)
nodes, noteNames, err := service.GetAll("", "file", models.NotyaIgnoreFiles)
loading.Stop()
if err != nil {
pkg.Alert(pkg.ErrorL, err.Error())
Expand Down
11 changes: 6 additions & 5 deletions lib/services/firebase_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (s *FirebaseService) Rename(editNode models.EditNode) error {

// ClearNodes removes all nodes from collection.
func (s *FirebaseService) ClearNodes() ([]models.Node, []error) {
nodes, _, err := s.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := s.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil && err.Error() != assets.EmptyWorkingDirectory.Error() {
return nil, []error{err}
}
Expand All @@ -349,7 +349,8 @@ func (s *FirebaseService) ClearNodes() ([]models.Node, []error) {
}

// GetAll returns all elements from notya collection.
func (s *FirebaseService) GetAll(additional string, ignore []string) ([]models.Node, []string, error) {
// TODO: implement the [typ] value.
func (s *FirebaseService) GetAll(additional, typ string, ignore []string) ([]models.Node, []string, error) {
var nodes []models.Node
var titles []string

Expand Down Expand Up @@ -481,7 +482,7 @@ func (s *FirebaseService) Mkdir(dir models.Folder) (*models.Folder, error) {
// MoveNote moves all notes from "CURRENT" firebase collection
// to new collection(given by settings parameter).
func (s *FirebaseService) MoveNotes(settings models.Settings) error {
nodes, _, err := s.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := s.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil {
return err
}
Expand All @@ -507,7 +508,7 @@ func (s *FirebaseService) MoveNotes(settings models.Settings) error {
// Fetch creates a clone of nodes(that doesn't exists on
// [s](firebase-service)) from given [remote] service.
func (s *FirebaseService) Fetch(remote ServiceRepo) ([]models.Node, []error) {
nodes, _, err := remote.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := remote.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil {
return nil, []error{err}
}
Expand Down Expand Up @@ -557,7 +558,7 @@ func (s *FirebaseService) Fetch(remote ServiceRepo) ([]models.Node, []error) {

// Push uploads nodes(that doens't exists on given remote) from [s](current) to given [remote].
func (s *FirebaseService) Push(remote ServiceRepo) ([]models.Node, []error) {
nodes, _, err := s.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := s.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil {
return nil, []error{err}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/services/local_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (l *LocalService) Remove(node models.Node) error {

// Check for directory, to remove sub nodes of it.
if pkg.IsDir(nodePath) {
subNodes, _, err := l.GetAll(node.StructAsFolder().Title, []string{})
subNodes, _, err := l.GetAll(node.StructAsFolder().Title, "", []string{})
if err != nil && err != assets.EmptyWorkingDirectory {
return err
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (l *LocalService) Rename(editNode models.EditNode) error {

// ClearNodes removes all nodes from local (including folders).
func (l *LocalService) ClearNodes() ([]models.Node, []error) {
nodes, _, err := l.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := l.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil && err.Error() != assets.EmptyWorkingDirectory.Error() {
return nil, []error{err}
}
Expand Down Expand Up @@ -403,11 +403,11 @@ func (l *LocalService) Mkdir(dir models.Folder) (*models.Folder, error) {
}

// GetAll gets all node [names], and returns it as array list.
func (l *LocalService) GetAll(additional string, ignore []string) ([]models.Node, []string, error) {
func (l *LocalService) GetAll(additional, typ string, ignore []string) ([]models.Node, []string, error) {
path, _ := l.GeneratePath(l.Config.NotesPath, models.Node{Title: additional})

// Generate array of all file names that are located in [path].
files, pretty, err := pkg.ListDir(path, "", "", ignore, true)
files, pretty, err := pkg.ListDir(path, "", typ, "", ignore, true)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -441,7 +441,7 @@ func (l *LocalService) GetAll(additional string, ignore []string) ([]models.Node

// MoveNote moves all notes from "CURRENT" path to new path(given by settings parameter).
func (l *LocalService) MoveNotes(settings models.Settings) error {
nodes, _, err := l.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := l.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil {
return err
}
Expand All @@ -464,7 +464,7 @@ func (l *LocalService) MoveNotes(settings models.Settings) error {

// Fetch creates a clone of nodes(that doesn't exists on [l](local-service)) from given [remote] service.
func (l *LocalService) Fetch(remote ServiceRepo) ([]models.Node, []error) {
nodes, _, err := remote.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := remote.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil {
return nil, []error{err}
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func (l *LocalService) Fetch(remote ServiceRepo) ([]models.Node, []error) {

// Push uploads nodes(that doesn't exists on given remote) from [l](current) to given [remote].
func (l *LocalService) Push(remote ServiceRepo) ([]models.Node, []error) {
nodes, _, err := l.GetAll("", models.NotyaIgnoreFiles)
nodes, _, err := l.GetAll("", "", models.NotyaIgnoreFiles)
if err != nil {
return nil, []error{err}
}
Expand Down
9 changes: 7 additions & 2 deletions lib/services/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ type ServiceRepo interface {
Rename(editNode models.EditNode) error
ClearNodes() ([]models.Node, []error)

// Note(file) related functions.
GetAll(additional string, ignore []string) ([]models.Node, []string, error)
// GetAll gets the all notes from current service.
//
// [additional] provides a way of entering to sub-folders of main folder.
// [ignore] provides a way of ignoring files. Default ignorable files: [models.NotyaIgnoreFiles].
// [typ] provides a way to get only specific type of file-nodes.
GetAll(additional, typ string, ignore []string) ([]models.Node, []string, error)

Create(note models.Note) (*models.Note, error)
View(note models.Note) (*models.Note, error)
Edit(note models.Note) (*models.Note, error)
Expand Down
22 changes: 18 additions & 4 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ReadBody(path string) (*string, error) {
}

// ListDir, reads all files from given-path directory.
func ListDir(path, prevPath, space string, ignore []string, tree bool) ([]string, [][]string, error) {
func ListDir(path, prevPath, typ, space string, ignore []string, tree bool) ([]string, [][]string, error) {
// Read directory's files.
list, err := os.ReadDir(path)
if err != nil {
Expand Down Expand Up @@ -146,7 +146,7 @@ func ListDir(path, prevPath, space string, ignore []string, tree bool) ([]string
if tree {
n := models.Node{Title: pathFI.Name(), Path: path}
sn, sp, err := ListDir(
n.StructAsFolder().Path+d.Name(), n.StructAsFolder().Title,
n.StructAsFolder().Path+d.Name(), n.StructAsFolder().Title, typ,
// Push to right by two empty characters each sub node.
space+" ", ignore, tree,
)
Expand All @@ -160,8 +160,22 @@ func ListDir(path, prevPath, space string, ignore []string, tree bool) ([]string
prettyName = space + models.NotePretty
}

res = append(res, name)
pretty = append(pretty, []string{prettyName, d.Name()})
// typeCondition is a boolean value generated by [typ] and [d] type matching.
typeCondition := func(isDir bool) bool {
switch typ {
case "file":
return !isDir
case "folder":
return isDir
}

return true
}(d.IsDir())

if typeCondition {
res = append(res, name)
pretty = append(pretty, []string{prettyName, d.Name()})
}

if len(subnames) > 0 && len(subpretty) > 0 {
res = append(res, subnames...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestListDir(t *testing.T) {
td.creatingFunc(td.folderName)

t.Run(td.testName, func(t *testing.T) {
got, _, err := pkg.ListDir(td.folderName, "", "", []string{"expectable.txt"}, true)
got, _, err := pkg.ListDir(td.folderName, "", "", "", []string{"expectable.txt"}, true)
if err != td.e.err {
t.Errorf("ListDir's error sum was different, Got: %v | Want: %v", err, td.e.err)
}
Expand Down

0 comments on commit 9dc2838

Please sign in to comment.