arche / internal/cli/cmd_phase.go

commit 154431fd
  1package cli
  2
  3import (
  4	"fmt"
  5	"time"
  6
  7	"arche/internal/object"
  8	"arche/internal/store"
  9
 10	"github.com/spf13/cobra"
 11)
 12
 13var phaseCmd = &cobra.Command{
 14	Use:   "phase",
 15	Short: "Manage commit phases (draft/public/secret)",
 16}
 17
 18var phaseSetCmd = &cobra.Command{
 19	Use:   "set <commit> <phase>",
 20	Short: "Set the phase of a commit",
 21	Long: `Manually set the lifecycle phase of a commit.
 22
 23Valid phases: draft, public, secret
 24
 25  draft   - work in progress; can be rewritten freely
 26  public  - pushed / shared; rewriting is discouraged
 27  secret  - hidden from default log output
 28`,
 29	Args: cobra.ExactArgs(2),
 30	RunE: func(cmd *cobra.Command, args []string) error {
 31		r := openRepo()
 32		defer r.Close()
 33
 34		commitID, err := resolveRef(r, args[0])
 35		if err != nil {
 36			return err
 37		}
 38
 39		var phase object.Phase
 40		switch args[1] {
 41		case "draft":
 42			phase = object.PhaseDraft
 43		case "public":
 44			phase = object.PhasePublic
 45		case "secret":
 46			phase = object.PhaseSecret
 47		default:
 48			return fmt.Errorf("unknown phase %q (want draft|public|secret)", args[1])
 49		}
 50
 51		before, _ := r.CaptureRefState()
 52		now := time.Now()
 53
 54		tx, err := r.Store.Begin()
 55		if err != nil {
 56			return err
 57		}
 58		if err := r.Store.SetPhase(tx, commitID, phase); err != nil {
 59			r.Store.Rollback(tx)
 60			return err
 61		}
 62
 63		after := before
 64		op := store.Operation{
 65			Kind: "phase-set", Timestamp: now.Unix(), Before: before, After: after,
 66			Metadata: fmt.Sprintf("set %s phase to %s", args[0], args[1]),
 67		}
 68		if _, err := r.Store.InsertOperation(tx, op); err != nil {
 69			r.Store.Rollback(tx)
 70			return err
 71		}
 72		if err := r.Store.Commit(tx); err != nil {
 73			return err
 74		}
 75
 76		c, _ := r.ReadCommit(commitID)
 77		displayID := fmt.Sprintf("%x", commitID[:6])
 78		if c != nil {
 79			displayID = "ch:" + c.ChangeID
 80		}
 81		fmt.Printf("Set phase of %s to %s\n", displayID, phase)
 82		return nil
 83	},
 84}
 85
 86var phaseGetCmd = &cobra.Command{
 87	Use:   "get <commit>",
 88	Short: "Show the current phase of a commit",
 89	Args:  cobra.ExactArgs(1),
 90	RunE: func(cmd *cobra.Command, args []string) error {
 91		r := openRepo()
 92		defer r.Close()
 93
 94		commitID, err := resolveRef(r, args[0])
 95		if err != nil {
 96			return err
 97		}
 98
 99		phase, err := r.Store.GetPhase(commitID)
100		if err != nil {
101			c, err2 := r.ReadCommit(commitID)
102			if err2 != nil {
103				return err2
104			}
105			phase = c.Phase
106		}
107
108		fmt.Println(phase.String())
109		return nil
110	},
111}
112
113func init() {
114	phaseCmd.AddCommand(phaseSetCmd)
115	phaseCmd.AddCommand(phaseGetCmd)
116}