gunk-dev/the-valley

pushlog.go

source60ad80d5a5a5main's published tip history
2.5 KiB
1package cmd
2
3import (
4 "fmt"
5 "os"
6
7 "the-valley/flow/internal/config"
8 "the-valley/flow/internal/git"
9 "github.com/spf13/cobra"
10)
11
12var pushLogPush bool
13
14var pushLogCmd = &cobra.Command{
15 Use: "push-log <run-id>",
16 Short: "Store a log held back for sensitivity on the data ref",
17 Long: `Commits a run's log to the data ref after the sensitivity scan held it
18back at finalize. Use after reviewing the log and confirming it's safe.
19
20The commit goes to the data ref in the run's local clone. It is pushed to
21origin only with --push, or when push_data_ref is true in config. A pushed
22data ref is world-readable on a public repo, and the push sends the ref's
23whole history, not just this log.`,
24 Args: cobra.ExactArgs(1),
25 RunE: func(cmd *cobra.Command, args []string) error {
26 id := args[0]
27
28 store, err := sessionStore()
29 if err != nil {
30 return err
31 }
32 state, err := store.Load(id)
33 if err != nil {
34 return fmt.Errorf("no run found with id: %s", id)
35 }
36
37 // Same repo _finalize synced to, so replay finds it there.
38 var root string
39 if state.CloneDir != nil {
40 root = *state.CloneDir
41 } else if root, err = git.RepoRoot(); err != nil {
42 return fmt.Errorf("not inside a git repository")
43 }
44
45 cfg, err := config.Load(root)
46 if err != nil {
47 return err
48 }
49
50 if state.LogFile == nil {
51 return fmt.Errorf("no log file for run %s", id)
52 }
53
54 if _, err := os.Stat(*state.LogFile); err != nil {
55 return fmt.Errorf("log file not found: %s", *state.LogFile)
56 }
57
58 ctx := cmd.Context()
59 gitClient := git.NewExecClient()
60
61 stateFile := store.StateDir() + "/" + id + ".json"
62 files := map[string]string{
63 "runs/" + id + ".json": stateFile,
64 "logs/" + id + ".jsonl": *state.LogFile,
65 }
66
67 if err := gitClient.SyncToDataRef(ctx, root, cfg.DataRef, "Run "+id, files); err != nil {
68 return fmt.Errorf("syncing to data ref: %w", err)
69 }
70 fmt.Printf("Committed log for %s to %s in %s (sensitivity check bypassed).\n", id, cfg.DataRef, root)
71
72 if !pushLogPush && !cfg.PushDataRef {
73 fmt.Printf("Not pushed: push_data_ref is false, so %s stays local. Pass --push to publish it to origin.\n", cfg.DataRef)
74 return nil
75 }
76
77 if err := gitClient.PushDataRef(ctx, root, cfg.DataRef); err != nil {
78 return fmt.Errorf("pushing %s to origin (the local commit is kept): %w", cfg.DataRef, err)
79 }
80 fmt.Printf("Pushed %s to origin.\n", cfg.DataRef)
81 return nil
82 },
83}
84
85func init() {
86 pushLogCmd.Flags().BoolVar(&pushLogPush, "push", false, "also push the data ref to origin (default: push only when push_data_ref is true)")
87 rootCmd.AddCommand(pushLogCmd)
88}