-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
172 lines (146 loc) · 4.12 KB
/
Copy pathenv.go
File metadata and controls
172 lines (146 loc) · 4.12 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
package cli
import (
"fmt"
"text/tabwriter"
"github.com/dvflw/mantle/internal/config"
"github.com/dvflw/mantle/internal/db"
"github.com/dvflw/mantle/internal/environment"
"github.com/dvflw/mantle/internal/workflow"
"github.com/spf13/cobra"
)
func newEnvCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "env",
Short: "Manage named environments",
Long: "Create, list, show, and delete named environments for parameterized workflow execution.",
}
cmd.AddCommand(newEnvCreateCommand())
cmd.AddCommand(newEnvListCommand())
cmd.AddCommand(newEnvShowCommand())
cmd.AddCommand(newEnvDeleteCommand())
return cmd
}
func newEnvCreateCommand() *cobra.Command {
var fromFile string
cmd := &cobra.Command{
Use: "create <name>",
Short: "Create a named environment",
Long: "Creates a named environment from a values file.",
Example: ` mantle env create production --from prod.values.yaml
mantle env create staging --from staging.values.yaml`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
vals, err := workflow.LoadValues(fromFile)
if err != nil {
return fmt.Errorf("loading values file: %w", err)
}
store, cleanup, err := newEnvStore(cmd)
if err != nil {
return err
}
defer cleanup()
env, err := store.Create(cmd.Context(), name, vals.Inputs, vals.Env)
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Created environment %q\n", env.Name)
return nil
},
}
cmd.Flags().StringVar(&fromFile, "from", "", "Values file to load inputs and env from (required)")
_ = cmd.MarkFlagRequired("from")
return cmd
}
func newEnvListCommand() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List all environments",
RunE: func(cmd *cobra.Command, args []string) error {
store, cleanup, err := newEnvStore(cmd)
if err != nil {
return err
}
defer cleanup()
envs, err := store.List(cmd.Context())
if err != nil {
return err
}
if len(envs) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "(no environments)")
return nil
}
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "NAME\tCREATED")
for _, e := range envs {
fmt.Fprintf(w, "%s\t%s\n", e.Name, e.CreatedAt.Format("2006-01-02 15:04:05"))
}
return w.Flush()
},
}
}
func newEnvShowCommand() *cobra.Command {
return &cobra.Command{
Use: "show <name>",
Short: "Show environment details",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
store, cleanup, err := newEnvStore(cmd)
if err != nil {
return err
}
defer cleanup()
env, err := store.Get(cmd.Context(), args[0])
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Name: %s\n", env.Name)
if len(env.Inputs) > 0 {
fmt.Fprintln(cmd.OutOrStdout(), "\nInputs:")
for k, v := range env.Inputs {
fmt.Fprintf(cmd.OutOrStdout(), " %s: %v\n", k, v)
}
}
if len(env.Env) > 0 {
fmt.Fprintln(cmd.OutOrStdout(), "\nEnv:")
for k, v := range env.Env {
fmt.Fprintf(cmd.OutOrStdout(), " %s: %s\n", k, v)
}
}
return nil
},
}
}
func newEnvDeleteCommand() *cobra.Command {
return &cobra.Command{
Use: "delete <name>",
Short: "Delete an environment",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
store, cleanup, err := newEnvStore(cmd)
if err != nil {
return err
}
defer cleanup()
if err := store.Delete(cmd.Context(), args[0]); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Deleted environment %q\n", args[0])
return nil
},
}
}
// newEnvStore builds an environment.Store from the current command context.
func newEnvStore(cmd *cobra.Command) (*environment.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 := &environment.Store{DB: database}
cleanup := func() { database.Close() }
return store, cleanup, nil
}