|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/loops-so/cli/internal/config" |
| 8 | + "github.com/loops-so/loops-go" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +func runThemesGet(cfg *config.Config, id string) (*loops.Theme, error) { |
| 13 | + return newAPIClient(cfg).GetTheme(id) |
| 14 | +} |
| 15 | + |
| 16 | +func runThemesList(cfg *config.Config, params loops.PaginationParams) ([]loops.Theme, error) { |
| 17 | + client := newAPIClient(cfg) |
| 18 | + if params.Cursor != "" { |
| 19 | + themes, _, err := client.ListThemes(params) |
| 20 | + return themes, err |
| 21 | + } |
| 22 | + return loops.Paginate(func(cursor string) ([]loops.Theme, *loops.Pagination, error) { |
| 23 | + return client.ListThemes(loops.PaginationParams{ |
| 24 | + PerPage: params.PerPage, |
| 25 | + Cursor: cursor, |
| 26 | + }) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +var themesCmd = &cobra.Command{ |
| 31 | + Use: "themes", |
| 32 | + Short: "Manage themes", |
| 33 | + Hidden: true, |
| 34 | +} |
| 35 | + |
| 36 | +var themesListCmd = &cobra.Command{ |
| 37 | + Use: "list", |
| 38 | + Short: "List themes", |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + if err := validatePickFlags(cmd); err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + cfg, err := loadConfig() |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + themes, err := runThemesList(cfg, paginationParams(cmd)) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + if isJSONOutput() { |
| 55 | + if themes == nil { |
| 56 | + themes = []loops.Theme{} |
| 57 | + } |
| 58 | + return printJSON(cmd.OutOrStdout(), themes) |
| 59 | + } |
| 60 | + |
| 61 | + if len(themes) == 0 { |
| 62 | + fmt.Fprintln(cmd.OutOrStdout(), "No themes found.") |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + headers := []string{"ID", "NAME", "DEFAULT", "UPDATED"} |
| 67 | + rows := make([][]string, 0, len(themes)) |
| 68 | + for _, th := range themes { |
| 69 | + rows = append(rows, []string{ |
| 70 | + th.ThemeID, |
| 71 | + th.Name, |
| 72 | + strconv.FormatBool(th.IsDefault), |
| 73 | + th.UpdatedAt, |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + if isPicking(cmd) { |
| 78 | + return runPicker(headers, rows, []pickBinding{ |
| 79 | + copyColumnBinding("enter", "copy id", "theme ID", rows, 0, cmd.OutOrStdout()), |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + t := newStyledTable(cmd.OutOrStdout(), headers...) |
| 84 | + for _, r := range rows { |
| 85 | + t.Row(r...) |
| 86 | + } |
| 87 | + return t.Render() |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +var themesGetCmd = &cobra.Command{ |
| 92 | + Use: "get <id>", |
| 93 | + Short: "Get a theme", |
| 94 | + Args: cobra.ExactArgs(1), |
| 95 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 96 | + cfg, err := loadConfig() |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + th, err := runThemesGet(cfg, args[0]) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + if isJSONOutput() { |
| 107 | + return printJSON(cmd.OutOrStdout(), th) |
| 108 | + } |
| 109 | + |
| 110 | + t := newStyledTable(cmd.OutOrStdout(), "FIELD", "VALUE") |
| 111 | + t.Row("themeId", th.ThemeID) |
| 112 | + t.Row("name", th.Name) |
| 113 | + t.Row("isDefault", strconv.FormatBool(th.IsDefault)) |
| 114 | + t.Row("createdAt", th.CreatedAt) |
| 115 | + t.Row("updatedAt", th.UpdatedAt) |
| 116 | + if err := t.Render(); err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + fmt.Fprintln(cmd.OutOrStdout()) |
| 121 | + return printThemeStyles(cmd, th.Styles) |
| 122 | + }, |
| 123 | +} |
| 124 | + |
| 125 | +func printThemeStyles(cmd *cobra.Command, s loops.ThemeStyles) error { |
| 126 | + t := newStyledTable(cmd.OutOrStdout(), "STYLE", "VALUE") |
| 127 | + for _, row := range themeStyleRows(s) { |
| 128 | + t.Row(row[0], dashIfEmpty(row[1])) |
| 129 | + } |
| 130 | + return t.Render() |
| 131 | +} |
| 132 | + |
| 133 | +func dashIfEmpty(s string) string { |
| 134 | + if s == "" { |
| 135 | + return "-" |
| 136 | + } |
| 137 | + return s |
| 138 | +} |
| 139 | + |
| 140 | +func themeStyleRows(s loops.ThemeStyles) [][2]string { |
| 141 | + return [][2]string{ |
| 142 | + {"backgroundColor", s.BackgroundColor}, |
| 143 | + {"backgroundXPadding", formatFloat(s.BackgroundXPadding)}, |
| 144 | + {"backgroundYPadding", formatFloat(s.BackgroundYPadding)}, |
| 145 | + {"bodyColor", s.BodyColor}, |
| 146 | + {"bodyXPadding", formatFloat(s.BodyXPadding)}, |
| 147 | + {"bodyYPadding", formatFloat(s.BodyYPadding)}, |
| 148 | + {"bodyFontFamily", s.BodyFontFamily}, |
| 149 | + {"bodyFontCategory", s.BodyFontCategory}, |
| 150 | + {"borderColor", s.BorderColor}, |
| 151 | + {"borderWidth", formatFloat(s.BorderWidth)}, |
| 152 | + {"borderRadius", formatFloat(s.BorderRadius)}, |
| 153 | + {"buttonBodyColor", s.ButtonBodyColor}, |
| 154 | + {"buttonBodyXPadding", formatFloat(s.ButtonBodyXPadding)}, |
| 155 | + {"buttonBodyYPadding", formatFloat(s.ButtonBodyYPadding)}, |
| 156 | + {"buttonBorderColor", s.ButtonBorderColor}, |
| 157 | + {"buttonBorderWidth", formatFloat(s.ButtonBorderWidth)}, |
| 158 | + {"buttonBorderRadius", formatFloat(s.ButtonBorderRadius)}, |
| 159 | + {"buttonTextColor", s.ButtonTextColor}, |
| 160 | + {"buttonTextFormat", formatFloat(s.ButtonTextFormat)}, |
| 161 | + {"buttonTextFontSize", formatFloat(s.ButtonTextFontSize)}, |
| 162 | + {"dividerColor", s.DividerColor}, |
| 163 | + {"dividerBorderWidth", formatFloat(s.DividerBorderWidth)}, |
| 164 | + {"textBaseColor", s.TextBaseColor}, |
| 165 | + {"textBaseFontSize", formatFloat(s.TextBaseFontSize)}, |
| 166 | + {"textBaseLineHeight", formatFloat(s.TextBaseLineHeight)}, |
| 167 | + {"textBaseLetterSpacing", formatFloat(s.TextBaseLetterSpacing)}, |
| 168 | + {"textLinkColor", s.TextLinkColor}, |
| 169 | + {"heading1Color", s.Heading1Color}, |
| 170 | + {"heading1FontSize", formatFloat(s.Heading1FontSize)}, |
| 171 | + {"heading1LineHeight", formatFloat(s.Heading1LineHeight)}, |
| 172 | + {"heading1LetterSpacing", formatFloat(s.Heading1LetterSpacing)}, |
| 173 | + {"heading2Color", s.Heading2Color}, |
| 174 | + {"heading2FontSize", formatFloat(s.Heading2FontSize)}, |
| 175 | + {"heading2LineHeight", formatFloat(s.Heading2LineHeight)}, |
| 176 | + {"heading2LetterSpacing", formatFloat(s.Heading2LetterSpacing)}, |
| 177 | + {"heading3Color", s.Heading3Color}, |
| 178 | + {"heading3FontSize", formatFloat(s.Heading3FontSize)}, |
| 179 | + {"heading3LineHeight", formatFloat(s.Heading3LineHeight)}, |
| 180 | + {"heading3LetterSpacing", formatFloat(s.Heading3LetterSpacing)}, |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func formatFloat(f float64) string { |
| 185 | + return strconv.FormatFloat(f, 'f', -1, 64) |
| 186 | +} |
| 187 | + |
| 188 | +func init() { |
| 189 | + addPaginationFlags(themesListCmd) |
| 190 | + addPickFlag(themesListCmd) |
| 191 | + themesCmd.AddCommand(themesListCmd) |
| 192 | + themesCmd.AddCommand(themesGetCmd) |
| 193 | + rootCmd.AddCommand(themesCmd) |
| 194 | +} |
0 commit comments