diff --git a/cmd/main.go b/cmd/main.go index cc86d351..35bef58b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,10 +2,39 @@ package main import ( "fmt" + "github.com/elasticpath/epcc-cli/external/command" + "github.com/elasticpath/epcc-cli/external/help" _ "github.com/elasticpath/epcc-cli/external/resources" + "os" ) +var commands = []command.Command{ + help.Command, +} + func main() { - fmt.Printf("Hello %s", "World!") + argsWithoutProg := os.Args[1:] + + if (len(argsWithoutProg)) == 0 { + fmt.Printf("No command specified") + os.Exit(1) + } + + commandToRun := argsWithoutProg[0] + + cmds := make(map[string]command.Command) + + for _, cmd := range commands { + cmds[cmd.Keyword] = cmd + } + + for _, cmd := range commands { + if cmd.Keyword == commandToRun { + argsWithoutCmd := argsWithoutProg[1:] + os.Exit(cmd.Execute(cmds, argsWithoutCmd)) + } + } + fmt.Printf("Unknown command %s specified", commandToRun) + os.Exit(0) } diff --git a/cmd/main_test.go b/cmd/main_test.go new file mode 100644 index 00000000..e02e7e0b --- /dev/null +++ b/cmd/main_test.go @@ -0,0 +1,15 @@ +package main + +import "testing" + +func TestNoDuplicateCommands(t *testing.T) { + set := make(map[string]bool) + + for _, command := range commands { + set[command.Keyword] = true + } + + if len(set) != len(commands) { + t.Fatalf("Duplicate commands have been registered since the length of the keyword set is not the same as the array") + } +} diff --git a/external/command/command.go b/external/command/command.go new file mode 100644 index 00000000..ea94f519 --- /dev/null +++ b/external/command/command.go @@ -0,0 +1,10 @@ +package command + +type Command struct { + // The keyword the command should use + Keyword string + // A one-line description of the command + Description string + // The function that will be executed + Execute func(cmds map[string]Command, args []string) int +} diff --git a/external/help/help.go b/external/help/help.go new file mode 100644 index 00000000..68a2e49b --- /dev/null +++ b/external/help/help.go @@ -0,0 +1,44 @@ +package help + +import ( + "fmt" + "github.com/elasticpath/epcc-cli/external/command" + "sort" +) + +var Command = command.Command{ + Keyword: "help", + Description: "Displays this screen", + Execute: func(cmds map[string]command.Command, args []string) int { + + keys := make([]string, 0, len(cmds)) + for k := range cmds { + keys = append(keys, k) + } + sort.Strings(keys) + + fmt.Printf(` +Setup + +The EPCC CLI tool uses environment variables for configuration and in particular a tool like https://direnv.net/ which +auto populates your shell with environment variables when you switch directories. This allows you to store a context in a folder, +and come back to it at any time. + +Environment Variables + +- EPCC_API_BASE_URL - The API endpoint that we will hit +- EPCC_CLIENT_ID - The client id (available in Commerce Manager) +- EPCC_CLIENT_SECRET - The client secret (available in Commerce Manager) +- EPCC_BETA_API_FEATURES - Beta features in the API we want to enable. + +The following commands are supported: + +`) + + for _, key := range keys { + fmt.Printf(" %s - %s", cmds[key].Keyword, cmds[key].Description) + } + + return 0 + }, +} diff --git a/external/resources/resources.go b/external/resources/resources.go index 54f1648b..bb5cb63e 100644 --- a/external/resources/resources.go +++ b/external/resources/resources.go @@ -2,7 +2,6 @@ package resources import ( _ "embed" - "fmt" "gopkg.in/yaml.v3" ) @@ -18,9 +17,6 @@ func init() { panic("Couldn't load the resource meta data") } - // You can delete this line later. - fmt.Printf("Loaded data for %d resources: ", len(Resources)) - for key, val := range Resources { // Fix the key val.Type = key @@ -29,13 +25,7 @@ func init() { // Fix the key attributeVal.Key = attributeName } - - // Delete me later - fmt.Printf("%s,", key) } - - // Delete me later - fmt.Print("\n") } type Resource struct {