/
rm.go
57 lines (49 loc) · 1.01 KB
/
rm.go
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
package main
import (
"log"
"path/filepath"
"github.com/spf13/cobra"
)
type rmOpt struct {
Files []string
ctx *Context
}
func runRm(opts *rmOpt) error {
for _, f := range opts.Files {
abs := filepath.Join(opts.ctx.WorkingDir, f)
rel, err := filepath.Rel(opts.ctx.RootDir, abs)
if err != nil {
return err
}
err = opts.ctx.Config.RemoveSecret(rel)
if err != nil {
return err
} else {
log.Printf("removed secret %q", rel)
}
}
return opts.ctx.WriteConfigFile()
}
func newRmCmd(ctx *Context) *cobra.Command {
opts := &rmOpt{
ctx: ctx,
}
cmd := &cobra.Command{
Use: "rm",
Short: "Remove secret file and untrack from agec",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if ctx == nil {
return ErrConfigurationNotFound
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Files = args
return runRm(opts)
},
ValidArgsFunction: SecretCompletion(ctx),
}
return cmd
}