Skip to content

Commit

Permalink
feat: Add aliases to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan committed Jun 6, 2022
1 parent d053a9d commit 4da0e2d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
75 changes: 65 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,59 @@ const (
CommandPrint = "print"
)

var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint}
type command struct {
Name string
Aliases []string
}

type commands []command

var Commands = commands{
{
Name: CommandStart,
Aliases: []string{},
},
{
Name: CommandStop,
Aliases: []string{"s", "st"},
},
{
Name: CommandNew,
Aliases: []string{"n"},
},
{
Name: CommandEdit,
Aliases: []string{"e"},
},
{
Name: CommandList,
Aliases: []string{"l"},
},
{
Name: CommandPrint,
Aliases: []string{"p"},
},
}

func (c *commands) Resolve(v string) (*command, error) {
for _, cmd := range *c {
if cmd.Name == v || Contains(cmd.Aliases, v) {
return &cmd, nil
}
}

return nil, ErrCommandNotFound
}

func (c *commands) FindByName(n string) *command {
for _, cmd := range *c {
if cmd.Name == n {
return &cmd
}
}

return nil
}

type Options struct {
Command string
Expand All @@ -31,6 +83,7 @@ type Options struct {
}

var ErrHelp = errors.New("help requested")
var ErrCommandNotFound = errors.New("command not found")

const (
WindowsUsage = "List of windows to start. If session exists, those windows will be attached to current session"
Expand Down Expand Up @@ -59,13 +112,12 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) {
return Options{}, ErrHelp
}

cmd := argv[0]
if !Contains(validCommands, cmd) {
helpRequested()
return Options{}, ErrHelp
cmd, cmdErr := Commands.Resolve(argv[0])
if errors.Is(cmdErr, ErrCommandNotFound) {
cmd = Commands.FindByName(CommandStart)
}

flags := NewFlagSet(cmd)
flags := NewFlagSet(cmd.Name)

config := flags.StringP("file", "f", "", FileUsage)
windows := flags.StringArrayP("windows", "w", []string{}, WindowsUsage)
Expand All @@ -75,7 +127,6 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) {
insideCurrentSession := flags.BoolP("inside-current-session", "i", false, InsideCurrentSessionUsage)

err := flags.Parse(argv)

if err == pflag.ErrHelp {
return Options{}, ErrHelp
}
Expand All @@ -85,8 +136,12 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) {
}

var project string
if *config == "" && len(argv) > 1 {
project = argv[1]
if *config == "" {
if errors.Is(cmdErr, ErrCommandNotFound) {
project = argv[0]
} else if len(argv) > 1 {
project = argv[1]
}
}

if strings.Contains(project, ":") {
Expand All @@ -111,7 +166,7 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) {
return Options{
Project: project,
Config: *config,
Command: cmd,
Command: cmd.Name,
Settings: settings,
Windows: *windows,
Attach: *attach,
Expand Down
22 changes: 19 additions & 3 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,25 @@ var usageTestTable = []struct {
},
{
[]string{"test"},
Options{},
ErrHelp,
1,
Options{
Command: "start",
Project: "test",
Windows: []string{},
Settings: map[string]string{},
},
nil,
0,
},
{
[]string{"test", "-w", "win1", "-w", "win2", "a=b", "x=y"},
Options{
Command: "start",
Project: "test",
Windows: []string{"win1", "win2"},
Settings: map[string]string{"a": "b", "x": "y"},
},
nil,
0,
},
{
[]string{},
Expand Down

0 comments on commit 4da0e2d

Please sign in to comment.