diff --git a/Makefile b/Makefile index e0decbe8..67ef5d09 100644 --- a/Makefile +++ b/Makefile @@ -133,7 +133,7 @@ ecr-connect: ## Login to ecr, requires aws cli installed start-local-be: ## Starts local BE instance. Note! Only for cycloid developers @if [ ! -d ${LOCAL_BE_GIT_PATH} ]; then echo "Unable to find BE at LOCAL_BE_GIT_PATH"; exit 1; fi; @if [ -z "$$API_LICENCE_KEY" ]; then echo "API_LICENCE_KEY is not set"; exit 1; fi; \ - @echo "Starting Local BE..." + echo "Starting Local BE..." @echo "Generating fake data to be used in the tests..." @cd $(LOCAL_BE_GIT_PATH) && sed -i '/cost-explorer-es/d' config.yml @cd $(LOCAL_BE_GIT_PATH) && YD_API_TAG=${YD_API_TAG} API_LICENCE_KEY=${API_LICENCE_KEY} \ @@ -159,4 +159,3 @@ delete-local-be: ## Creates local BE instance and starts e2e tests. Note! Only f new-changelog-entry: ## Create a new entry for unreleased element @echo ${PATH} docker run -it -v $(CURDIR):/cycloid-cli -w /cycloid-cli cycloid/cycloid-toolkit changie new - diff --git a/changelog/unreleased/CLI-BREAKING-20240702-154137.yaml b/changelog/unreleased/CLI-BREAKING-20240702-154137.yaml new file mode 100644 index 00000000..031d2a20 --- /dev/null +++ b/changelog/unreleased/CLI-BREAKING-20240702-154137.yaml @@ -0,0 +1,9 @@ +component: CLI +kind: BREAKING +body: Env var TOKEN is replaced by CY_API_KEY for giving token via env var. +time: 2024-07-02T15:41:37.096071763Z +custom: + DETAILS: The old `TOKEN` env var still works but emits a warning. It will be removed + in a futur release. + PR: "274" + TYPE: CLI diff --git a/changelog/unreleased/CLI-CHANGED-20240702-153738.yaml b/changelog/unreleased/CLI-CHANGED-20240702-153738.yaml new file mode 100644 index 00000000..b4f6a3ee --- /dev/null +++ b/changelog/unreleased/CLI-CHANGED-20240702-153738.yaml @@ -0,0 +1,9 @@ +component: CLI +kind: CHANGED +body: Allow to use CY_ORG env var to define the current org instead of --org flag +time: 2024-07-02T15:37:38.698172633Z +custom: + DETAILS: You can still use the --org flag, the CLI flag will have precedence over + the env var. + PR: "274" + TYPE: CLI diff --git a/changelog/unreleased/CLI-FIXED-20240702-154027.yaml b/changelog/unreleased/CLI-FIXED-20240702-154027.yaml new file mode 100644 index 00000000..a223db30 --- /dev/null +++ b/changelog/unreleased/CLI-FIXED-20240702-154027.yaml @@ -0,0 +1,9 @@ +component: CLI +kind: FIXED +body: --org flag is now global +time: 2024-07-02T15:40:27.815286561Z +custom: + DETAILS: This fixes inconsistency between commands, you can also set the org via + env vars now. + PR: "274" + TYPE: CLI diff --git a/changelog/unreleased/CLI-SECURITY-20240702-153824.yaml b/changelog/unreleased/CLI-SECURITY-20240702-153824.yaml new file mode 100644 index 00000000..30ff4dae --- /dev/null +++ b/changelog/unreleased/CLI-SECURITY-20240702-153824.yaml @@ -0,0 +1,8 @@ +component: CLI +kind: SECURITY +body: Make the configuration write as 0600 permissions in user home config +time: 2024-07-02T15:38:24.286288523Z +custom: + DETAILS: "" + PR: "274" + TYPE: CLI diff --git a/changelog/unreleased/CLI-SECURITY-20240702-153935.yaml b/changelog/unreleased/CLI-SECURITY-20240702-153935.yaml new file mode 100644 index 00000000..db132caf --- /dev/null +++ b/changelog/unreleased/CLI-SECURITY-20240702-153935.yaml @@ -0,0 +1,10 @@ +component: CLI +kind: SECURITY +body: Allow to login using CY_API_TOKEN instead of providing the token via --api-token + flag +time: 2024-07-02T15:39:35.1456052Z +custom: + DETAILS: This will become the default way, using the flag will be deprecated in + the future. + PR: "274" + TYPE: CLI diff --git a/cmd/cycloid.go b/cmd/cycloid.go index 8b99c3c5..f1eb4ff0 100644 --- a/cmd/cycloid.go +++ b/cmd/cycloid.go @@ -47,7 +47,15 @@ func NewRootCommand() *cobra.Command { SilenceUsage: false, Use: "cy", Short: "Cycloid CLI", - Long: `Cy is a CLI for Cycloid framework. Learn more at https://www.cycloid.io/.`, + Long: `CLI tool to interact with Cycloid API. +Documentation at https://docs.beta.cycloid.io/reference/cli/ + +Environment: + +CY_API_URL -> Specify the HTTP url of Cycloid API to use, default https://http-api.cycloid.io +CY_ORG -> Set the current organization +CY_API_TOKEN -> Set the current API TOKEN +CY_VERBOSITY -> Set the verbosity level (debug, info, warning, error), default warning `, } rootCmd.PersistentFlags().StringVarP(&userOutput, "output", "o", "table", "The formatting style for command output: json|yaml|table") @@ -63,6 +71,9 @@ func NewRootCommand() *cobra.Command { rootCmd.PersistentFlags().Bool("insecure", false, "Decide to skip or not TLS verification") viper.BindPFlag("insecure", rootCmd.PersistentFlags().Lookup("insecure")) + rootCmd.PersistentFlags().String("org", "", "Specify the org to use. override CY_ORG env var. Required for all Org scoped endpoint.") + viper.BindPFlag("org", rootCmd.PersistentFlags().Lookup("org")) + // Remove usage on error, this is annoying in scripting rootCmd.SilenceUsage = true diff --git a/cmd/cycloid/apikey/cmd.go b/cmd/cycloid/apikey/cmd.go index e409f41b..ad3a59c9 100644 --- a/cmd/cycloid/apikey/cmd.go +++ b/cmd/cycloid/apikey/cmd.go @@ -2,8 +2,6 @@ package apikey import ( "github.com/spf13/cobra" - - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" ) var ( @@ -24,7 +22,6 @@ func NewCommands() *cobra.Command { Example: example, Short: short, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand( NewDeleteCommand(), diff --git a/cmd/cycloid/apikey/delete.go b/cmd/cycloid/apikey/delete.go index d0c52973..e00053f8 100644 --- a/cmd/cycloid/apikey/delete.go +++ b/cmd/cycloid/apikey/delete.go @@ -22,21 +22,7 @@ func NewDeleteCommand() *cobra.Command { # delete the API key 'my-key' in the org my-org cy api-key delete --org my-org --canonical my-key `, - RunE: func(cmd *cobra.Command, args []string) error { - org, err := cmd.Flags().GetString("org") - if err != nil { - return fmt.Errorf("unable to get org flag: %w", err) - } - output, err := cmd.Flags().GetString("output") - if err != nil { - return fmt.Errorf("unable to get output flag: %w", err) - } - canonical, err := cmd.Flags().GetString("canonical") - if err != nil { - return fmt.Errorf("unable to get canonical flag: %w", err) - } - return remove(cmd, org, canonical, output) - }, + RunE: remove, } WithFlagCanonical(cmd) @@ -46,7 +32,20 @@ func NewDeleteCommand() *cobra.Command { // remove will send the DELETE request to the API in order to // delete a generated token -func remove(cmd *cobra.Command, org, canonical, output string) error { +func remove(cmd *cobra.Command, args []string) error { + org, err := common.GetOrg(cmd) + if err != nil { + return fmt.Errorf("unable to get org flag: %w", err) + } + output, err := cmd.Flags().GetString("output") + if err != nil { + return fmt.Errorf("unable to get output flag: %w", err) + } + canonical, err := cmd.Flags().GetString("canonical") + if err != nil { + return fmt.Errorf("unable to get canonical flag: %w", err) + } + api := common.NewAPI() m := middleware.NewMiddleware(api) diff --git a/cmd/cycloid/apikey/get.go b/cmd/cycloid/apikey/get.go index c4353481..9b960e16 100644 --- a/cmd/cycloid/apikey/get.go +++ b/cmd/cycloid/apikey/get.go @@ -22,21 +22,7 @@ func NewGetCommand() *cobra.Command { # get API key 'my-key' in the org my-org cy api-key get --org my-org --canonical my-key `, - RunE: func(cmd *cobra.Command, args []string) error { - org, err := cmd.Flags().GetString("org") - if err != nil { - return fmt.Errorf("unable to get org flag: %w", err) - } - output, err := cmd.Flags().GetString("output") - if err != nil { - return fmt.Errorf("unable to get output flag: %w", err) - } - canonical, err := cmd.Flags().GetString("canonical") - if err != nil { - return fmt.Errorf("unable to get canonical flag: %w", err) - } - return get(cmd, org, canonical, output) - }, + RunE: get, } WithFlagCanonical(cmd) @@ -46,7 +32,20 @@ func NewGetCommand() *cobra.Command { // get will send the GET request to the API in order to // get the generated token -func get(cmd *cobra.Command, org, canonical, output string) error { +func get(cmd *cobra.Command, args []string) error { + org, err := common.GetOrg(cmd) + if err != nil { + return fmt.Errorf("unable to get org flag: %w", err) + } + output, err := cmd.Flags().GetString("output") + if err != nil { + return fmt.Errorf("unable to get output flag: %w", err) + } + canonical, err := cmd.Flags().GetString("canonical") + if err != nil { + return fmt.Errorf("unable to get canonical flag: %w", err) + } + api := common.NewAPI() m := middleware.NewMiddleware(api) diff --git a/cmd/cycloid/apikey/list.go b/cmd/cycloid/apikey/list.go index ad9ba20e..fe93fdb7 100644 --- a/cmd/cycloid/apikey/list.go +++ b/cmd/cycloid/apikey/list.go @@ -19,35 +19,35 @@ func NewListCommand() *cobra.Command { Use: "list", Short: "list API keys", Example: ` - # list API keys in the org my-org - cy api-key list --org my-org +# list API keys in the org my-org +cy api-key list --org my-org `, - RunE: func(cmd *cobra.Command, args []string) error { - org, err := cmd.Flags().GetString("org") - if err != nil { - return fmt.Errorf("unable to get org flag: %w", err) - } - output, err := cmd.Flags().GetString("output") - if err != nil { - return fmt.Errorf("unable to get output flag: %w", err) - } - return list(cmd, org, output) - }, + RunE: list, } return cmd } -// list will send the GET request to the API in order to // list the generated tokens -func list(cmd *cobra.Command, org, output string) error { +func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) + org, err := cmd.Flags().GetString("org") + if err != nil { + return err + } + + output, err := cmd.Flags().GetString("output") + if err != nil { + return fmt.Errorf("unable to get output flag: %w", err) + } + // fetch the printer from the factory p, err := factory.GetPrinter(output) if err != nil { return errors.Wrap(err, "unable to get printer") } + keys, err := m.ListAPIKey(org) return printer.SmartPrint(p, keys, err, "unable to list API keys", printer.Options{}, cmd.OutOrStdout()) } diff --git a/cmd/cycloid/catalog-repositories/cmd.go b/cmd/cycloid/catalog-repositories/cmd.go index e7f8a7ad..811c92a3 100644 --- a/cmd/cycloid/catalog-repositories/cmd.go +++ b/cmd/cycloid/catalog-repositories/cmd.go @@ -1,7 +1,6 @@ package catalogRepositories import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -15,7 +14,6 @@ func NewCommands() *cobra.Command { }, Short: "Manage the catalog repositories", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewCreateCommand(), NewUpdateCommand(), diff --git a/cmd/cycloid/catalog-repositories/create.go b/cmd/cycloid/catalog-repositories/create.go index b21a4d7e..acb3c4d7 100644 --- a/cmd/cycloid/catalog-repositories/create.go +++ b/cmd/cycloid/catalog-repositories/create.go @@ -43,7 +43,7 @@ func createCatalogRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/catalog-repositories/delete.go b/cmd/cycloid/catalog-repositories/delete.go index 9af5a81c..e6345019 100644 --- a/cmd/cycloid/catalog-repositories/delete.go +++ b/cmd/cycloid/catalog-repositories/delete.go @@ -30,7 +30,7 @@ func deleteCatalogRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/catalog-repositories/get.go b/cmd/cycloid/catalog-repositories/get.go index e78822ad..91a2bb1e 100644 --- a/cmd/cycloid/catalog-repositories/get.go +++ b/cmd/cycloid/catalog-repositories/get.go @@ -29,7 +29,7 @@ func getCatalogRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/catalog-repositories/list.go b/cmd/cycloid/catalog-repositories/list.go index 9a5cada7..90bb0a34 100644 --- a/cmd/cycloid/catalog-repositories/list.go +++ b/cmd/cycloid/catalog-repositories/list.go @@ -28,7 +28,7 @@ func listCatalogRepositories(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/catalog-repositories/refresh.go b/cmd/cycloid/catalog-repositories/refresh.go index 74d75f9c..76238c9c 100644 --- a/cmd/cycloid/catalog-repositories/refresh.go +++ b/cmd/cycloid/catalog-repositories/refresh.go @@ -31,7 +31,7 @@ func refreshCatalogRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/catalog-repositories/update.go b/cmd/cycloid/catalog-repositories/update.go index b3e4c708..08801c42 100644 --- a/cmd/cycloid/catalog-repositories/update.go +++ b/cmd/cycloid/catalog-repositories/update.go @@ -36,7 +36,7 @@ func updateCatalogRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/common/flags.go b/cmd/cycloid/common/flags.go index 228da51d..1c3b8c2d 100644 --- a/cmd/cycloid/common/flags.go +++ b/cmd/cycloid/common/flags.go @@ -1,7 +1,12 @@ package common import ( + "fmt" + "os" + + "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var ( @@ -9,10 +14,17 @@ var ( idFlag uint32 ) -func WithFlagOrg(cmd *cobra.Command) string { - flagName := "org" - cmd.PersistentFlags().StringVar(&orgFlag, flagName, "", "Org cannonical name") - return flagName +func GetOrg(cmd *cobra.Command) (org string, err error) { + org = viper.GetString("org") + if org == "" { + return "", errors.New("org is not set, use --org flag or CY_ORG env var") + } + + if viper.GetString("verbosity") == "debug" { + fmt.Fprintln(os.Stderr, "\033[1;34mdebug:\033[0m using org:", org) + } + + return org, nil } func WithFlagProject(cmd *cobra.Command) string { diff --git a/cmd/cycloid/common/helpers.go b/cmd/cycloid/common/helpers.go index f94679e8..56fcf449 100644 --- a/cmd/cycloid/common/helpers.go +++ b/cmd/cycloid/common/helpers.go @@ -158,8 +158,16 @@ func (a *APIClient) Credentials(org *string) runtime.ClientAuthInfoWriter { var token = a.Config.Token if token == "" { // we first try to get the token from the env variable - token = os.Getenv("TOKEN") + var ok bool + token, ok = os.LookupEnv("CY_API_TOKEN") + if !ok { + token, ok = os.LookupEnv("TOKEN") + if ok { + fmt.Println("TOKEN env var is deprecated, please use CY_API_TOKEN instead") + } + } } + // if the token is not set with env variable we try to fetch // him from the config (if the user is logged) if len(token) == 0 { @@ -332,8 +340,6 @@ func ParseFormsConfig(form *models.FormUseCase, getCurrent bool) (vars map[strin vars[*varEntity.Key] = value } - // We have to strings.ToLower() the groups and sections names - // otherwise, it will not be recognized as input for a create-env groups[strings.ToLower(*group.Name)] = vars } diff --git a/cmd/cycloid/config-repositories/cmd.go b/cmd/cycloid/config-repositories/cmd.go index 09edc295..93c59d94 100644 --- a/cmd/cycloid/config-repositories/cmd.go +++ b/cmd/cycloid/config-repositories/cmd.go @@ -2,8 +2,6 @@ package configRepositories import ( "github.com/spf13/cobra" - - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" ) func NewCommands() *cobra.Command { @@ -15,7 +13,6 @@ func NewCommands() *cobra.Command { }, Short: "Manage the catalog repositories", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewCreateCommand(), NewUpdateCommand(), diff --git a/cmd/cycloid/config-repositories/create.go b/cmd/cycloid/config-repositories/create.go index 3ed913ec..1a0c4b1e 100644 --- a/cmd/cycloid/config-repositories/create.go +++ b/cmd/cycloid/config-repositories/create.go @@ -23,8 +23,6 @@ func NewCreateCommand() *cobra.Command { PreRunE: internal.CheckAPIAndCLIVersion, } - // create --branch test --cred 105 --url "git@github.com:foo/bla.git" --name configname --default - common.RequiredFlag(common.WithFlagCred, cmd) common.RequiredFlag(WithFlagName, cmd) common.RequiredFlag(WithFlagBranch, cmd) @@ -38,7 +36,7 @@ func createConfigRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/config-repositories/delete.go b/cmd/cycloid/config-repositories/delete.go index ecd67fad..7fc61d99 100644 --- a/cmd/cycloid/config-repositories/delete.go +++ b/cmd/cycloid/config-repositories/delete.go @@ -32,7 +32,7 @@ func deleteConfigRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/config-repositories/get.go b/cmd/cycloid/config-repositories/get.go index 031066cb..905c55fd 100644 --- a/cmd/cycloid/config-repositories/get.go +++ b/cmd/cycloid/config-repositories/get.go @@ -31,7 +31,7 @@ func getConfigRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/config-repositories/list.go b/cmd/cycloid/config-repositories/list.go index ce290bc8..af84e56a 100644 --- a/cmd/cycloid/config-repositories/list.go +++ b/cmd/cycloid/config-repositories/list.go @@ -30,7 +30,7 @@ func listConfigRepositories(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/config-repositories/update.go b/cmd/cycloid/config-repositories/update.go index 82e2eae5..038c37c4 100644 --- a/cmd/cycloid/config-repositories/update.go +++ b/cmd/cycloid/config-repositories/update.go @@ -39,7 +39,7 @@ func updateConfigRepository(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/creds/cmd.go b/cmd/cycloid/creds/cmd.go index fc6d7930..44ec7304 100644 --- a/cmd/cycloid/creds/cmd.go +++ b/cmd/cycloid/creds/cmd.go @@ -1,7 +1,6 @@ package creds import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -22,7 +21,6 @@ func NewCommands() *cobra.Command { NewDeleteCommand(), NewListCommand(), NewGetCommand()) - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) return cmd } diff --git a/cmd/cycloid/creds/create.go b/cmd/cycloid/creds/create.go index 645b6b30..f225778e 100644 --- a/cmd/cycloid/creds/create.go +++ b/cmd/cycloid/creds/create.go @@ -3,6 +3,7 @@ package creds import ( "fmt" "io/ioutil" + "os" "strings" "github.com/pkg/errors" @@ -187,7 +188,7 @@ func create(cmd *cobra.Command, args []string) error { var rawCred *models.CredentialRaw credT := cmd.CalledAs() - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } @@ -226,7 +227,7 @@ func create(cmd *cobra.Command, args []string) error { return err } - sshKey, err := ioutil.ReadFile(sshKeyPath) + sshKey, err := os.ReadFile(sshKeyPath) if err != nil { return errors.Wrap(err, "unable to read SSH key") } diff --git a/cmd/cycloid/creds/delete.go b/cmd/cycloid/creds/delete.go index 41359993..b7a93513 100644 --- a/cmd/cycloid/creds/delete.go +++ b/cmd/cycloid/creds/delete.go @@ -31,7 +31,7 @@ func del(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/creds/get.go b/cmd/cycloid/creds/get.go index 2a151da3..0fcc750a 100644 --- a/cmd/cycloid/creds/get.go +++ b/cmd/cycloid/creds/get.go @@ -32,7 +32,7 @@ func get(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/creds/list.go b/cmd/cycloid/creds/list.go index d380677d..e2c1d0e5 100644 --- a/cmd/cycloid/creds/list.go +++ b/cmd/cycloid/creds/list.go @@ -31,7 +31,7 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/creds/update.go b/cmd/cycloid/creds/update.go index 879af8e8..5996eb7c 100644 --- a/cmd/cycloid/creds/update.go +++ b/cmd/cycloid/creds/update.go @@ -3,6 +3,7 @@ package creds import ( "fmt" "io/ioutil" + "os" "strings" "github.com/cycloidio/cycloid-cli/client/models" @@ -198,7 +199,7 @@ func update(cmd *cobra.Command, args []string) error { var rawCred *models.CredentialRaw credT := cmd.CalledAs() - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } @@ -237,7 +238,7 @@ func update(cmd *cobra.Command, args []string) error { return err } - sshKey, err := ioutil.ReadFile(sshKeyPath) + sshKey, err := os.ReadFile(sshKeyPath) if err != nil { return errors.Wrap(err, "unable to read SSH key") } diff --git a/cmd/cycloid/events/cmd.go b/cmd/cycloid/events/cmd.go index d281e866..814362d5 100644 --- a/cmd/cycloid/events/cmd.go +++ b/cmd/cycloid/events/cmd.go @@ -1,7 +1,6 @@ package events import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -15,8 +14,6 @@ func NewCommands() *cobra.Command { Short: "Manage the events", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) - cmd.AddCommand(NewSendCommand()) return cmd diff --git a/cmd/cycloid/events/send.go b/cmd/cycloid/events/send.go index cc137b51..c17b7687 100644 --- a/cmd/cycloid/events/send.go +++ b/cmd/cycloid/events/send.go @@ -2,7 +2,7 @@ package events import ( "fmt" - "io/ioutil" + "os" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -58,7 +58,7 @@ func send(cmd *cobra.Command, args []string) error { var err error - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } @@ -104,7 +104,7 @@ func send(cmd *cobra.Command, args []string) error { var msg string if messageFile != "" { - rawMsg, err := ioutil.ReadFile(messageFile) + rawMsg, err := os.ReadFile(messageFile) if err != nil { return errors.Wrap(err, "unable to read message file") } diff --git a/cmd/cycloid/external-backends/cmd.go b/cmd/cycloid/external-backends/cmd.go index 1d09b7ae..118c9b79 100644 --- a/cmd/cycloid/external-backends/cmd.go +++ b/cmd/cycloid/external-backends/cmd.go @@ -1,7 +1,6 @@ package externalBackends import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -14,7 +13,6 @@ func NewCommands() *cobra.Command { }, Short: "manage external backends", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewGetCommand(), NewDeleteCommand(), diff --git a/cmd/cycloid/external-backends/create_events.go b/cmd/cycloid/external-backends/create_events.go index 7383dc9f..024331a5 100644 --- a/cmd/cycloid/external-backends/create_events.go +++ b/cmd/cycloid/external-backends/create_events.go @@ -19,13 +19,13 @@ func createEvents(cmd *cobra.Command, args []string) error { var ( err error - org, cred string + cred string purpose = "events" ebC models.ExternalBackendConfiguration engine = cmd.CalledAs() ) - org, err = cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/external-backends/create_infra_view.go b/cmd/cycloid/external-backends/create_infra_view.go index 45cb78bd..f3cc1a32 100644 --- a/cmd/cycloid/external-backends/create_infra_view.go +++ b/cmd/cycloid/external-backends/create_infra_view.go @@ -18,19 +18,19 @@ func createInfraView(cmd *cobra.Command, args []string) error { m := middleware.NewMiddleware(api) var ( - purpose = "remote_tfstate" - err error - org, project, env string - ebC models.ExternalBackendConfiguration - engine = cmd.CalledAs() - defaultEB bool + purpose = "remote_tfstate" + err error + project, env string + ebC models.ExternalBackendConfiguration + engine = cmd.CalledAs() + defaultEB bool ) output, err := cmd.Flags().GetString("output") if err != nil { return errors.Wrap(err, "unable to get output flag") } - org, err = cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/external-backends/create_logs.go b/cmd/cycloid/external-backends/create_logs.go index 1d4f4ae9..f06591cc 100644 --- a/cmd/cycloid/external-backends/create_logs.go +++ b/cmd/cycloid/external-backends/create_logs.go @@ -29,7 +29,7 @@ func createLogs(cmd *cobra.Command, args []string) error { if err != nil { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/external-backends/delete.go b/cmd/cycloid/external-backends/delete.go index 82aaa34e..1e31f873 100644 --- a/cmd/cycloid/external-backends/delete.go +++ b/cmd/cycloid/external-backends/delete.go @@ -23,7 +23,6 @@ func NewDeleteCommand() *cobra.Command { PreRunE: internal.CheckAPIAndCLIVersion, } common.RequiredFlag(common.WithFlagID, cmd) - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) return cmd } @@ -32,7 +31,7 @@ func del(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/external-backends/list.go b/cmd/cycloid/external-backends/list.go index cb2fe4b6..e296f18e 100644 --- a/cmd/cycloid/external-backends/list.go +++ b/cmd/cycloid/external-backends/list.go @@ -34,8 +34,6 @@ func NewListCommand() *cobra.Command { PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) - return cmd } @@ -43,7 +41,7 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/infrapolicies/cmd.go b/cmd/cycloid/infrapolicies/cmd.go index fe8e0130..7d26c910 100644 --- a/cmd/cycloid/infrapolicies/cmd.go +++ b/cmd/cycloid/infrapolicies/cmd.go @@ -2,8 +2,6 @@ package infrapolicies import ( "github.com/spf13/cobra" - - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" ) func NewCommands() *cobra.Command { @@ -18,13 +16,12 @@ func NewCommands() *cobra.Command { Short: "Manage the infraPolicies", } - common.RequiredFlag(common.WithFlagOrg, cmd) - cmd.AddCommand(NewValidateCommand(), NewUpdateCommand(), NewCreateCommand(), NewListCommand(), NewGetCommand(), NewDeleteCommand()) + return cmd } diff --git a/cmd/cycloid/infrapolicies/create.go b/cmd/cycloid/infrapolicies/create.go index 90077a26..9fafb8de 100644 --- a/cmd/cycloid/infrapolicies/create.go +++ b/cmd/cycloid/infrapolicies/create.go @@ -49,7 +49,7 @@ func create(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/infrapolicies/delete.go b/cmd/cycloid/infrapolicies/delete.go index 8a5ea745..ecf65619 100644 --- a/cmd/cycloid/infrapolicies/delete.go +++ b/cmd/cycloid/infrapolicies/delete.go @@ -35,7 +35,7 @@ func delete(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/infrapolicies/get.go b/cmd/cycloid/infrapolicies/get.go index b267293f..542837be 100644 --- a/cmd/cycloid/infrapolicies/get.go +++ b/cmd/cycloid/infrapolicies/get.go @@ -35,7 +35,7 @@ func get(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/infrapolicies/list.go b/cmd/cycloid/infrapolicies/list.go index d4e89119..74f56424 100644 --- a/cmd/cycloid/infrapolicies/list.go +++ b/cmd/cycloid/infrapolicies/list.go @@ -36,7 +36,7 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/infrapolicies/update.go b/cmd/cycloid/infrapolicies/update.go index 9bba2843..6d10f72e 100644 --- a/cmd/cycloid/infrapolicies/update.go +++ b/cmd/cycloid/infrapolicies/update.go @@ -50,7 +50,7 @@ func update(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/infrapolicies/validate.go b/cmd/cycloid/infrapolicies/validate.go index 0686980d..07759b92 100644 --- a/cmd/cycloid/infrapolicies/validate.go +++ b/cmd/cycloid/infrapolicies/validate.go @@ -2,7 +2,7 @@ package infrapolicies import ( "fmt" - "io/ioutil" + "os" "github.com/spf13/cobra" @@ -35,7 +35,7 @@ func NewValidateCommand() *cobra.Command { // validate will send the GET request to the API in order to // validate the terraform Plan located in planPath func validate(cmd *cobra.Command, args []string) error { - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return fmt.Errorf("unable to validate org flag: %w", err) } @@ -56,7 +56,7 @@ func validate(cmd *cobra.Command, args []string) error { return fmt.Errorf("unable to get output flag: %w", err) } - plan, err := ioutil.ReadFile(planPath) + plan, err := os.ReadFile(planPath) if err != nil { return fmt.Errorf("unable to read terraform plan file: %w", err) } diff --git a/cmd/cycloid/kpis/cmd.go b/cmd/cycloid/kpis/cmd.go index d612a8af..c2452ae3 100644 --- a/cmd/cycloid/kpis/cmd.go +++ b/cmd/cycloid/kpis/cmd.go @@ -1,7 +1,6 @@ package kpis import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -13,7 +12,6 @@ func NewCommands() *cobra.Command { }, Short: "Manage the kpis", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewCreateCommand(), NewDeleteCommand(), diff --git a/cmd/cycloid/kpis/create.go b/cmd/cycloid/kpis/create.go index 593f4b03..1ce183a1 100644 --- a/cmd/cycloid/kpis/create.go +++ b/cmd/cycloid/kpis/create.go @@ -44,7 +44,7 @@ func create(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/kpis/delete.go b/cmd/cycloid/kpis/delete.go index 378dd9a4..cfdd32d3 100644 --- a/cmd/cycloid/kpis/delete.go +++ b/cmd/cycloid/kpis/delete.go @@ -33,7 +33,7 @@ func delete(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/kpis/list.go b/cmd/cycloid/kpis/list.go index c1cd0e61..3da61fc7 100644 --- a/cmd/cycloid/kpis/list.go +++ b/cmd/cycloid/kpis/list.go @@ -33,7 +33,7 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/login/common.go b/cmd/cycloid/login/common.go deleted file mode 100644 index f583bbde..00000000 --- a/cmd/cycloid/login/common.go +++ /dev/null @@ -1,20 +0,0 @@ -package login - -import "github.com/spf13/cobra" - -var ( - apiKeyFlag string - orgFlag string -) - -func WithFlagAPIKey(cmd *cobra.Command) string { - flagName := "api-key" - cmd.Flags().StringVar(&apiKeyFlag, flagName, "", "API key") - return flagName -} - -func WithFlagOrg(cmd *cobra.Command) string { - flagName := "org" - cmd.Flags().StringVar(&orgFlag, flagName, "", "Org cannonical name") - return flagName -} diff --git a/cmd/cycloid/login/login.go b/cmd/cycloid/login/login.go index e4521ade..f1e51de8 100644 --- a/cmd/cycloid/login/login.go +++ b/cmd/cycloid/login/login.go @@ -3,10 +3,13 @@ package login import ( "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/spf13/viper" "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/cycloidio/cycloid-cli/cmd/cycloid/internal" "github.com/cycloidio/cycloid-cli/config" + "github.com/cycloidio/cycloid-cli/printer" + "github.com/cycloidio/cycloid-cli/printer/factory" ) // NewCommands returns the cobra command holding @@ -15,28 +18,15 @@ func NewCommands() *cobra.Command { cmd := &cobra.Command{ Use: "login", Short: "Login against the Cycloid console", - Example: `, - # Login in an org using api-key - cy login --org my-org --api-key eyJhbGciOiJI... -`, + Example: `# Login in an org using api-key +export CY_API_KEY=xxxx +cy login --org my-org`, PreRunE: internal.CheckAPIAndCLIVersion, - RunE: func(cmd *cobra.Command, args []string) error { - - org, err := cmd.Flags().GetString("org") - if err != nil { - return errors.Wrap(err, "unable to get org flag") - } - apiKey, err := cmd.Flags().GetString("api-key") - if err != nil { - return errors.Wrap(err, "unable to get child flag") - } - - return login(org, apiKey) - }, + RunE: login, } - common.RequiredFlag(WithFlagAPIKey, cmd) - common.RequiredFlag(WithFlagOrg, cmd) + cmd.Flags().String("api-key", "", "[deprecated] set the API key, use CY_API_KEY env var instead.") + viper.BindPFlag("api-key", cmd.Flags().Lookup("api-key")) cmd.AddCommand( NewListCommand(), @@ -45,17 +35,43 @@ func NewCommands() *cobra.Command { return cmd } -func login(org, key string) error { - - // we save the new token and remove the previous one +func login(cmd *cobra.Command, args []string) error { conf, _ := config.Read() + // If err != nil, the file does not exist, we create it anyway + + org, err := common.GetOrg(cmd) + if err != nil { + return errors.Wrap(err, "unable to get org flag") + } + + p, err := factory.GetPrinter(viper.GetString("output")) + if err != nil { + return errors.Wrap(err, "unable to get printer") + } + + // Get api key via env var or cli flag + apiKey := viper.GetString("api-key") + if apiKey == "" { + return printer.SmartPrint(p, nil, nil, "CY_API_KEY is not set or invalid", printer.Options{}, cmd.OutOrStderr()) + } + + // Warn user about deprecation + if cmd.Flags().Lookup("api-key").Changed { + internal.Warning(cmd.ErrOrStderr(), "--api-key is deprecated, use CY_API_KEY env var instead") + } + + // Check for a nil map. + // This can be the case if the config file is empty + if conf.Organizations == nil { + conf.Organizations = make(map[string]config.Organization) + } conf.Organizations[org] = config.Organization{ - Token: key, + Token: apiKey, } if err := config.Write(conf); err != nil { - return errors.Wrap(err, "unable to save config") + return printer.SmartPrint(p, nil, err, "unable to write config file", printer.Options{}, cmd.OutOrStderr()) } return nil diff --git a/cmd/cycloid/members/cmd.go b/cmd/cycloid/members/cmd.go index 37a606bf..7489d215 100644 --- a/cmd/cycloid/members/cmd.go +++ b/cmd/cycloid/members/cmd.go @@ -2,8 +2,6 @@ package members import ( "github.com/spf13/cobra" - - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" ) func NewCommands() *cobra.Command { @@ -22,7 +20,6 @@ func NewCommands() *cobra.Command { Short: short, Long: long, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewUpdateCommand(), NewDeleteCommand(), diff --git a/cmd/cycloid/members/delete-invite.go b/cmd/cycloid/members/delete-invite.go index 6ba4e482..f38826e0 100644 --- a/cmd/cycloid/members/delete-invite.go +++ b/cmd/cycloid/members/delete-invite.go @@ -43,7 +43,7 @@ func deleteInvite(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/members/delete.go b/cmd/cycloid/members/delete.go index 2c30d2fd..18cfd3f5 100644 --- a/cmd/cycloid/members/delete.go +++ b/cmd/cycloid/members/delete.go @@ -39,7 +39,7 @@ func deleteMember(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/members/get.go b/cmd/cycloid/members/get.go index c28d6a72..93d2155c 100644 --- a/cmd/cycloid/members/get.go +++ b/cmd/cycloid/members/get.go @@ -44,7 +44,7 @@ func getMember(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/members/invite.go b/cmd/cycloid/members/invite.go index a7e59b92..547b6bb5 100644 --- a/cmd/cycloid/members/invite.go +++ b/cmd/cycloid/members/invite.go @@ -40,7 +40,7 @@ func inviteMember(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/members/list-invites.go b/cmd/cycloid/members/list-invites.go index feae7328..423c759d 100644 --- a/cmd/cycloid/members/list-invites.go +++ b/cmd/cycloid/members/list-invites.go @@ -42,7 +42,7 @@ func listInvites(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/members/list.go b/cmd/cycloid/members/list.go index 81c164a0..22823f29 100644 --- a/cmd/cycloid/members/list.go +++ b/cmd/cycloid/members/list.go @@ -42,7 +42,7 @@ func listMembers(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/members/update.go b/cmd/cycloid/members/update.go index b9f69ca3..e1cbd6ec 100644 --- a/cmd/cycloid/members/update.go +++ b/cmd/cycloid/members/update.go @@ -47,7 +47,7 @@ func updateConfigRepository(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/organizations/create-children.go b/cmd/cycloid/organizations/create-children.go index e38fd8a5..c30a0430 100644 --- a/cmd/cycloid/organizations/create-children.go +++ b/cmd/cycloid/organizations/create-children.go @@ -18,7 +18,6 @@ func NewCreateChildCommand() *cobra.Command { RunE: createChild, PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) common.RequiredPersistentFlag(WithFlagParentOrganization, cmd) return cmd @@ -28,10 +27,11 @@ func createChild(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } + porg, err := cmd.Flags().GetString("parent-org") if err != nil { return err diff --git a/cmd/cycloid/organizations/delete.go b/cmd/cycloid/organizations/delete.go index 399158e4..dda49147 100644 --- a/cmd/cycloid/organizations/delete.go +++ b/cmd/cycloid/organizations/delete.go @@ -24,8 +24,6 @@ func NewDeleteCommand() *cobra.Command { PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) - return cmd } @@ -33,7 +31,7 @@ func del(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return errors.Wrap(err, "unable get org flag") } diff --git a/cmd/cycloid/organizations/get.go b/cmd/cycloid/organizations/get.go index c41bd49d..677470f1 100644 --- a/cmd/cycloid/organizations/get.go +++ b/cmd/cycloid/organizations/get.go @@ -22,7 +22,6 @@ func NewGetCommand() *cobra.Command { RunE: get, PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) return cmd } @@ -31,7 +30,7 @@ func get(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/organizations/list-children.go b/cmd/cycloid/organizations/list-children.go index 18ab8a2b..c00489ad 100644 --- a/cmd/cycloid/organizations/list-children.go +++ b/cmd/cycloid/organizations/list-children.go @@ -21,7 +21,6 @@ func NewListChildrensCommand() *cobra.Command { RunE: listChildrens, PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) return cmd } @@ -30,7 +29,7 @@ func listChildrens(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/organizations/list-workers.go b/cmd/cycloid/organizations/list-workers.go index e75bc1c3..49ddd16b 100644 --- a/cmd/cycloid/organizations/list-workers.go +++ b/cmd/cycloid/organizations/list-workers.go @@ -18,7 +18,6 @@ func NewListWorkersCommand() *cobra.Command { RunE: listWorkers, PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) return cmd } @@ -27,7 +26,7 @@ func listWorkers(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/organizations/update.go b/cmd/cycloid/organizations/update.go index b14d1d7f..8e062f7e 100644 --- a/cmd/cycloid/organizations/update.go +++ b/cmd/cycloid/organizations/update.go @@ -25,7 +25,6 @@ func NewUpdateCommand() *cobra.Command { PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) common.RequiredFlag(WithFlagName, cmd) return cmd @@ -40,7 +39,7 @@ func update(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return errors.Wrap(err, "unable get org flag") } diff --git a/cmd/cycloid/pipelines/clear-task-cache.go b/cmd/cycloid/pipelines/clear-task-cache.go index 4dbfb080..6086e83f 100644 --- a/cmd/cycloid/pipelines/clear-task-cache.go +++ b/cmd/cycloid/pipelines/clear-task-cache.go @@ -1,7 +1,6 @@ package pipelines import ( - "github.com/pkg/errors" "github.com/spf13/cobra" @@ -36,7 +35,7 @@ func cleartaskCache(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/cmd.go b/cmd/cycloid/pipelines/cmd.go index 3d651ea6..461b0619 100644 --- a/cmd/cycloid/pipelines/cmd.go +++ b/cmd/cycloid/pipelines/cmd.go @@ -1,7 +1,6 @@ package pipelines import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -15,8 +14,6 @@ func NewCommands() *cobra.Command { Short: "Manage the pipelines", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) - cmd.AddCommand(NewUnpauseJobCommand(), NewUpdateCommand(), NewGetJobCommand(), diff --git a/cmd/cycloid/pipelines/diff.go b/cmd/cycloid/pipelines/diff.go index 4cb0afb3..050992c0 100644 --- a/cmd/cycloid/pipelines/diff.go +++ b/cmd/cycloid/pipelines/diff.go @@ -2,7 +2,7 @@ package pipelines import ( "fmt" - "io/ioutil" + "os" "github.com/pkg/errors" @@ -38,7 +38,7 @@ func diff(cmd *cobra.Command, args []string) error { var err error - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } @@ -69,13 +69,13 @@ func diff(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "unable to get printer") } - rawPipeline, err := ioutil.ReadFile(pipelinePath) + rawPipeline, err := os.ReadFile(pipelinePath) if err != nil { return fmt.Errorf("Pipeline file reading error : %s", err.Error()) } pipelineTemplate := string(rawPipeline) - rawVars, err := ioutil.ReadFile(varsPath) + rawVars, err := os.ReadFile(varsPath) if err != nil { return fmt.Errorf("Pipeline variables file reading error : %s", err.Error()) } diff --git a/cmd/cycloid/pipelines/get-job.go b/cmd/cycloid/pipelines/get-job.go index d664db75..55865527 100644 --- a/cmd/cycloid/pipelines/get-job.go +++ b/cmd/cycloid/pipelines/get-job.go @@ -34,7 +34,7 @@ func getJob(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/get.go b/cmd/cycloid/pipelines/get.go index 1e6f7310..17d3337f 100644 --- a/cmd/cycloid/pipelines/get.go +++ b/cmd/cycloid/pipelines/get.go @@ -23,8 +23,6 @@ func NewGetCommand() *cobra.Command { RunE: get, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) - common.RequiredPersistentFlag(common.WithFlagProject, cmd) common.RequiredPersistentFlag(common.WithFlagEnv, cmd) @@ -35,7 +33,7 @@ func get(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/list-builds.go b/cmd/cycloid/pipelines/list-builds.go index 436904f4..e7502300 100644 --- a/cmd/cycloid/pipelines/list-builds.go +++ b/cmd/cycloid/pipelines/list-builds.go @@ -34,7 +34,7 @@ func listBuilds(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/list-jobs.go b/cmd/cycloid/pipelines/list-jobs.go index 70107820..39706f8f 100644 --- a/cmd/cycloid/pipelines/list-jobs.go +++ b/cmd/cycloid/pipelines/list-jobs.go @@ -33,10 +33,11 @@ func listJobs(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } + project, err := cmd.Flags().GetString("project") if err != nil { return err diff --git a/cmd/cycloid/pipelines/list.go b/cmd/cycloid/pipelines/list.go index a96eaa12..aefb2930 100644 --- a/cmd/cycloid/pipelines/list.go +++ b/cmd/cycloid/pipelines/list.go @@ -26,7 +26,7 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/pause-job.go b/cmd/cycloid/pipelines/pause-job.go index 201861b7..d4093866 100644 --- a/cmd/cycloid/pipelines/pause-job.go +++ b/cmd/cycloid/pipelines/pause-job.go @@ -34,7 +34,7 @@ func pauseJob(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/pause.go b/cmd/cycloid/pipelines/pause.go index 43e1a91b..19feb1d9 100644 --- a/cmd/cycloid/pipelines/pause.go +++ b/cmd/cycloid/pipelines/pause.go @@ -33,7 +33,7 @@ func pause(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/synced.go b/cmd/cycloid/pipelines/synced.go index 315d2aec..7f0d8be2 100644 --- a/cmd/cycloid/pipelines/synced.go +++ b/cmd/cycloid/pipelines/synced.go @@ -25,8 +25,6 @@ func NewSyncedCommand() *cobra.Command { RunE: synced, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) - common.RequiredPersistentFlag(common.WithFlagProject, cmd) common.RequiredPersistentFlag(common.WithFlagEnv, cmd) @@ -41,14 +39,17 @@ func synced(cmd *cobra.Command, args []string) error { if err != nil { return err } + project, err := cmd.Flags().GetString("project") if err != nil { return err } + env, err := cmd.Flags().GetString("env") if err != nil { return err } + output, err := cmd.Flags().GetString("output") if err != nil { return errors.Wrap(err, "unable to get output flag") diff --git a/cmd/cycloid/pipelines/trigger-build.go b/cmd/cycloid/pipelines/trigger-build.go index 9d9c2264..9688557f 100644 --- a/cmd/cycloid/pipelines/trigger-build.go +++ b/cmd/cycloid/pipelines/trigger-build.go @@ -34,10 +34,11 @@ func createBuild(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } + project, err := cmd.Flags().GetString("project") if err != nil { return err diff --git a/cmd/cycloid/pipelines/unpause-job.go b/cmd/cycloid/pipelines/unpause-job.go index 71414634..a9d42353 100644 --- a/cmd/cycloid/pipelines/unpause-job.go +++ b/cmd/cycloid/pipelines/unpause-job.go @@ -34,7 +34,7 @@ func unpauseJob(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/pipelines/unpause.go b/cmd/cycloid/pipelines/unpause.go index 80a11d72..b8f22d64 100644 --- a/cmd/cycloid/pipelines/unpause.go +++ b/cmd/cycloid/pipelines/unpause.go @@ -16,8 +16,8 @@ func NewUnpauseCommand() *cobra.Command { Use: "unpause", Short: "unpause a pipeline", Example: ` - # unpause pipeline my-project-env - cy --org my-org pipeline unpause --project my-project --env env +# unpause pipeline my-project-env +cy --org my-org pipeline unpause --project my-project --env env `, RunE: unpause, PreRunE: internal.CheckAPIAndCLIVersion, @@ -33,18 +33,21 @@ func unpause(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } + project, err := cmd.Flags().GetString("project") if err != nil { return err } + env, err := cmd.Flags().GetString("env") if err != nil { return err } + output, err := cmd.Flags().GetString("output") if err != nil { return errors.Wrap(err, "unable to get output flag") diff --git a/cmd/cycloid/pipelines/update.go b/cmd/cycloid/pipelines/update.go index b6cd441c..e45203bc 100644 --- a/cmd/cycloid/pipelines/update.go +++ b/cmd/cycloid/pipelines/update.go @@ -1,7 +1,7 @@ package pipelines import ( - "io/ioutil" + "os" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -40,10 +40,11 @@ func update(cmd *cobra.Command, args []string) error { var err error - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } + project, err := cmd.Flags().GetString("project") if err != nil { return err @@ -71,13 +72,13 @@ func update(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "unable to get printer") } - rawPipeline, err := ioutil.ReadFile(pipelinePath) + rawPipeline, err := os.ReadFile(pipelinePath) if err != nil { return errors.Wrap(err, "unable to read pipeline file") } pipeline := string(rawPipeline) - rawVars, err := ioutil.ReadFile(varsPath) + rawVars, err := os.ReadFile(varsPath) if err != nil { return errors.Wrap(err, "unable to read variables file") } diff --git a/cmd/cycloid/projects/cmd.go b/cmd/cycloid/projects/cmd.go index 0504dfee..78f51b9c 100644 --- a/cmd/cycloid/projects/cmd.go +++ b/cmd/cycloid/projects/cmd.go @@ -1,7 +1,6 @@ package projects import ( - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" "github.com/spf13/cobra" ) @@ -14,7 +13,6 @@ func NewCommands() *cobra.Command { }, Short: "Manage the projects", } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewDeleteCommand(), NewCreateCommand(), diff --git a/cmd/cycloid/projects/create-env.go b/cmd/cycloid/projects/create-env.go index 906b28dd..9e6e15d1 100644 --- a/cmd/cycloid/projects/create-env.go +++ b/cmd/cycloid/projects/create-env.go @@ -64,7 +64,6 @@ cy project get-env-config --org my-org --project my-project --env prod \ RunE: createEnv, } - common.WithFlagOrg(cmd) cmd.PersistentFlags().StringP("project", "p", "", "project name") cmd.MarkFlagRequired("project") cmd.PersistentFlags().StringP("env", "e", "", "environment name") @@ -86,18 +85,11 @@ func createEnv(cmd *cobra.Command, args []string) error { var err error - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } - // Right now, in the way orgs are handled, there is a possibility - // of an empty org. - // TODO: Fix in another PR about orgs. - if org == "" { - return errors.New("org is empty, please specify an org with --org") - } - project, err := cmd.Flags().GetString("project") if err != nil { return err diff --git a/cmd/cycloid/projects/create-raw-env.go b/cmd/cycloid/projects/create-raw-env.go index a022702f..74a276b3 100644 --- a/cmd/cycloid/projects/create-raw-env.go +++ b/cmd/cycloid/projects/create-raw-env.go @@ -63,7 +63,7 @@ func createRawEnv(cmd *cobra.Command, args []string) error { var err error - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/projects/create.go b/cmd/cycloid/projects/create.go index 7c624535..033c8525 100644 --- a/cmd/cycloid/projects/create.go +++ b/cmd/cycloid/projects/create.go @@ -53,7 +53,7 @@ func create(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/projects/delete-env.go b/cmd/cycloid/projects/delete-env.go index c738ab3d..f51f86e6 100644 --- a/cmd/cycloid/projects/delete-env.go +++ b/cmd/cycloid/projects/delete-env.go @@ -32,7 +32,7 @@ func deleteEnv(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/projects/delete.go b/cmd/cycloid/projects/delete.go index 0d6dbc1e..5051ede4 100644 --- a/cmd/cycloid/projects/delete.go +++ b/cmd/cycloid/projects/delete.go @@ -31,7 +31,7 @@ func del(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/projects/get-env.go b/cmd/cycloid/projects/get-env.go index d700a238..c5c0d776 100644 --- a/cmd/cycloid/projects/get-env.go +++ b/cmd/cycloid/projects/get-env.go @@ -48,7 +48,6 @@ cy --org my-org project get-env-config my-project my-project use_case -o yaml`, Args: cobra.RangeArgs(0, 2), } - common.WithFlagOrg(cmd) cmd.Flags().StringP("project", "p", "", "specify the project") cmd.Flags().StringP("env", "e", "", "specify the env") cmd.Flags().BoolP("default", "d", false, "if set, will fetch the default value from the stack instead of the current ones.") @@ -85,7 +84,7 @@ func getEnvConfig(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/projects/get.go b/cmd/cycloid/projects/get.go index e314d23e..acc2cf07 100644 --- a/cmd/cycloid/projects/get.go +++ b/cmd/cycloid/projects/get.go @@ -32,7 +32,7 @@ func get(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/projects/list.go b/cmd/cycloid/projects/list.go index eb3eb8d7..7f05e7e4 100644 --- a/cmd/cycloid/projects/list.go +++ b/cmd/cycloid/projects/list.go @@ -29,10 +29,11 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { - return err + return errors.Wrap(err, "unable to get org") } + output, err := cmd.Flags().GetString("output") if err != nil { return errors.Wrap(err, "unable to get output flag") diff --git a/cmd/cycloid/roles/cmd.go b/cmd/cycloid/roles/cmd.go index bae802e2..5da2e851 100644 --- a/cmd/cycloid/roles/cmd.go +++ b/cmd/cycloid/roles/cmd.go @@ -2,8 +2,6 @@ package roles import ( "github.com/spf13/cobra" - - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" ) func NewCommands() *cobra.Command { @@ -22,7 +20,6 @@ func NewCommands() *cobra.Command { Short: short, Long: long, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.AddCommand(NewDeleteCommand(), NewListCommand(), diff --git a/cmd/cycloid/roles/delete.go b/cmd/cycloid/roles/delete.go index f6a6d0bf..c9d1443b 100644 --- a/cmd/cycloid/roles/delete.go +++ b/cmd/cycloid/roles/delete.go @@ -39,7 +39,7 @@ func deleteRole(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/roles/get.go b/cmd/cycloid/roles/get.go index e72c59df..8f10143c 100644 --- a/cmd/cycloid/roles/get.go +++ b/cmd/cycloid/roles/get.go @@ -44,7 +44,7 @@ func getRole(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/roles/list.go b/cmd/cycloid/roles/list.go index 0af97424..aeac294b 100644 --- a/cmd/cycloid/roles/list.go +++ b/cmd/cycloid/roles/list.go @@ -42,7 +42,7 @@ func listRoles(cmd *cobra.Command, args []string) error { return err } - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/stacks/get.go b/cmd/cycloid/stacks/get.go index 206bb468..6f8b4ebc 100644 --- a/cmd/cycloid/stacks/get.go +++ b/cmd/cycloid/stacks/get.go @@ -24,7 +24,6 @@ func NewGetCommand() *cobra.Command { RunE: get, PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) cmd.Flags().StringVar(&refFlag, "ref", "", "referential of the stack") cmd.MarkFlagRequired("ref") @@ -36,7 +35,7 @@ func get(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/stacks/list.go b/cmd/cycloid/stacks/list.go index 76ac7a72..4ce05c57 100644 --- a/cmd/cycloid/stacks/list.go +++ b/cmd/cycloid/stacks/list.go @@ -22,7 +22,6 @@ func NewListCommand() *cobra.Command { RunE: list, PreRunE: internal.CheckAPIAndCLIVersion, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) return cmd } @@ -31,7 +30,7 @@ func list(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/stacks/validate-form.go b/cmd/cycloid/stacks/validate-form.go index 6f88d7be..775d5fa5 100644 --- a/cmd/cycloid/stacks/validate-form.go +++ b/cmd/cycloid/stacks/validate-form.go @@ -24,7 +24,6 @@ func NewValidateFormCmd() *cobra.Command { RunE: validateForm, } - common.RequiredPersistentFlag(common.WithFlagOrg, cmd) common.RequiredPersistentFlag(WithFlagForms, cmd) return cmd @@ -34,7 +33,7 @@ func validateForm(cmd *cobra.Command, args []string) error { api := common.NewAPI() m := middleware.NewMiddleware(api) - org, err := cmd.Flags().GetString("org") + org, err := common.GetOrg(cmd) if err != nil { return err } diff --git a/cmd/cycloid/terracost/cmd.go b/cmd/cycloid/terracost/cmd.go index dabe4ca6..17eee9ad 100644 --- a/cmd/cycloid/terracost/cmd.go +++ b/cmd/cycloid/terracost/cmd.go @@ -2,8 +2,6 @@ package terracost import ( "github.com/spf13/cobra" - - "github.com/cycloidio/cycloid-cli/cmd/cycloid/common" ) // NewCommands returns an implementation of Terracost commands @@ -17,8 +15,6 @@ func NewCommands() *cobra.Command { Short: "Use terracost feature", } - common.RequiredFlag(common.WithFlagOrg, cmd) - cmd.AddCommand(NewEstimateCommand()) return cmd } diff --git a/cmd/cycloid/terracost/estimate.go b/cmd/cycloid/terracost/estimate.go index e2f170ab..a880c47b 100644 --- a/cmd/cycloid/terracost/estimate.go +++ b/cmd/cycloid/terracost/estimate.go @@ -2,7 +2,7 @@ package terracost import ( "fmt" - "io/ioutil" + "os" "github.com/spf13/cobra" @@ -16,29 +16,27 @@ import ( // of the cost estimation func NewEstimateCommand() *cobra.Command { cmd := &cobra.Command{ - Use: "estimate", - RunE: func(cmd *cobra.Command, args []string) error { - org, err := cmd.Flags().GetString("org") - if err != nil { - return fmt.Errorf("unable to validate org flag: %w", err) - } - planPath, err := cmd.Flags().GetString("plan-path") - if err != nil { - return fmt.Errorf("unable to get plan path flag: %w", err) - } - output, err := cmd.Flags().GetString("output") - if err != nil { - return fmt.Errorf("unable to get output flag: %w", err) - } - return estimate(cmd, org, planPath, output) - }, + Use: "estimate", + RunE: estimate, } common.RequiredFlag(WithFlagPlanPath, cmd) return cmd } -func estimate(cmd *cobra.Command, org, planPath, output string) error { - plan, err := ioutil.ReadFile(planPath) +func estimate(cmd *cobra.Command, args []string) error { + org, err := common.GetOrg(cmd) + if err != nil { + return fmt.Errorf("unable to validate org flag: %w", err) + } + planPath, err := cmd.Flags().GetString("plan-path") + if err != nil { + return fmt.Errorf("unable to get plan path flag: %w", err) + } + output, err := cmd.Flags().GetString("output") + if err != nil { + return fmt.Errorf("unable to get output flag: %w", err) + } + plan, err := os.ReadFile(planPath) if err != nil { return fmt.Errorf("unable to read terraform plan file: %w", err) } diff --git a/config/config.go b/config/config.go index a03ee743..2bc6ee3d 100644 --- a/config/config.go +++ b/config/config.go @@ -2,7 +2,7 @@ package config import ( "fmt" - "io/ioutil" + "os" "github.com/adrg/xdg" "github.com/pkg/errors" @@ -38,7 +38,7 @@ func Read() (*Config, error) { Organizations: make(map[string]Organization), }, errors.Wrap(err, "unable to find XDG config path") } - content, err := ioutil.ReadFile(configFilePath) + content, err := os.ReadFile(configFilePath) if err != nil { // we return an empty Config in case it's the first time we try to access // the config and it does not exist yet @@ -65,5 +65,5 @@ func Write(c *Config) error { return errors.Wrap(err, "unable to find XDG config path") } - return ioutil.WriteFile(configFilePath, content, 0644) + return os.WriteFile(configFilePath, content, 0600) } diff --git a/e2e/helpers.go b/e2e/helpers.go index 8f725d49..dfd4801c 100644 --- a/e2e/helpers.go +++ b/e2e/helpers.go @@ -7,7 +7,6 @@ import ( "io" "os" "regexp" - "strings" "time" rootCmd "github.com/cycloidio/cycloid-cli/cmd" @@ -98,25 +97,6 @@ func executeCommand(args []string) (string, error) { return string(cmdOut), cmdErr } -func executeCommandWithStdin(args []string, stdin string) (string, error) { - cmd := rootCmd.NewRootCommand() - - buf := new(bytes.Buffer) - errBuf := new(bytes.Buffer) - cmd.SetOut(buf) - cmd.SetErr(errBuf) - - cmd.SetArgs(args) - cmd.SetIn(strings.NewReader(stdin)) - - cmdErr := cmd.Execute() - cmdOut, err := io.ReadAll(buf) - if err != nil { - panic(fmt.Sprintf("Unable to read command output buffer")) - } - return string(cmdOut), cmdErr -} - // AddNowTimestamp add a timestamp suffix to a string func AddNowTimestamp(txt string) string { return fmt.Sprintf(txt, NowTimestamp) diff --git a/e2e/login_test.go b/e2e/login_test.go index fc3d7cfc..cfe8f7d1 100644 --- a/e2e/login_test.go +++ b/e2e/login_test.go @@ -1,9 +1,7 @@ -//go:build e2e -// +build e2e - package e2e import ( + "os" "testing" "github.com/stretchr/testify/assert" @@ -11,7 +9,7 @@ import ( ) func TestLogin(t *testing.T) { - t.Run("SuccessOrgLogin", func(t *testing.T) { + t.Run("SuccessOrgLoginLegacy", func(t *testing.T) { cmdOut, cmdErr := executeCommand([]string{ "login", "--org", CY_TEST_ROOT_ORG, @@ -22,13 +20,16 @@ func TestLogin(t *testing.T) { assert.Equal(t, "", cmdOut) }) - t.Run("ErrorMissingRequiredFlag", func(t *testing.T) { - _, cmdErr := executeCommand([]string{ + t.Run("SuccessOrgLogin", func(t *testing.T) { + err := os.Setenv("CY_API_KEY", CY_TEST_ROOT_API_KEY) + require.Nil(t, err) + + cmdOut, cmdErr := executeCommand([]string{ "login", "--org", CY_TEST_ROOT_ORG, }) - require.NotNil(t, cmdErr) - assert.Contains(t, string(cmdErr.Error()), "required flag(s) \"api-key\" not set") + require.Nil(t, cmdErr) + assert.Equal(t, "", cmdOut) }) }