|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/codefresh-io/cli-v2/pkg/log" |
| 9 | + "github.com/codefresh-io/cli-v2/pkg/store" |
| 10 | + "github.com/codefresh-io/cli-v2/pkg/util" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +func NewConfigCommand() *cobra.Command { |
| 16 | + cmd := &cobra.Command{ |
| 17 | + Use: "config", |
| 18 | + Short: "Manage Codefresh authentication contexts", |
| 19 | + PersistentPreRunE: cfConfig.Load, |
| 20 | + Long: util.Doc(`By default, Codefresh authentication contexts are persisted at $HOME/.cfconfig. |
| 21 | +You can create, delete and list authentication contexts using the following |
| 22 | +commands, respectively: |
| 23 | +
|
| 24 | + <BIN> config create-context <NAME> --api-key <key> |
| 25 | +
|
| 26 | + <BIN> config delete-context <NAME> |
| 27 | +
|
| 28 | + <BIN> config get-contexts |
| 29 | +`), |
| 30 | + Run: func(cmd *cobra.Command, args []string) { |
| 31 | + cmd.HelpFunc()(cmd, args) |
| 32 | + exit(1) |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + cmd.AddCommand(NewConfigGetContextsCommand()) |
| 37 | + cmd.AddCommand(NewConfigCurrentContextCommand()) |
| 38 | + cmd.AddCommand(NewConfigUseContextCommand()) |
| 39 | + cmd.AddCommand(NewConfigCreateContextCommand()) |
| 40 | + cmd.AddCommand(NewConfigDeleteContextCommand()) |
| 41 | + |
| 42 | + return cmd |
| 43 | +} |
| 44 | + |
| 45 | +func NewConfigCreateContextCommand() *cobra.Command { |
| 46 | + var ( |
| 47 | + apiKey string |
| 48 | + url string |
| 49 | + ) |
| 50 | + |
| 51 | + cmd := &cobra.Command{ |
| 52 | + Use: "create-context", |
| 53 | + Short: "Create a new Codefresh authentication context", |
| 54 | + Example: util.Doc(` |
| 55 | +# Create a new context named 'test': |
| 56 | +
|
| 57 | + <BIN> config create-context test --api-key TOKEN`), |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + if len(args) < 1 { |
| 60 | + return fmt.Errorf("must provide context name to use") |
| 61 | + } |
| 62 | + return RunConfigCreateContext(cmd.Context(), args[0], apiKey, url) |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + cmd.Flags().StringVar(&apiKey, "api-key", "", "API key") |
| 67 | + cmd.Flags().StringVar(&url, "url", store.Get().DefaultAPI, "Codefresh system custom url ") |
| 68 | + die(cmd.MarkFlagRequired("api-key")) |
| 69 | + |
| 70 | + return cmd |
| 71 | +} |
| 72 | + |
| 73 | +func RunConfigCreateContext(ctx context.Context, context, apiKey, url string) error { |
| 74 | + if err := cfConfig.CreateContext(ctx, context, apiKey, url); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + log.G().Infof("create new context: %s", context) |
| 78 | + return RunConfigUseContext(ctx, context) |
| 79 | +} |
| 80 | + |
| 81 | +func NewConfigGetContextsCommand() *cobra.Command { |
| 82 | + cmd := &cobra.Command{ |
| 83 | + Use: "get-contexts", |
| 84 | + Aliases: []string{"view"}, |
| 85 | + Short: "Lists all Codefresh authentication contexts", |
| 86 | + Example: util.Doc(` |
| 87 | +# List all authentication contexts: |
| 88 | +
|
| 89 | + <BIN> config get-contexts`), |
| 90 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 91 | + return RunConfigGetContexts(cmd.Context()) |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + return cmd |
| 96 | +} |
| 97 | + |
| 98 | +func RunConfigGetContexts(ctx context.Context) error { |
| 99 | + return cfConfig.Write(ctx, os.Stdout) |
| 100 | +} |
| 101 | + |
| 102 | +func NewConfigCurrentContextCommand() *cobra.Command { |
| 103 | + cmd := &cobra.Command{ |
| 104 | + Use: "current-context", |
| 105 | + Short: "Shows the currently selected Codefresh authentication context", |
| 106 | + Example: util.Doc(` |
| 107 | +# Shows the current context: |
| 108 | +
|
| 109 | + <BIN> config current-context`), |
| 110 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 111 | + return RunConfigCurrentContext(cmd.Context()) |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + return cmd |
| 116 | +} |
| 117 | + |
| 118 | +func RunConfigCurrentContext(ctx context.Context) error { |
| 119 | + cur := cfConfig.GetCurrentContext() |
| 120 | + if cur.Name == "" { |
| 121 | + log.G().Fatal(util.Doc("no currently selected context, use '<BIN> config use-context' to select a context")) |
| 122 | + } |
| 123 | + log.G().Info(cur.Name) |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func NewConfigUseContextCommand() *cobra.Command { |
| 128 | + cmd := &cobra.Command{ |
| 129 | + Use: "use-context CONTEXT", |
| 130 | + Short: "Switch the current authentication context", |
| 131 | + Example: util.Doc(` |
| 132 | +# Switch to another authentication context: |
| 133 | +
|
| 134 | + <BIN> config use-context test`), |
| 135 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 136 | + if len(args) < 1 { |
| 137 | + return fmt.Errorf("must provide context name to use") |
| 138 | + } |
| 139 | + return RunConfigUseContext(cmd.Context(), args[0]) |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + return cmd |
| 144 | +} |
| 145 | + |
| 146 | +func RunConfigUseContext(ctx context.Context, context string) error { |
| 147 | + if err := cfConfig.UseContext(ctx, context); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + log.G().Infof("switched to context: %s", context) |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func NewConfigDeleteContextCommand() *cobra.Command { |
| 155 | + cmd := &cobra.Command{ |
| 156 | + Use: "delete-context CONTEXT", |
| 157 | + Short: "Delete the specified authentication context", |
| 158 | + Example: util.Doc(` |
| 159 | +# Deleting an authentication context name 'test': |
| 160 | +
|
| 161 | + <BIN> config delete-context test`), |
| 162 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 163 | + if len(args) < 1 { |
| 164 | + return fmt.Errorf("must provide context name to use") |
| 165 | + } |
| 166 | + return RunConfigDeleteContext(cmd.Context(), args[0]) |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + return cmd |
| 171 | +} |
| 172 | + |
| 173 | +func RunConfigDeleteContext(ctx context.Context, context string) error { |
| 174 | + if err := cfConfig.DeleteContext(context); err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + log.G().Infof("delete context: %s", context) |
| 178 | + return nil |
| 179 | +} |
0 commit comments