-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepos.go
More file actions
201 lines (186 loc) · 6.43 KB
/
Copy pathrepos.go
File metadata and controls
201 lines (186 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package cli
import (
"fmt"
"text/tabwriter"
"github.com/dvflw/mantle/internal/config"
"github.com/dvflw/mantle/internal/db"
"github.com/dvflw/mantle/internal/repo"
"github.com/spf13/cobra"
)
// newReposCommand returns the "repos" subcommand for managing registered
// GitOps source repositories (issue #16). Subcommands handle registration,
// listing, detailed status, and removal. Sync behavior lives in Plan B.
func newReposCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "repos",
Short: "Manage GitOps workflow source repositories",
Long: `Registers GitOps source repositories whose workflow YAML definitions will be
synced into this Mantle instance. This command manages the registry; the sync
engine itself (sidecar, file discovery, validate/plan/apply) ships in a later
milestone. Auth material is stored in a "git" credential type
(` + "`mantle secrets create --type git`" + `) and referenced here by name.`,
}
cmd.AddCommand(newReposAddCommand())
cmd.AddCommand(newReposListCommand())
cmd.AddCommand(newReposStatusCommand())
cmd.AddCommand(newReposRemoveCommand())
return cmd
}
// newRepoStore builds a repo.Store from the current command context.
func newRepoStore(cmd *cobra.Command) (*repo.Store, func(), error) {
cfg := config.FromContext(cmd.Context())
if cfg == nil {
return nil, nil, fmt.Errorf("config not loaded")
}
database, err := db.Open(cfg.Database)
if err != nil {
return nil, nil, fmt.Errorf("failed to connect to database: %w", err)
}
store := &repo.Store{DB: database, Actor: "cli"}
cleanup := func() { database.Close() }
return store, cleanup, nil
}
func newReposAddCommand() *cobra.Command {
var url, branch, path, pollInterval, credential string
var autoApply, prune bool
cmd := &cobra.Command{
Use: "add <name>",
Short: "Register a new GitOps source repo",
Long: `Registers a new repository to sync workflow definitions from. The named
credential must already exist and be of type "git".`,
Example: ` mantle repos add acme --url https://github.com/acme/workflows.git --credential github-pat
mantle repos add staging --url git@github.com:acme/wf.git --credential github-ssh --branch release`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
store, cleanup, err := newRepoStore(cmd)
if err != nil {
return err
}
defer cleanup()
r, err := store.Create(cmd.Context(), repo.CreateParams{
Name: args[0],
URL: url,
Branch: branch,
Path: path,
PollInterval: pollInterval,
Credential: credential,
AutoApply: autoApply,
Prune: prune,
})
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Added repo %q (%s)\n", r.Name, r.ID)
return nil
},
}
cmd.Flags().StringVar(&url, "url", "", "Git repository URL (required)")
cmd.Flags().StringVar(&branch, "branch", "main", "Branch to sync")
cmd.Flags().StringVar(&path, "path", "/", "Subdirectory inside the repo to scan")
cmd.Flags().StringVar(&pollInterval, "poll-interval", "60s", "Interval between syncs (Go duration, min 10s)")
cmd.Flags().StringVar(&credential, "credential", "", "Git credential name (required)")
cmd.Flags().BoolVar(&autoApply, "auto-apply", true, "Automatically apply changes (false = plan-only)")
cmd.Flags().BoolVar(&prune, "prune", true, "When true, workflows deleted from the repo are disabled in Mantle")
_ = cmd.MarkFlagRequired("url")
_ = cmd.MarkFlagRequired("credential")
return cmd
}
func newReposStatusCommand() *cobra.Command {
return &cobra.Command{
Use: "status <name>",
Short: "Show detailed status for a registered repo",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
store, cleanup, err := newRepoStore(cmd)
if err != nil {
return err
}
defer cleanup()
r, err := store.Get(cmd.Context(), args[0])
if err != nil {
return err
}
out := cmd.OutOrStdout()
fmt.Fprintf(out, "Name: %s\n", r.Name)
fmt.Fprintf(out, "ID: %s\n", r.ID)
fmt.Fprintf(out, "URL: %s\n", r.URL)
fmt.Fprintf(out, "Branch: %s\n", r.Branch)
fmt.Fprintf(out, "Path: %s\n", r.Path)
fmt.Fprintf(out, "Poll: %s\n", r.PollInterval)
fmt.Fprintf(out, "Credential: %s\n", r.Credential)
fmt.Fprintf(out, "Auto-Apply: %t\n", r.AutoApply)
fmt.Fprintf(out, "Prune: %t\n", r.Prune)
fmt.Fprintf(out, "Enabled: %t\n", r.Enabled)
if r.LastSyncAt != nil {
fmt.Fprintf(out, "Last Sync: %s (SHA %s)\n",
r.LastSyncAt.UTC().Format("2006-01-02 15:04:05 UTC"), r.LastSyncSHA)
} else {
fmt.Fprintln(out, "Last Sync: (never)")
}
if r.LastSyncError != "" {
fmt.Fprintf(out, "Last Error: %s\n", r.LastSyncError)
}
return nil
},
}
}
func newReposRemoveCommand() *cobra.Command {
var yes bool
cmd := &cobra.Command{
Use: "remove <name>",
Short: "Unregister a GitOps source repo",
Long: `Unregisters a repo. Any previously applied workflows remain in place — this
command only stops future syncs. Requires --yes to confirm.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if !yes {
return fmt.Errorf("refusing to remove %q without --yes", args[0])
}
store, cleanup, err := newRepoStore(cmd)
if err != nil {
return err
}
defer cleanup()
if err := store.Delete(cmd.Context(), args[0]); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Removed repo %q\n", args[0])
return nil
},
}
cmd.Flags().BoolVarP(&yes, "yes", "y", false, "Confirm deletion (required)")
return cmd
}
func newReposListCommand() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List all registered GitOps repos",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
store, cleanup, err := newRepoStore(cmd)
if err != nil {
return err
}
defer cleanup()
repos, err := store.List(cmd.Context())
if err != nil {
return err
}
if len(repos) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "(no repos)")
return nil
}
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "NAME\tURL\tBRANCH\tAUTO-APPLY\tENABLED\tLAST SYNC")
for _, r := range repos {
last := "(never)"
if r.LastSyncAt != nil {
last = r.LastSyncAt.UTC().Format("2006-01-02 15:04:05 UTC")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%t\t%t\t%s\n",
r.Name, r.URL, r.Branch, r.AutoApply, r.Enabled, last)
}
return w.Flush()
},
}
}