Skip to content

Commit 014a65c

Browse files
committed
Merge branch 'create-config-file-from-cmd'
2 parents dd24764 + dad3cf6 commit 014a65c

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

cmd/activities.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var createCmd = &cobra.Command{
3535
if taskId == 0 && err == nil {
3636
taskId, err = strconv.Atoi(os.Getenv("MOCO_TASK_ID"))
3737
}
38+
if description == "" && err == nil {
39+
description = os.Getenv("MOCO_DESCRIPTION")
40+
}
3841

3942
// if no flags or config, prompt
4043
if projectId == 0 {

cmd/config.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

cmd/track.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ var trackCmd = &cobra.Command{
3030
err = godotenv.Load(".moco")
3131
if projectId == 0 && err == nil {
3232
projectId, err = strconv.Atoi(os.Getenv("MOCO_PROJECT_ID"))
33-
fmt.Println("read projectId", projectId)
33+
fmt.Printf("read %s\n", os.Getenv("MOCO_PROJECT_ID"))
3434
}
3535
if taskId == 0 && err == nil {
3636
taskId, err = strconv.Atoi(os.Getenv("MOCO_TASK_ID"))
37-
fmt.Println("read taskId", taskId)
37+
}
38+
if description == "" && err == nil {
39+
description = os.Getenv("MOCO_DESCRIPTION")
3840
}
3941

4042
// if no flags, prompt

0 commit comments

Comments
 (0)