|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/quantcli/liftoff-export-cli/internal/client" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// Preset mirrors a Liftoff fitnessService preset (what the app calls a saved |
| 15 | +// workout template; users call them "routines"). The field names match the |
| 16 | +// upstream JSON so `--format json | jq` reads naturally against API docs. |
| 17 | +type Preset struct { |
| 18 | + ID string `json:"id"` |
| 19 | + CreatedAt string `json:"createdAt"` |
| 20 | + UserID string `json:"userId"` |
| 21 | + Name string `json:"name"` |
| 22 | + Image *string `json:"image"` |
| 23 | + MarketPresetID *string `json:"marketPresetId"` |
| 24 | + BookmarkID string `json:"bookmarkId"` |
| 25 | + Completed int `json:"completed"` |
| 26 | + AvgDuration int `json:"avgDuration"` |
| 27 | + IsFavorite bool `json:"isFavorite"` |
| 28 | + FolderID *string `json:"folderId"` |
| 29 | + ExerciseData []PresetExerciseData `json:"exerciseData"` |
| 30 | +} |
| 31 | + |
| 32 | +// PresetExerciseData mirrors an entry in a preset's exerciseData array. |
| 33 | +// Shape parallels workouts.ExerciseData, with the additional preset linkage |
| 34 | +// fields (presetCuid, exerciseDataId on sets). |
| 35 | +type PresetExerciseData struct { |
| 36 | + ID string `json:"id"` |
| 37 | + PresetCUID string `json:"presetCuid"` |
| 38 | + ExerciseIndex int `json:"exerciseIndex"` |
| 39 | + ExerciseName string `json:"exerciseName"` |
| 40 | + ExerciseID string `json:"exerciseId"` |
| 41 | + ExerciseTypes string `json:"exerciseTypes"` |
| 42 | + Superset *string `json:"superset"` |
| 43 | + OverrideWeightUnit *string `json:"overrideWeightUnit"` |
| 44 | + ExerciseCUID *string `json:"exerciseCuid"` |
| 45 | + MarketExerciseCUID *string `json:"marketExerciseCuid"` |
| 46 | + ExerciseNotes *string `json:"exerciseNotes"` |
| 47 | + SetsData []PresetSetData `json:"setsData"` |
| 48 | +} |
| 49 | + |
| 50 | +type PresetSetData struct { |
| 51 | + ID string `json:"id"` |
| 52 | + ExerciseDataID string `json:"exerciseDataId"` |
| 53 | + SetIndex int `json:"setIndex"` |
| 54 | + SetType string `json:"setType"` |
| 55 | + InputOne json.Number `json:"inputOne"` |
| 56 | + InputTwo json.Number `json:"inputTwo"` |
| 57 | +} |
| 58 | + |
| 59 | +// Folder is a Liftoff routine folder — a labeled group of presets in the |
| 60 | +// app's Routines tab. The nested Presets array contains the full Preset |
| 61 | +// objects (each with folderId pointing back to this folder). |
| 62 | +type Folder struct { |
| 63 | + ID string `json:"id"` |
| 64 | + CreatedAt string `json:"createdAt"` |
| 65 | + UserID string `json:"userId"` |
| 66 | + Name string `json:"name"` |
| 67 | + PresetsOrder []string `json:"presetsOrder"` |
| 68 | + Presets []Preset `json:"presets"` |
| 69 | +} |
| 70 | + |
| 71 | +// presetsResponse is the top-level shape of fitnessService.fetchUserPresetsWithFolders. |
| 72 | +// PresetsWithoutFolder are what the Liftoff app surfaces under "My Routines" |
| 73 | +// (the implicit ungrouped section); folder Presets are surfaced under their |
| 74 | +// folder name. Both rendering paths share the same Preset shape. |
| 75 | +type presetsResponse struct { |
| 76 | + Folders []Folder `json:"folders"` |
| 77 | + PresetsWithoutFolder []Preset `json:"presetsWithoutFolder"` |
| 78 | +} |
| 79 | + |
| 80 | +// unfiledLabel is the section title we use for PresetsWithoutFolder in |
| 81 | +// markdown output. Matches the Liftoff app's UI label so a user comparing |
| 82 | +// terminal output to the app sees the same heading. |
| 83 | +const unfiledLabel = "My Routines" |
| 84 | + |
| 85 | +var routinesCmd = &cobra.Command{ |
| 86 | + Use: "routines", |
| 87 | + Short: "Saved workout routine (preset) commands", |
| 88 | + Long: `Routines are reusable workout templates saved in the Liftoff app. |
| 89 | +The upstream API calls them "presets"; the JSON output preserves that |
| 90 | +naming. The fitdown markdown renderer treats a routine the same way it |
| 91 | +treats a logged workout.`, |
| 92 | +} |
| 93 | + |
| 94 | +var routinesListFormatFlag string |
| 95 | + |
| 96 | +var routinesListCmd = &cobra.Command{ |
| 97 | + Use: "list", |
| 98 | + Short: "List all your saved routines", |
| 99 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 100 | + format, err := validateFormat(routinesListFormatFlag) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + resp, err := fetchPresets() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + if format == "json" { |
| 109 | + return printJSON(resp) |
| 110 | + } |
| 111 | + return renderRoutinesFitdown(os.Stdout, resp) |
| 112 | + }, |
| 113 | +} |
| 114 | + |
| 115 | +var routinesShowFormatFlag string |
| 116 | + |
| 117 | +var routinesShowCmd = &cobra.Command{ |
| 118 | + Use: "show <name-or-id>", |
| 119 | + Short: "Show one routine by name (case-insensitive exact match) or by id", |
| 120 | + Args: cobra.ExactArgs(1), |
| 121 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 122 | + format, err := validateFormat(routinesShowFormatFlag) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + resp, err := fetchPresets() |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + match, err := pickPreset(resp, args[0]) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + if format == "json" { |
| 135 | + return printJSON([]Preset{*match}) |
| 136 | + } |
| 137 | + // `show` emits a single routine without any section heading — |
| 138 | + // the routine name is enough context for a one-off lookup. |
| 139 | + return renderOnePreset(os.Stdout, *match, "#") |
| 140 | + }, |
| 141 | +} |
| 142 | + |
| 143 | +func fetchPresets() (*presetsResponse, error) { |
| 144 | + c := client.New() |
| 145 | + var resp presetsResponse |
| 146 | + if err := c.Query("fitnessService.fetchUserPresetsWithFolders", nil, &resp); err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + // Normalize nil → empty slice so --format json emits `[]` not `null`, |
| 150 | + // matching what the upstream API does on an empty account. |
| 151 | + if resp.Folders == nil { |
| 152 | + resp.Folders = []Folder{} |
| 153 | + } |
| 154 | + if resp.PresetsWithoutFolder == nil { |
| 155 | + resp.PresetsWithoutFolder = []Preset{} |
| 156 | + } |
| 157 | + return &resp, nil |
| 158 | +} |
| 159 | + |
| 160 | +// allPresets flattens both unfiled and foldered presets into one slice for |
| 161 | +// lookup. Used by pickPreset so `routines show "Valley Creek 1"` finds a |
| 162 | +// routine regardless of whether it lives in a folder. |
| 163 | +func allPresets(resp *presetsResponse) []Preset { |
| 164 | + out := make([]Preset, 0, len(resp.PresetsWithoutFolder)) |
| 165 | + out = append(out, resp.PresetsWithoutFolder...) |
| 166 | + for _, f := range resp.Folders { |
| 167 | + out = append(out, f.Presets...) |
| 168 | + } |
| 169 | + return out |
| 170 | +} |
| 171 | + |
| 172 | +// pickPreset resolves a `show` argument to exactly one preset across the |
| 173 | +// full account (unfiled + foldered). Match order: (1) case-insensitive |
| 174 | +// exact name match, (2) exact id match. Multiple name-matches return an |
| 175 | +// error so the caller can disambiguate by id. |
| 176 | +func pickPreset(resp *presetsResponse, arg string) (*Preset, error) { |
| 177 | + presets := allPresets(resp) |
| 178 | + target := strings.ToLower(strings.TrimSpace(arg)) |
| 179 | + var nameHits []Preset |
| 180 | + for _, p := range presets { |
| 181 | + if strings.ToLower(p.Name) == target { |
| 182 | + nameHits = append(nameHits, p) |
| 183 | + } |
| 184 | + } |
| 185 | + if len(nameHits) == 1 { |
| 186 | + return &nameHits[0], nil |
| 187 | + } |
| 188 | + if len(nameHits) > 1 { |
| 189 | + ids := make([]string, 0, len(nameHits)) |
| 190 | + for _, p := range nameHits { |
| 191 | + ids = append(ids, p.ID) |
| 192 | + } |
| 193 | + return nil, fmt.Errorf("multiple routines named %q — disambiguate by id: %s", arg, strings.Join(ids, ", ")) |
| 194 | + } |
| 195 | + for i := range presets { |
| 196 | + if presets[i].ID == arg { |
| 197 | + return &presets[i], nil |
| 198 | + } |
| 199 | + } |
| 200 | + return nil, fmt.Errorf("no routine matches %q", arg) |
| 201 | +} |
| 202 | + |
| 203 | +// renderRoutinesFitdown renders the full account: a "# My Routines" H1 with |
| 204 | +// each unfiled preset as an H2 underneath, then "# <FolderName>" H1 per |
| 205 | +// folder with its foldered presets as H2. "---" rules separate sibling |
| 206 | +// routines and section boundaries. Favorite routines are marked with a |
| 207 | +// star; per-exercise notes are appended in parens so cues like "Left Only" |
| 208 | +// aren't rendered as a markdown heading by mistake. |
| 209 | +func renderRoutinesFitdown(w io.Writer, resp *presetsResponse) error { |
| 210 | + first := true |
| 211 | + emitSection := func(heading string, presets []Preset) { |
| 212 | + if len(presets) == 0 { |
| 213 | + return |
| 214 | + } |
| 215 | + if !first { |
| 216 | + fmt.Fprintln(w) |
| 217 | + fmt.Fprintln(w, "---") |
| 218 | + fmt.Fprintln(w) |
| 219 | + } |
| 220 | + first = false |
| 221 | + fmt.Fprintf(w, "# %s\n", heading) |
| 222 | + for i, p := range presets { |
| 223 | + if i > 0 { |
| 224 | + fmt.Fprintln(w) |
| 225 | + fmt.Fprintln(w, "---") |
| 226 | + } |
| 227 | + fmt.Fprintln(w) |
| 228 | + renderOnePreset(w, p, "##") |
| 229 | + } |
| 230 | + } |
| 231 | + emitSection(unfiledLabel, resp.PresetsWithoutFolder) |
| 232 | + for _, f := range resp.Folders { |
| 233 | + emitSection(f.Name, f.Presets) |
| 234 | + } |
| 235 | + return nil |
| 236 | +} |
| 237 | + |
| 238 | +// renderOnePreset prints a single preset under the given heading marker |
| 239 | +// (e.g. "##" inside a folder section, "#" for `routines show` where the |
| 240 | +// routine has no enclosing section). Body format (exercises, set lines, |
| 241 | +// note parens) is identical regardless of caller. |
| 242 | +func renderOnePreset(w io.Writer, p Preset, headingMarker string) error { |
| 243 | + star := "" |
| 244 | + if p.IsFavorite { |
| 245 | + star = " ★" |
| 246 | + } |
| 247 | + fmt.Fprintf(w, "%s Routine: %s%s\n", headingMarker, p.Name, star) |
| 248 | + for _, ex := range p.ExerciseData { |
| 249 | + fmt.Fprintln(w) |
| 250 | + name := ex.ExerciseName |
| 251 | + if ex.ExerciseNotes != nil && *ex.ExerciseNotes != "" { |
| 252 | + name = fmt.Sprintf("%s (%s)", name, *ex.ExerciseNotes) |
| 253 | + } |
| 254 | + fmt.Fprintln(w, name) |
| 255 | + var lines []string |
| 256 | + for _, s := range ex.SetsData { |
| 257 | + lines = append(lines, fitdownSetLine(ex.ExerciseTypes, s)) |
| 258 | + } |
| 259 | + // Compress consecutive identical lines into Nx... notation. |
| 260 | + for i := 0; i < len(lines); { |
| 261 | + j := i + 1 |
| 262 | + for j < len(lines) && lines[j] == lines[i] { |
| 263 | + j++ |
| 264 | + } |
| 265 | + if n := j - i; n > 1 { |
| 266 | + fmt.Fprintf(w, "%dx%s\n", n, lines[i]) |
| 267 | + } else { |
| 268 | + fmt.Fprintln(w, lines[i]) |
| 269 | + } |
| 270 | + i = j |
| 271 | + } |
| 272 | + } |
| 273 | + return nil |
| 274 | +} |
| 275 | + |
| 276 | +// fitdownSetLine is split out of workouts.printFitdown so routines can render |
| 277 | +// the same notation without duplicating the type switch. Keeping it here |
| 278 | +// rather than in workouts.go keeps the workouts file untouched, at the cost |
| 279 | +// of a tiny bit of duplication of the WR/BR/AB/WD/DD/ND mapping. |
| 280 | +func fitdownSetLine(exTypes string, s PresetSetData) string { |
| 281 | + switch exTypes { |
| 282 | + case "WR": |
| 283 | + return fmt.Sprintf("%s@%s", s.InputTwo, s.InputOne) |
| 284 | + case "AB": |
| 285 | + return fmt.Sprintf("%s@-%s", s.InputTwo, s.InputOne) |
| 286 | + case "BR": |
| 287 | + return fmt.Sprintf("%s@+%s", s.InputTwo, s.InputOne) |
| 288 | + case "WD": |
| 289 | + km, _ := s.InputTwo.Float64() |
| 290 | + return fmt.Sprintf("%slb %.3fmi", s.InputOne, km/1.60934) |
| 291 | + case "DD": |
| 292 | + secs, _ := s.InputTwo.Int64() |
| 293 | + km, _ := s.InputOne.Float64() |
| 294 | + return fmt.Sprintf("%.2fmi %d:%02d", km/1.60934, secs/60, secs%60) |
| 295 | + case "ND": |
| 296 | + secs, _ := s.InputTwo.Int64() |
| 297 | + return fmt.Sprintf("%d:%02d", secs/60, secs%60) |
| 298 | + default: |
| 299 | + return fmt.Sprintf("[%s] %s %s", exTypes, s.InputOne, s.InputTwo) |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | +func init() { |
| 304 | + routinesCmd.AddCommand(routinesListCmd) |
| 305 | + routinesCmd.AddCommand(routinesShowCmd) |
| 306 | + routinesListCmd.Flags().StringVar(&routinesListFormatFlag, "format", "markdown", |
| 307 | + "Output format: markdown (default, fitdown-style) or json") |
| 308 | + routinesShowCmd.Flags().StringVar(&routinesShowFormatFlag, "format", "markdown", |
| 309 | + "Output format: markdown (default, fitdown-style) or json") |
| 310 | +} |
0 commit comments