|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/goccy/go-yaml" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 19 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + limitFlag = "limit" |
| 24 | +) |
| 25 | + |
| 26 | +type inputModel struct { |
| 27 | + *globalflags.GlobalFlagModel |
| 28 | + Limit *int64 |
| 29 | +} |
| 30 | + |
| 31 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "list", |
| 34 | + Short: "Lists all STACKIT public-ip ranges", |
| 35 | + Long: "Lists all STACKIT public-ip ranges.", |
| 36 | + Args: args.NoArgs, |
| 37 | + Example: examples.Build( |
| 38 | + examples.NewExample( |
| 39 | + `Lists all STACKIT public-ip ranges`, |
| 40 | + "$ stackit public-ip ranges list", |
| 41 | + ), |
| 42 | + examples.NewExample( |
| 43 | + `Lists all STACKIT public-ip ranges, piping to a tool like fzf for interactive selection`, |
| 44 | + "$ stackit public-ip ranges list -o pretty | fzf", |
| 45 | + ), |
| 46 | + examples.NewExample( |
| 47 | + `Lists up to 10 STACKIT public-ip ranges`, |
| 48 | + "$ stackit public-ip ranges list --limit 10", |
| 49 | + ), |
| 50 | + ), |
| 51 | + RunE: func(cmd *cobra.Command, _ []string) error { |
| 52 | + ctx := context.Background() |
| 53 | + model, err := parseInput(params.Printer, cmd) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + |
| 58 | + // Configure API client |
| 59 | + apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // Call API |
| 65 | + req := apiClient.ListPublicIPRanges(ctx) |
| 66 | + resp, err := req.Execute() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("list public IP ranges: %w", err) |
| 69 | + } |
| 70 | + publicIpRanges := utils.GetSliceFromPointer(resp.Items) |
| 71 | + |
| 72 | + // Truncate output |
| 73 | + if model.Limit != nil && len(publicIpRanges) > int(*model.Limit) { |
| 74 | + publicIpRanges = publicIpRanges[:*model.Limit] |
| 75 | + } |
| 76 | + |
| 77 | + return outputResult(params.Printer, model.OutputFormat, publicIpRanges) |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + configureFlags(cmd) |
| 82 | + return cmd |
| 83 | +} |
| 84 | + |
| 85 | +func configureFlags(cmd *cobra.Command) { |
| 86 | + cmd.Flags().Int64(limitFlag, 0, "Maximum number of entries to list") |
| 87 | +} |
| 88 | + |
| 89 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 90 | + globalFlags := globalflags.Parse(p, cmd) |
| 91 | + |
| 92 | + limit := flags.FlagToInt64Pointer(p, cmd, limitFlag) |
| 93 | + if limit != nil && *limit < 1 { |
| 94 | + return nil, &errors.FlagValidationError{ |
| 95 | + Flag: limitFlag, |
| 96 | + Details: "must be greater than 0", |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + model := inputModel{ |
| 101 | + GlobalFlagModel: globalFlags, |
| 102 | + Limit: limit, |
| 103 | + } |
| 104 | + |
| 105 | + if p.IsVerbosityDebug() { |
| 106 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 107 | + if err != nil { |
| 108 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 109 | + } else { |
| 110 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return &model, nil |
| 115 | +} |
| 116 | + |
| 117 | +func outputResult(p *print.Printer, outputFormat string, publicIpRanges []iaas.PublicNetwork) error { |
| 118 | + switch outputFormat { |
| 119 | + case print.JSONOutputFormat: |
| 120 | + details, err := json.MarshalIndent(publicIpRanges, "", " ") |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("marshal public IP ranges: %w", err) |
| 123 | + } |
| 124 | + p.Outputln(string(details)) |
| 125 | + |
| 126 | + return nil |
| 127 | + case print.YAMLOutputFormat: |
| 128 | + details, err := yaml.MarshalWithOptions(publicIpRanges, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 129 | + if err != nil { |
| 130 | + return fmt.Errorf("marshal public IP ranges: %w", err) |
| 131 | + } |
| 132 | + p.Outputln(string(details)) |
| 133 | + |
| 134 | + return nil |
| 135 | + default: |
| 136 | + if len(publicIpRanges) == 0 { |
| 137 | + p.Outputln("No public IP ranges found") |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + for _, item := range publicIpRanges { |
| 142 | + if item.Cidr != nil && *item.Cidr != "" { |
| 143 | + p.Outputln(*item.Cidr) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | + } |
| 149 | +} |
0 commit comments