gunk-dev/the-valley

untrack.go

source60ad80d5a5a5main's published tip history
2.8 KiB
1package cmd
2
3import (
4 "fmt"
5 "os"
6 "strings"
7
8 "the-valley/flow/internal/run"
9 "github.com/spf13/cobra"
10)
11
12var untrackCmd = &cobra.Command{
13 Use: "untrack <ref> [<ref>...]",
14 Short: "Stop tracking PRs or valley changes on the dashboard",
15 Long: `Removes tracked PRs or valley changes from the klaus dashboard and klaus
16status. Only removes what was added via 'klaus track' — agent-created runs are
17never removed.
18
19A PR is named by its number. A valley change is named by its handle, its
20full change ID, or <project>:<handle>; --repo names the project when the ref
21doesn't. A change ref that names tracked changes in more than one project
22removes none; name the project.
23
24Examples:
25 klaus untrack 405
26 klaus untrack vpkz
27 klaus untrack the-valley:vpkz
28 klaus untrack --repo the-valley vpkz`,
29 Args: cobra.MinimumNArgs(1),
30 RunE: func(cmd *cobra.Command, args []string) error {
31 repoFlag, _ := cmd.Flags().GetString("repo")
32 store, err := sessionStore()
33 if err != nil {
34 return err
35 }
36
37 states, err := store.List()
38 if err != nil {
39 return err
40 }
41
42 for _, ref := range args {
43 if project, id, ok := parseChangeRef(ref); ok {
44 if project == "" {
45 project = repoFlag
46 }
47 untrackChange(store, states, ref, project, id)
48 continue
49 }
50 prNum := strings.TrimPrefix(ref, "#")
51 found := false
52 for _, s := range states {
53 if s.Type != "track" {
54 continue
55 }
56 if extractPRNumber(s) == prNum {
57 if err := store.Delete(s.ID); err != nil {
58 fmt.Fprintf(os.Stderr, "warning: failed to remove PR #%s: %v\n", prNum, err)
59 } else {
60 fmt.Printf("Stopped tracking PR #%s\n", prNum)
61 }
62 found = true
63 break
64 }
65 }
66 if !found {
67 fmt.Fprintf(os.Stderr, "warning: no tracked PR #%s found\n", prNum)
68 }
69 }
70 return nil
71 },
72}
73
74// untrackChange removes the track record of the valley change id names, in
75// project when it isn't "", saying what it did, or why it removed none.
76func untrackChange(store run.StateStore, states []*run.State, ref, project, id string) {
77 matches := trackedChanges(states, project, id)
78 switch len(matches) {
79 case 0:
80 fmt.Fprintf(os.Stderr, "warning: no tracked change %s found\n", ref)
81 return
82 case 1:
83 default:
84 var names []string
85 for _, s := range matches {
86 names = append(names, s.Valley.Repo+":"+s.Valley.Handle)
87 }
88 fmt.Fprintf(os.Stderr, "warning: %s names tracked changes in more than one project (%s); name the project\n", ref, strings.Join(names, ", "))
89 return
90 }
91 s := matches[0]
92 if err := store.Delete(s.ID); err != nil {
93 fmt.Fprintf(os.Stderr, "warning: failed to stop tracking change %s: %v\n", ref, err)
94 return
95 }
96 fmt.Printf("Stopped tracking change %s (%s)\n", s.Valley.Handle, s.Valley.Repo)
97}
98
99func init() {
100 untrackCmd.Flags().String("repo", "", "Project of a valley change named by its handle or change ID")
101 rootCmd.AddCommand(untrackCmd)
102}