|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "moco/data" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/charmbracelet/huh" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +var configCmd = &cobra.Command{ |
| 13 | + Use: "config", |
| 14 | + Short: "Add default options when running in current directory", |
| 15 | + Run: func(cmd *cobra.Command, args []string) { |
| 16 | + pid, perr := cmd.Flags().GetInt("project") |
| 17 | + tid, terr := cmd.Flags().GetInt("task") |
| 18 | + desc, derr := cmd.Flags().GetString("description") |
| 19 | + |
| 20 | + projects, err := data.GetProjects() |
| 21 | + if err != nil { |
| 22 | + fmt.Println("Could not retrieve projects", err) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + if pid == 0 || perr != nil { |
| 27 | + options := make([]huh.Option[int], len(projects)) |
| 28 | + for i, p := range projects { |
| 29 | + options[i] = huh.NewOption[int](p.Name, p.Id) |
| 30 | + } |
| 31 | + |
| 32 | + pform := huh.NewSelect[int]().Options(options...).Value(&pid) |
| 33 | + pform.Run() |
| 34 | + if pid == 0 { |
| 35 | + return |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + var project data.Project |
| 40 | + for _, p := range projects { |
| 41 | + if p.Id == pid { |
| 42 | + project = p |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if tid == 0 || terr != nil { |
| 47 | + options := make([]huh.Option[int], len(project.Tasks)) |
| 48 | + for i, t := range project.Tasks { |
| 49 | + options[i] = huh.NewOption[int](t.Name, t.Id) |
| 50 | + } |
| 51 | + tform := huh.NewSelect[int]().Options(options...).Value(&tid) |
| 52 | + tform.Run() |
| 53 | + if tid == 0 { |
| 54 | + return |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if desc == "" || derr != nil { |
| 59 | + huh.NewInput().Title("Description:").Prompt("> ").Value(&desc).Run() |
| 60 | + } |
| 61 | + |
| 62 | + os.WriteFile(".moco", []byte(fmt.Sprintf("MOCO_PROJECT_ID=%d\nMOCO_TASK_ID=%d\nMOCO_DESCRIPTION=%s", pid, tid, desc)), 0644) |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +func init() { |
| 67 | + configCmd.Flags().IntP("project", "p", 0, "Set the default project ID") |
| 68 | + configCmd.Flags().IntP("task", "t", 0, "Set the default task ID") |
| 69 | + configCmd.Flags().StringP("description", "d", "", "Set the default description") |
| 70 | + rootCmd.AddCommand(configCmd) |
| 71 | +} |
0 commit comments