forked from nats-io/nsc
/
deleteimport.go
202 lines (176 loc) · 4.81 KB
/
deleteimport.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
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
202
/*
* Copyright 2018-2019 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"strings"
"github.com/kbehouse/nsc/cmd/store"
cli "github.com/nats-io/cliprompts/v2"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
"github.com/spf13/cobra"
)
func createDeleteImportCmd() *cobra.Command {
var params DeleteImportParams
cmd := &cobra.Command{
Use: "import",
Short: "Delete an import",
Args: MaxArgs(0),
Example: params.longHelp(),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return RunAction(cmd, args, ¶ms)
},
}
cmd.Flags().StringVarP(¶ms.subject, "subject", "s", "", "stream/service subject")
cmd.Flags().StringVarP(¶ms.srcAccount, "src-account", "", "", "source account (only if subject is ambiguous)")
params.AccountContextParams.BindFlags(cmd)
return cmd
}
func init() {
deleteCmd.AddCommand(createDeleteImportCmd())
}
type DeleteImportParams struct {
AccountContextParams
SignerParams
claim *jwt.AccountClaims
index int
subject string
picked bool
srcAccount string
}
func (p *DeleteImportParams) longHelp() string {
v := `toolName delete import -i
toolName delete import -s "bar.>"`
return strings.Replace(v, "toolName", GetToolName(), -1)
}
func (p *DeleteImportParams) SetDefaults(ctx ActionCtx) error {
if err := p.AccountContextParams.SetDefaults(ctx); err != nil {
return err
}
p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx)
p.index = -1
return nil
}
func (p *DeleteImportParams) PreInteractive(ctx ActionCtx) error {
p.picked = true
var err error
if err = p.AccountContextParams.Edit(ctx); err != nil {
return err
}
return nil
}
func (p *DeleteImportParams) Load(ctx ActionCtx) error {
var err error
if err = p.AccountContextParams.Validate(ctx); err != nil {
return err
}
p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name)
if err != nil {
return err
}
switch len(p.claim.Imports) {
case 0:
return fmt.Errorf("account %q doesn't have imports", p.AccountContextParams.Name)
case 1:
if p.subject == "" {
p.subject = string(p.claim.Imports[0].Subject)
}
}
// if auto-selecting there can only be one
count := 0
last := -1
for i, e := range p.claim.Imports {
if string(e.Subject) == p.subject {
count++
last = i
}
}
if count == 1 {
p.index = last
}
// if we have more than one, both the subject and src account must match
if count > 1 {
for i, e := range p.claim.Imports {
if string(e.Subject) == p.subject && p.srcAccount == e.Account {
p.index = i
break
}
}
}
return nil
}
func (p *DeleteImportParams) PostInteractive(ctx ActionCtx) error {
var err error
choices, err := GetAccountImports(p.claim)
if err != nil {
return err
}
labels := choices.String()
p.index, err = cli.Select("select import to delete", "", labels)
if err != nil {
return err
}
if err = p.SignerParams.Edit(ctx); err != nil {
return err
}
return nil
}
func (p *DeleteImportParams) Validate(ctx ActionCtx) error {
var err error
if p.subject == "" && p.index == -1 {
return fmt.Errorf("subject is required")
}
if p.index == -1 && p.srcAccount == "" && !p.picked {
var accounts []string
for _, e := range p.claim.Imports {
if string(e.Subject) == p.subject {
accounts = append(accounts, e.Account)
}
}
if len(accounts) > 1 && p.srcAccount == "" {
return fmt.Errorf("more than one import %q found - specify --src-account with one of %s", p.subject, strings.Join(accounts, ", "))
}
}
if p.index == -1 {
m := fmt.Sprintf("no import matching %q found", p.subject)
if p.srcAccount != "" {
m = fmt.Sprintf("%s from account %s", m, p.srcAccount)
}
return fmt.Errorf(m)
}
if err = p.SignerParams.Resolve(ctx); err != nil {
return err
}
return nil
}
func (p *DeleteImportParams) Run(ctx ActionCtx) (store.Status, error) {
din := p.claim.Imports[p.index]
p.claim.Imports = append(p.claim.Imports[:p.index], p.claim.Imports[p.index+1:]...)
token, err := p.claim.Encode(p.signerKP)
if err != nil {
return nil, err
}
r := store.NewDetailedReport(true)
r.AddOK("deleted %s import %q", din.Type, din.Subject)
rs, err := ctx.StoreCtx().Store.StoreClaim([]byte(token))
if rs != nil {
r.Add(rs)
}
if err != nil {
r.Add(store.FromError(err))
}
return r, nil
}