-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
68 lines (55 loc) · 1.5 KB
/
edit.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
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"os"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"github.com/UsingCoding/gostore/internal/common/maybe"
appedit "github.com/UsingCoding/gostore/internal/gostore/app/edit"
"github.com/UsingCoding/gostore/internal/gostore/app/store"
"github.com/UsingCoding/gostore/internal/gostore/infrastructure/consoleoutput"
"github.com/UsingCoding/gostore/internal/gostore/infrastructure/editor"
)
func edit() *cli.Command {
return &cli.Command{
Name: "edit",
Usage: "Edit secrets",
UsageText: "edit <SECRET_ID> ?<KEY>",
Action: executeEdit,
BashComplete: func(ctx *cli.Context) {
if ctx.NArg() > 0 {
return
}
service, _ := newStoreService(ctx)
entries, err := service.List(ctx.Context, store.ListParams{})
if err != nil {
return
}
o := consoleoutput.New(os.Stdout, consoleoutput.WithNewline(true))
for _, p := range inlinePaths(entries) {
o.Printf(p)
}
},
}
}
func executeEdit(ctx *cli.Context) error {
if ctx.Args().Len() < 1 {
return errors.New("not enough arguments")
}
path := ctx.Args().Get(0)
var key maybe.Maybe[string]
if ctx.Args().Len() > 1 {
key = maybe.NewJust(ctx.Args().Get(1))
}
s, _ := newStoreService(ctx)
e, err := editor.NewEditor()
if err != nil {
return err
}
o := consoleoutput.New(os.Stdout, consoleoutput.WithNewline(true))
err = appedit.NewService(s, e).Edit(ctx.Context, path, key)
if errors.Is(err, appedit.ErrNoChangesMade) {
o.Printf("No changes made")
return nil
}
return err
}