diff --git a/external/commerce-manager/commercemanager.go b/cmd/commercemanager.go similarity index 61% rename from external/commerce-manager/commercemanager.go rename to cmd/commercemanager.go index 21e22448..154148e8 100644 --- a/external/commerce-manager/commercemanager.go +++ b/cmd/commercemanager.go @@ -1,22 +1,21 @@ -package commercemanager +package cmd import ( "fmt" - "github.com/elasticpath/epcc-cli/config" - "github.com/elasticpath/epcc-cli/external/command" + "github.com/spf13/cobra" "net/url" "os/exec" "runtime" ) -var Command = command.Command{ - Keyword: "commerce-manager", - Description: "Open commerce manager", - Execute: func(cmds map[string]command.Command, cmd string, args []string, envs config.Env) int { - u, err := url.Parse(envs.EPCC_API_BASE_URL) +var cmCommand = &cobra.Command{ + Use: "commerce-manager", + Short: "Open commerce manager", + RunE: func(cmd *cobra.Command, args []string) error { + u, err := url.Parse(Envs.EPCC_API_BASE_URL) if err != nil { fmt.Println(err) - return 1 + return err } var cmUrl string switch u.Host { @@ -27,8 +26,8 @@ var Command = command.Command{ } if cmUrl == "" { - fmt.Printf("Don't know where Commerce Manager is for $EPCC_API_BASE_URL=%s \n", envs.EPCC_API_BASE_URL) - return 1 + fmt.Printf("Don't know where Commerce Manager is for $EPCC_API_BASE_URL=%s \n", u) + return err } switch runtime.GOOS { @@ -41,11 +40,14 @@ var Command = command.Command{ default: err = fmt.Errorf("unsupported platform") } + if err != nil { fmt.Println(err) - return 1 + return err } - return 0 + fmt.Printf("Opening browser to %s", cmUrl) + + return nil }, } diff --git a/cmd/docs.go b/cmd/docs.go new file mode 100644 index 00000000..41943a6b --- /dev/null +++ b/cmd/docs.go @@ -0,0 +1,14 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" +) + +var docsCommand = &cobra.Command{ + Use: "docs ", + Short: "Opens up API documentation for the resource", + RunE: func(cmd *cobra.Command, args []string) error { + return fmt.Errorf("This function is not implemented") + }, +} diff --git a/cmd/main.go b/cmd/main.go deleted file mode 100644 index a2b8c822..00000000 --- a/cmd/main.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "fmt" - "github.com/caarlos0/env/v6" - "github.com/elasticpath/epcc-cli/config" - "github.com/elasticpath/epcc-cli/external/command" - commercemanager "github.com/elasticpath/epcc-cli/external/commerce-manager" - "github.com/elasticpath/epcc-cli/external/help" - _ "github.com/elasticpath/epcc-cli/external/resources" - "os" -) - -var commands = []command.Command{ - help.Command, - commercemanager.Command, -} - -func main() { - envs := config.Env{} - if err := env.Parse(&envs); err != nil { - fmt.Printf("%+v\n", err) - } - - 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, argsWithoutProg[0], argsWithoutCmd, envs)) - } - } - - fmt.Printf("Unknown command %s specified", commandToRun) - os.Exit(0) - -} diff --git a/cmd/main_test.go b/cmd/main_test.go deleted file mode 100644 index e02e7e0b..00000000 --- a/cmd/main_test.go +++ /dev/null @@ -1,15 +0,0 @@ -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/cmd/root.go b/cmd/root.go new file mode 100644 index 00000000..15a19f2a --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,83 @@ +package cmd + +import ( + "fmt" + "github.com/caarlos0/env/v6" + "github.com/elasticpath/epcc-cli/config" + homedir "github.com/mitchellh/go-homedir" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "os" +) + +var Envs = &config.Env{} + +func init() { + cobra.OnInitialize(initConfig) + + if err := env.Parse(Envs); err != nil { + panic("Could not parse environment variables") + } + + rootCmd.AddCommand( + cmCommand, + docsCommand, + testJson, + ) + +} + +var rootCmd = &cobra.Command{ + Use: os.Args[0], + Short: "A command line interface for interacting with the Elastic Path Commerce Cloud API", + Long: `The EPCC CLI tool provides a powerful command line interface for interacting with the Elastic Path Commerce Cloud API. + +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. +`, +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func initConfig() { + // Don't forget to read config either from cfgFile or from home directory! + cfgFile := "" + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := homedir.Dir() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Search config in home directory with name ".cobra" (without extension). + viper.AddConfigPath(home) + viper.SetConfigName(".epcc") + } + + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + // Config file not found; ignore error if desired + } else { + fmt.Println("Can't read config:", err) + os.Exit(1) + } + + } +} diff --git a/cmd/test-json.go b/cmd/test-json.go new file mode 100644 index 00000000..fad69208 --- /dev/null +++ b/cmd/test-json.go @@ -0,0 +1,14 @@ +package cmd + +import ( + "fmt" + "github.com/spf13/cobra" +) + +var testJson = &cobra.Command{ + Use: "test-json [KEY_1] [VAL_1] [KEY_2] [VAL_2] ...", + Short: "Prints the resulting json for what a command will look like", + RunE: func(cmd *cobra.Command, args []string) error { + return fmt.Errorf("This function is not implemented") + }, +} diff --git a/external/command/command.go b/external/command/command.go deleted file mode 100644 index f1f59651..00000000 --- a/external/command/command.go +++ /dev/null @@ -1,12 +0,0 @@ -package command - -import "github.com/elasticpath/epcc-cli/config" - -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, cmd string, args []string, envs config.Env) int -} diff --git a/external/help/help.go b/external/help/help.go deleted file mode 100644 index 090d357d..00000000 --- a/external/help/help.go +++ /dev/null @@ -1,45 +0,0 @@ -package help - -import ( - "fmt" - "github.com/elasticpath/epcc-cli/config" - "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, cmd string, args []string, envs config.Env) 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/go.mod b/go.mod index 1bb77ab9..760eb4ea 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,27 @@ module github.com/elasticpath/epcc-cli go 1.18 require ( - github.com/caarlos0/env/v6 v6.9.1 // indirect + github.com/caarlos0/env/v6 v6.9.1 + github.com/mitchellh/go-homedir v1.1.0 + github.com/spf13/cobra v1.4.0 + github.com/spf13/viper v1.10.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) + +require ( + github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/magiconair/properties v1.8.5 // indirect + github.com/mitchellh/mapstructure v1.4.3 // indirect + github.com/pelletier/go-toml v1.9.4 // indirect + github.com/spf13/afero v1.6.0 // indirect + github.com/spf13/cast v1.4.1 // indirect + github.com/spf13/jwalterweatherman v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.2.0 // indirect + golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect + golang.org/x/text v0.3.7 // indirect + gopkg.in/ini.v1 v1.66.2 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum index e8503fdf..9a2d8c4b 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,66 @@ github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k= github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= +github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= +github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= +github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= +github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= +github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= +github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= +github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= +github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk= +github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= +github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486 h1:5hpz5aRr+W1erYCL5JRhSUBJRph7l9XkNveoExlrKYk= +golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= +gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 00000000..7fcedb76 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/elasticpath/epcc-cli/cmd" + +func main() { + cmd.Execute() +}