|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/loops-so/loops-go" |
| 7 | + "github.com/loops-so/cli/internal/config" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +func runComponentsGet(cfg *config.Config, id string) (*loops.Component, error) { |
| 12 | + return newAPIClient(cfg).GetComponent(id) |
| 13 | +} |
| 14 | + |
| 15 | +func runComponentsList(cfg *config.Config, params loops.PaginationParams) ([]loops.Component, error) { |
| 16 | + client := newAPIClient(cfg) |
| 17 | + if params.Cursor != "" { |
| 18 | + components, _, err := client.ListComponents(params) |
| 19 | + return components, err |
| 20 | + } |
| 21 | + return loops.Paginate(func(cursor string) ([]loops.Component, *loops.Pagination, error) { |
| 22 | + return client.ListComponents(loops.PaginationParams{ |
| 23 | + PerPage: params.PerPage, |
| 24 | + Cursor: cursor, |
| 25 | + }) |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +var componentsCmd = &cobra.Command{ |
| 30 | + Use: "components", |
| 31 | + Short: "Manage components", |
| 32 | + Hidden: true, |
| 33 | +} |
| 34 | + |
| 35 | +var componentsListCmd = &cobra.Command{ |
| 36 | + Use: "list", |
| 37 | + Short: "List components", |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + if err := validatePickFlags(cmd); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + cfg, err := loadConfig() |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + components, err := runComponentsList(cfg, paginationParams(cmd)) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + if isJSONOutput() { |
| 54 | + if components == nil { |
| 55 | + components = []loops.Component{} |
| 56 | + } |
| 57 | + return printJSON(cmd.OutOrStdout(), components) |
| 58 | + } |
| 59 | + |
| 60 | + if len(components) == 0 { |
| 61 | + fmt.Fprintln(cmd.OutOrStdout(), "No components found.") |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + headers := []string{"ID", "NAME"} |
| 66 | + rows := make([][]string, 0, len(components)) |
| 67 | + for _, c := range components { |
| 68 | + rows = append(rows, []string{c.ComponentID, c.Name}) |
| 69 | + } |
| 70 | + |
| 71 | + if isPicking(cmd) { |
| 72 | + return runPicker(headers, rows, []pickBinding{ |
| 73 | + copyColumnBinding("enter", "copy id", "component ID", rows, 0, cmd.OutOrStdout()), |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + t := newStyledTable(cmd.OutOrStdout(), headers...) |
| 78 | + for _, r := range rows { |
| 79 | + t.Row(r...) |
| 80 | + } |
| 81 | + return t.Render() |
| 82 | + }, |
| 83 | +} |
| 84 | + |
| 85 | +var componentsGetCmd = &cobra.Command{ |
| 86 | + Use: "get <id>", |
| 87 | + Short: "Get a component", |
| 88 | + Args: cobra.ExactArgs(1), |
| 89 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 90 | + cfg, err := loadConfig() |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + c, err := runComponentsGet(cfg, args[0]) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + if isJSONOutput() { |
| 101 | + return printJSON(cmd.OutOrStdout(), c) |
| 102 | + } |
| 103 | + |
| 104 | + t := newStyledTable(cmd.OutOrStdout(), "FIELD", "VALUE") |
| 105 | + t.Row("componentId", c.ComponentID) |
| 106 | + t.Row("name", c.Name) |
| 107 | + if err := t.Render(); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + fmt.Fprintln(cmd.OutOrStdout()) |
| 112 | + return renderLMX(cmd.OutOrStdout(), c.LMX) |
| 113 | + }, |
| 114 | +} |
| 115 | + |
| 116 | +func init() { |
| 117 | + addPaginationFlags(componentsListCmd) |
| 118 | + addPickFlag(componentsListCmd) |
| 119 | + componentsCmd.AddCommand(componentsListCmd) |
| 120 | + componentsCmd.AddCommand(componentsGetCmd) |
| 121 | + rootCmd.AddCommand(componentsCmd) |
| 122 | +} |
0 commit comments