arche / internal/cli/cmd_oplog.go

commit 154431fd
 1package cli
 2
 3import (
 4	"fmt"
 5	"time"
 6
 7	"github.com/spf13/cobra"
 8)
 9
10var opLogCmd = &cobra.Command{
11	Use:   "op",
12	Short: "Operation log subcommands",
13	Long:  "Subcommands for the operation log.",
14}
15
16var opLogListCmd = &cobra.Command{
17	Use:   "log",
18	Short: "Show the operation history",
19	Long: `Display the operation log in reverse chronological order. Each entry shows
20the sequence number, operation kind, timestamp, and a summary of what changed.
21Use 'arche undo --step N' to revert to any point in this log.`,
22	RunE: func(cmd *cobra.Command, args []string) error {
23		r := openRepo()
24		defer r.Close()
25
26		ops, err := r.Store.ListOperations(0)
27		if err != nil {
28			return err
29		}
30		if len(ops) == 0 {
31			fmt.Println("No operations recorded.")
32			return nil
33		}
34		for _, op := range ops {
35			ts := time.Unix(op.Timestamp, 0).Format("2006-01-02 15:04:05")
36			if op.Metadata != "" {
37				fmt.Printf("#%-4d  %-14s  %s  %s\n", op.Seq, op.Kind, ts, op.Metadata)
38			} else {
39				fmt.Printf("#%-4d  %-14s  %s\n", op.Seq, op.Kind, ts)
40			}
41		}
42		return nil
43	},
44}
45
46func init() {
47	opLogCmd.AddCommand(opLogListCmd)
48}