Skip to content

Commit

Permalink
Merge branch 'create-config-file-from-cmd'
Browse files Browse the repository at this point in the history
  • Loading branch information
prmaloney committed Jan 25, 2024
2 parents dd24764 + dad3cf6 commit 014a65c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cmd/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var createCmd = &cobra.Command{
if taskId == 0 && err == nil {
taskId, err = strconv.Atoi(os.Getenv("MOCO_TASK_ID"))
}
if description == "" && err == nil {
description = os.Getenv("MOCO_DESCRIPTION")
}

// if no flags or config, prompt
if projectId == 0 {
Expand Down
71 changes: 71 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"fmt"
"moco/data"
"os"

"github.com/charmbracelet/huh"
"github.com/spf13/cobra"
)

var configCmd = &cobra.Command{
Use: "config",
Short: "Add default options when running in current directory",
Run: func(cmd *cobra.Command, args []string) {
pid, perr := cmd.Flags().GetInt("project")
tid, terr := cmd.Flags().GetInt("task")
desc, derr := cmd.Flags().GetString("description")

projects, err := data.GetProjects()
if err != nil {
fmt.Println("Could not retrieve projects", err)
return
}

if pid == 0 || perr != nil {
options := make([]huh.Option[int], len(projects))
for i, p := range projects {
options[i] = huh.NewOption[int](p.Name, p.Id)
}

pform := huh.NewSelect[int]().Options(options...).Value(&pid)
pform.Run()
if pid == 0 {
return
}
}

var project data.Project
for _, p := range projects {
if p.Id == pid {
project = p
}
}

if tid == 0 || terr != nil {
options := make([]huh.Option[int], len(project.Tasks))
for i, t := range project.Tasks {
options[i] = huh.NewOption[int](t.Name, t.Id)
}
tform := huh.NewSelect[int]().Options(options...).Value(&tid)
tform.Run()
if tid == 0 {
return
}
}

if desc == "" || derr != nil {
huh.NewInput().Title("Description:").Prompt("> ").Value(&desc).Run()
}

os.WriteFile(".moco", []byte(fmt.Sprintf("MOCO_PROJECT_ID=%d\nMOCO_TASK_ID=%d\nMOCO_DESCRIPTION=%s", pid, tid, desc)), 0644)
},
}

func init() {
configCmd.Flags().IntP("project", "p", 0, "Set the default project ID")
configCmd.Flags().IntP("task", "t", 0, "Set the default task ID")
configCmd.Flags().StringP("description", "d", "", "Set the default description")
rootCmd.AddCommand(configCmd)
}
6 changes: 4 additions & 2 deletions cmd/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ var trackCmd = &cobra.Command{
err = godotenv.Load(".moco")
if projectId == 0 && err == nil {
projectId, err = strconv.Atoi(os.Getenv("MOCO_PROJECT_ID"))
fmt.Println("read projectId", projectId)
fmt.Printf("read %s\n", os.Getenv("MOCO_PROJECT_ID"))
}
if taskId == 0 && err == nil {
taskId, err = strconv.Atoi(os.Getenv("MOCO_TASK_ID"))
fmt.Println("read taskId", taskId)
}
if description == "" && err == nil {
description = os.Getenv("MOCO_DESCRIPTION")
}

// if no flags, prompt
Expand Down

0 comments on commit 014a65c

Please sign in to comment.