-
Notifications
You must be signed in to change notification settings - Fork 26
/
manifest.go
135 lines (113 loc) · 2.78 KB
/
manifest.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
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
package cmd
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/clients"
"github.com/fatih/color"
"github.com/pkg/errors"
)
type ManifestCmd struct {
Get GetManifestCmd `cmd:"" help:"get manifest"`
Set SetManifestCmd `cmd:"" help:"set manifest"`
Delete DeleteManifestCmd `cmd:"" help:"delete manifest"`
}
type GetManifestCmd struct {
Path string `arg:"path" help:"filepath to manifest file" type:"path" optional:""`
Stdout bool `flag:"" help:"output manifest to --stdout"`
clients.Config
}
type SetManifestCmd struct {
Path string `arg:"" help:"filepath to manifest file" type:"path" optional:""`
Stdin bool `flag:"" help:"set manifest from --stdin"`
clients.Config
}
type DeleteManifestCmd struct {
Force bool `flag:"" help:"do not ask for conformation to delete manifest"`
clients.Config
}
func (cmd *GetManifestCmd) Run(c *cc.CommonCtx) error {
if !c.IsServing(cmd.Host) {
return errors.Wrap(ErrNotServing, cmd.Host)
}
dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
color.Green(">>> get manifest to %s", cmd.Path)
r, err := dirClient.V3.GetManifest(c.Context)
if err != nil {
return err
}
w := os.Stdout
if cmd.Path != "" {
w, err = os.Create(cmd.Path)
if err != nil {
return err
}
}
defer w.Close()
if _, err := io.Copy(w, r); err != nil {
return err
}
return nil
}
func (cmd *SetManifestCmd) Run(c *cc.CommonCtx) error {
if !c.IsServing(cmd.Host) {
return errors.Wrap(ErrNotServing, cmd.Host)
}
dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
r := os.Stdin
if cmd.Path != "" {
r, err = os.Open(cmd.Path)
if err != nil {
return err
}
}
color.Green(">>> set manifest from %s", cmd.Path)
return dirClient.V3.SetManifest(c.Context, r)
}
func (cmd *DeleteManifestCmd) Run(c *cc.CommonCtx) error {
if !c.IsServing(cmd.Host) {
return errors.Wrap(ErrNotServing, cmd.Host)
}
dirClient, err := clients.NewDirectoryClient(c, &cmd.Config)
if err != nil {
return err
}
c.UI.Exclamation().Msg("WARNING: delete manifest resets all directory state, including relation and object data")
if cmd.Force || promptYesNo("Do you want to continue?", false) {
color.Green(">>> delete manifest")
return dirClient.V3.DeleteManifest(c.Context)
}
return nil
}
func promptYesNo(label string, def bool) bool {
choices := "Y/n"
if !def {
choices = "y/N"
}
r := bufio.NewReader(os.Stdin)
var s string
for {
fmt.Fprintf(os.Stderr, "%s (%s) ", label, choices)
s, _ = r.ReadString('\n')
s = strings.TrimSpace(s)
if s == "" {
return def
}
s = strings.ToLower(s)
if s == "y" || s == "yes" {
return true
}
if s == "n" || s == "no" {
return false
}
}
}