Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lsa command #96

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Commands struct {
Cp *CopyCommand
Grep *GrepCommand
Ls *ListCommand
Lsa *ListAllCommand
Mv *MoveCommand
Replace *ReplaceCommand
Rm *RemoveCommand
Expand All @@ -53,6 +54,7 @@ func NewCommands(client *client.Client) *Commands {
Cp: NewCopyCommand(client),
Grep: NewGrepCommand(client),
Ls: NewListCommand(client),
Lsa: NewListAllCommand(client),
Mv: NewMoveCommand(client),
Replace: NewReplaceCommand(client),
Rm: NewRemoveCommand(client),
Expand Down
82 changes: 82 additions & 0 deletions cli/lsall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cli

import (
"fmt"
"strings"

"github.com/fishi0x01/vsh/client"
"github.com/fishi0x01/vsh/log"
)

// ListCommand container for 'ls' parameters
type ListAllCommand struct {
name string
args *ListAllCommandArgs

client *client.Client
}

// ListCommandArgs provides a struct for go-arg parsing
type ListAllCommandArgs struct {
Path string `arg:"positional" help:"path to list all child paths of, defaults to current path"`
}

// Description provides detail on what the command does
func (ListAllCommandArgs) Description() string {
return "lists all child paths from the a given parent path"
}

// NewListCommand creates a new ListCommand parameter container
func NewListAllCommand(c *client.Client) *ListAllCommand {
return &ListAllCommand{
name: "lsa",
client: c,
args: &ListAllCommandArgs{},
}
}

// GetName returns the ListCommand's name identifier
func (cmd *ListAllCommand) GetName() string {
return cmd.name
}

// GetArgs provides the struct holding arguments for the command
func (cmd *ListAllCommand) GetArgs() interface{} {
return cmd.args
}

// IsSane returns true if command is sane
func (cmd *ListAllCommand) IsSane() bool {
return cmd.args.Path != ""
}

// PrintUsage print command usage
func (cmd *ListAllCommand) PrintUsage() {
fmt.Println(Help(cmd))
}

// Parse given arguments and return status
func (cmd *ListAllCommand) Parse(args []string) error {
_, err := parseCommandArgs(args, cmd)
if err != nil {
return err
}
if cmd.args.Path == "" {
cmd.args.Path = cmd.client.Pwd
}

return nil
}

// Run executes 'ls' with given ListAllCommand's parameters
func (cmd *ListAllCommand) Run() int {
newPwd := cmdPath(cmd.client.Pwd, cmd.args.Path)
result, err := cmd.client.ListAll(newPwd)

if err != nil {
log.UserError("Not a valid path for operation: %s", newPwd)
return 1
}
log.UserInfo(strings.Join(result, "\n"))
return 0
}
10 changes: 10 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func (client *Client) List(absolutePath string) (result []string, err error) {
return result, err
}

// List elements at the given absolutePath, using the given client
func (client *Client) ListAll(absolutePath string) (result []string, err error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets move that to Run function of lsall.go.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work without changing the package structure.

The used functions as isTopLevelPath, topLevelTraverse and lowLevelTraverse are not available in the
lsall.go cli package. So without a package refactoring not really possible.
And the other command functions like List, Delete, ... are also defined here and not in there command Run function.

if client.isTopLevelPath(absolutePath) {
result = client.topLevelTraverse()
} else {
result = client.lowLevelTraverse(normalizedVaultPath(absolutePath), false)
}
return result, err
}

// GetType returns the file type the given absolutePath points to. Possible return values are BACKEND, NODE, LEAF or NONE
func (client *Client) GetType(absolutePath string) (kind PathKind) {
if client.isTopLevelPath(absolutePath) {
Expand Down
2 changes: 2 additions & 0 deletions client/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import (
"errors"
_"fmt"

"github.com/fishi0x01/vsh/log"
)

Expand Down