• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nsc / 14765656943

30 Apr 2025 10:34PM UTC coverage: 69.949% (-4.2%) from 74.148%
14765656943

Pull #691

github

web-flow
Merge b4ee16f94 into a9c0df586
Pull Request #691: Generate docs

1 of 1 new or added line in 1 file covered. (100.0%)

3680 existing lines in 93 files now uncovered.

12325 of 17620 relevant lines covered (69.95%)

1.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.05
/cmd/deleteexport.go
1
// Copyright 2018-2019 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package cmd
15

16
import (
17
        "fmt"
18

19
        cli "github.com/nats-io/cliprompts/v2"
20
        "github.com/nats-io/jwt/v2"
21
        "github.com/nats-io/nkeys"
22
        "github.com/nats-io/nsc/v2/cmd/store"
23
        "github.com/spf13/cobra"
24
)
25

26
func createDeleteExportCmd() *cobra.Command {
27
        var params DeleteExportParams
28
        cmd := &cobra.Command{
2✔
29
                Use:          "export",
2✔
30
                Short:        "Delete an export",
2✔
31
                Args:         MaxArgs(0),
2✔
32
                SilenceUsage: true,
2✔
33
                RunE: func(cmd *cobra.Command, args []string) error {
2✔
34
                        return RunAction(cmd, args, &params)
2✔
35
                },
4✔
36
        }
2✔
37
        cmd.Flags().StringVarP(&params.subject, "subject", "s", "", "subject")
2✔
38
        params.AccountContextParams.BindFlags(cmd)
39
        return cmd
2✔
40
}
2✔
41

2✔
42
func init() {
43
        deleteCmd.AddCommand(createDeleteExportCmd())
44
}
2✔
45

2✔
46
type DeleteExportParams struct {
2✔
47
        AccountContextParams
48
        SignerParams
49
        claim   *jwt.AccountClaims
50
        index   int
51
        subject string
52
}
53

54
func (p *DeleteExportParams) SetDefaults(ctx ActionCtx) error {
55
        p.AccountContextParams.SetDefaults(ctx)
56
        p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx)
2✔
57
        p.index = -1
2✔
58
        return nil
2✔
59
}
2✔
60

2✔
61
func (p *DeleteExportParams) PreInteractive(ctx ActionCtx) error {
2✔
62
        var err error
63
        if err = p.AccountContextParams.Edit(ctx); err != nil {
2✔
64
                return err
2✔
65
        }
2✔
UNCOV
66
        return nil
×
UNCOV
67
}
×
68

2✔
69
func (p *DeleteExportParams) Load(ctx ActionCtx) error {
70
        var err error
71

2✔
72
        if err = p.AccountContextParams.Validate(ctx); err != nil {
2✔
73
                return err
2✔
74
        }
4✔
75

2✔
76
        p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name)
2✔
77
        if err != nil {
78
                return err
2✔
79
        }
2✔
UNCOV
80

×
UNCOV
81
        switch len(p.claim.Exports) {
×
82
        case 0:
83
                return fmt.Errorf("account %q doesn't have exports", p.AccountContextParams.Name)
2✔
UNCOV
84
        case 1:
×
UNCOV
85
                if p.subject == "" {
×
86
                        p.subject = string(p.claim.Exports[0].Subject)
2✔
87
                }
4✔
88
        }
2✔
89
        for i, e := range p.claim.Exports {
2✔
90
                if string(e.Subject) == p.subject {
91
                        p.index = i
4✔
92
                        break
4✔
93
                }
2✔
94
        }
2✔
95

96
        return nil
97
}
98

2✔
99
func (p *DeleteExportParams) PostInteractive(ctx ActionCtx) error {
100
        var err error
101

2✔
102
        choices, err := GetAccountExports(p.claim)
2✔
103
        if err != nil {
2✔
104
                return err
2✔
105
        }
2✔
UNCOV
106
        labels := AccountExportChoices(choices).String()
×
UNCOV
107

×
108
        p.index, err = cli.Select("select export to delete", "", labels)
2✔
109
        if err != nil {
2✔
110
                return err
2✔
111
        }
2✔
UNCOV
112

×
UNCOV
113
        if err = p.SignerParams.Edit(ctx); err != nil {
×
114
                return err
115
        }
2✔
UNCOV
116

×
UNCOV
117
        return nil
×
118
}
119

2✔
120
func (p *DeleteExportParams) Validate(ctx ActionCtx) error {
121
        if p.subject == "" && p.index == -1 {
122
                return fmt.Errorf("subject is required")
2✔
123
        }
4✔
124
        if p.index == -1 {
2✔
125
                return fmt.Errorf("no export matching %q found", p.subject)
2✔
126
        }
4✔
127
        if err := p.SignerParams.Resolve(ctx); err != nil {
2✔
128
                return err
2✔
129
        }
2✔
UNCOV
130
        return nil
×
UNCOV
131
}
×
132

2✔
133
func (p *DeleteExportParams) Run(ctx ActionCtx) (store.Status, error) {
134
        dex := p.claim.Exports[p.index]
135
        p.claim.Exports = append(p.claim.Exports[:p.index], p.claim.Exports[p.index+1:]...)
2✔
136
        token, err := p.claim.Encode(p.signerKP)
2✔
137
        if err != nil {
2✔
138
                return nil, err
2✔
139
        }
2✔
UNCOV
140

×
UNCOV
141
        r := store.NewDetailedReport(true)
×
142
        r.AddOK("deleted %s export %q", dex.Type, dex.Subject)
143
        rs, err := ctx.StoreCtx().Store.StoreClaim([]byte(token))
2✔
144
        if rs != nil {
2✔
145
                r.Add(rs)
2✔
146
        }
4✔
147
        if err != nil {
2✔
148
                r.AddFromError(err)
2✔
149
        }
2✔
UNCOV
150
        return r, nil
×
UNCOV
151
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc