Skip to content

Commit

Permalink
Add --organization and fix list when the flag is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Aug 2, 2023
1 parent df501cc commit db95516
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
11 changes: 7 additions & 4 deletions pkg/koyeb/koyeb.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
forceASCII bool
debugFull bool
debug bool
organization string

loginCmd = &cobra.Command{
Use: "login",
Expand Down Expand Up @@ -75,7 +76,6 @@ func GetRootCommand() *cobra.Command {
return err
}
DetectUpdates()
organization := viper.GetString("organization")
return SetupCLIContext(cmd, organization)
},
}
Expand All @@ -90,11 +90,13 @@ func GetRootCommand() *cobra.Command {
rootCmd.PersistentFlags().BoolP("full", "", false, "do not truncate output")
rootCmd.PersistentFlags().String("url", "https://app.koyeb.com", "url of the api")
rootCmd.PersistentFlags().String("token", "", "API token")
rootCmd.PersistentFlags().StringVar(&organization, "organization", "", "organization ID")

// viper.BindPFlag returns an error only if the second argument is nil, which is never the case here, so we ignore the error
viper.BindPFlag("url", rootCmd.PersistentFlags().Lookup("url")) //nolint:errcheck
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")) //nolint:errcheck
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug")) //nolint:errcheck
viper.BindPFlag("url", rootCmd.PersistentFlags().Lookup("url")) //nolint:errcheck
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")) //nolint:errcheck
viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug")) //nolint:errcheck
viper.BindPFlag("organization", rootCmd.PersistentFlags().Lookup("organization")) //nolint:errcheck

rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(versionCmd)
Expand Down Expand Up @@ -252,5 +254,6 @@ func initConfig(rootCmd *cobra.Command) error {
apiurl = viper.GetString("url")
token = viper.GetString("token")
debug = viper.GetBool("debug")
organization = viper.GetString("organization")
return nil
}
13 changes: 12 additions & 1 deletion pkg/koyeb/organizations_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ func (h *OrganizationHandler) List(ctx *CLIContext, cmd *cobra.Command, args []s
}
}

// ctx.Organization is empty when the field "organization" is not set in the
// configuration file, and is not provided with the --organization flag.
currentOrganization := ctx.Organization
if currentOrganization == "" {
res, resp, err := ctx.Client.ProfileApi.GetCurrentOrganization(ctx.Context).Execute()
if err != nil {
return errors.NewCLIErrorFromAPIError("Unable to fetch the current organization", err, resp)
}
currentOrganization = *res.Organization.Id
}

full := GetBoolFlags(cmd, "full")
reply := NewListOragnizationsReply(ctx.Mapper, &koyeb.ListOrganizationMembersReply{Members: list}, full, ctx.Organization)
reply := NewListOragnizationsReply(ctx.Mapper, &koyeb.ListOrganizationMembersReply{Members: list}, full, currentOrganization)
ctx.Renderer.Render(reply)
return nil
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/koyeb/organizations_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ func (h *OrganizationHandler) Switch(ctx *CLIContext, cmd *cobra.Command, args [
return err
}
viper.Set("organization", organization)

err = viper.WriteConfig()
if err != nil {
if err := viper.WriteConfig(); err != nil {
return &errors.CLIError{
What: "Unable to switch the current organization",
Why: "we were unable to write the configuration file",
Expand Down

0 comments on commit db95516

Please sign in to comment.