|
| 1 | +package _import |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 6 | + "github.com/stackitcloud/stackit-cli/internal/pkg/config" |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + nameFlag = "name" |
| 16 | + configFlag = "config" |
| 17 | + noSetFlag = "no-set" |
| 18 | +) |
| 19 | + |
| 20 | +type inputModel struct { |
| 21 | + *globalflags.GlobalFlagModel |
| 22 | + ProfileName string |
| 23 | + Config string |
| 24 | + NoSet bool |
| 25 | +} |
| 26 | + |
| 27 | +func NewCmd(p *print.Printer) *cobra.Command { |
| 28 | + cmd := &cobra.Command{ |
| 29 | + Use: "import", |
| 30 | + Short: "Imports a CLI configuration profile", |
| 31 | + Long: "Imports a CLI configuration profile.", |
| 32 | + Example: examples.Build( |
| 33 | + examples.NewExample( |
| 34 | + `Import a config with name "PROFILE_NAME" from file "./config.json"`, |
| 35 | + "$ stackit config profile --name PROFILE_NAME --config `@./config.json`", |
| 36 | + ), |
| 37 | + examples.NewExample( |
| 38 | + `Import a config with name "PROFILE_NAME" from file "./config.json" and set not as active`, |
| 39 | + "$ stackit config profile --name PROFILE_NAME --config `@./config.json` --no-set", |
| 40 | + ), |
| 41 | + ), |
| 42 | + Args: args.NoArgs, |
| 43 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 44 | + model, err := parseInput(p, cmd) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + err = config.ImportProfile(p, model.ProfileName, model.Config, !model.NoSet) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + p.Info("Successfully imported profile %q\n", model.ProfileName) |
| 55 | + |
| 56 | + return nil |
| 57 | + }, |
| 58 | + } |
| 59 | + configureFlags(cmd) |
| 60 | + return cmd |
| 61 | +} |
| 62 | + |
| 63 | +func configureFlags(cmd *cobra.Command) { |
| 64 | + cmd.Flags().String(nameFlag, "", "Profile name") |
| 65 | + cmd.Flags().VarP(flags.ReadFromFileFlag(), configFlag, "c", "Config to be imported") |
| 66 | + cmd.Flags().Bool(noSetFlag, false, "Set the imported profile not as active") |
| 67 | + |
| 68 | + cobra.CheckErr(cmd.MarkFlagRequired(nameFlag)) |
| 69 | + cobra.CheckErr(cmd.MarkFlagRequired(configFlag)) |
| 70 | +} |
| 71 | + |
| 72 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 73 | + globalFlags := globalflags.Parse(p, cmd) |
| 74 | + |
| 75 | + model := &inputModel{ |
| 76 | + GlobalFlagModel: globalFlags, |
| 77 | + ProfileName: flags.FlagToStringValue(p, cmd, nameFlag), |
| 78 | + Config: flags.FlagToStringValue(p, cmd, configFlag), |
| 79 | + NoSet: flags.FlagToBoolValue(p, cmd, noSetFlag), |
| 80 | + } |
| 81 | + |
| 82 | + if model.Config == "" { |
| 83 | + return nil, &errors.FlagValidationError{ |
| 84 | + Flag: configFlag, |
| 85 | + Details: "must not be empty", |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if model.ProfileName == "" { |
| 90 | + return nil, &errors.FlagValidationError{ |
| 91 | + Flag: nameFlag, |
| 92 | + Details: "must not be empty", |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if p.IsVerbosityDebug() { |
| 97 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 98 | + if err != nil { |
| 99 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 100 | + } else { |
| 101 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return model, nil |
| 106 | +} |
0 commit comments