• 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

74.6
/cmd/pubkeyparams.go
1
// Copyright 2018-2022 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

23
        "github.com/nats-io/nsc/v2/cmd/store"
24

25
        "github.com/spf13/cobra"
26
)
27

28
type PubKeyChoice struct {
29
        Label string
30
        Key   string
31
}
32

33
type PubKeyParams struct {
34
        flagName      string
35
        kind          nkeys.PrefixByte
36
        publicKey     string
37
        AllowWildcard bool
38
}
39

40
func (e *PubKeyParams) SetDefaults(ctx ActionCtx) error {
41
        if e.publicKey == "" || e.publicKey == "*" || store.IsPublicKey(e.kind, e.publicKey) {
42
                return nil
2✔
43
        }
4✔
44
        switch e.kind {
2✔
45
        case nkeys.PrefixByteAccount:
2✔
46
                if c, err := ctx.StoreCtx().Store.ReadAccountClaim(e.publicKey); err != nil {
2✔
47
                        return err
2✔
48
                } else {
2✔
UNCOV
49
                        e.publicKey = c.Subject
×
50
                }
2✔
51
        case nkeys.PrefixByteUser:
2✔
52
                an := ctx.StoreCtx().Account.Name
2✔
53
                if c, err := ctx.StoreCtx().Store.ReadUserClaim(an, e.publicKey); err != nil {
×
54
                        return err
×
55
                } else {
×
56
                        e.publicKey = c.Subject
×
57
                }
×
UNCOV
58
        }
×
UNCOV
59
        return nil
×
60
}
61

2✔
62
func (e *PubKeyParams) BindFlags(flagName string, shorthand string, kind nkeys.PrefixByte, cmd *cobra.Command) {
63
        e.flagName = flagName
64
        e.kind = kind
2✔
65
        cmd.Flags().StringVarP(&e.publicKey, flagName, shorthand, "", flagName)
2✔
66
}
2✔
67

2✔
68
func (e *PubKeyParams) valid(s string) error {
2✔
69
        if s == "" {
70
                return fmt.Errorf("%s cannot be empty", e.flagName)
2✔
71
        }
4✔
72
        if e.AllowWildcard && s == jwt.All {
2✔
73
                return nil
2✔
74
        }
4✔
75

2✔
76
        if !store.IsPublicKey(e.kind, s) {
2✔
77
                return fmt.Errorf("%s is not a valid %q public key", e.publicKey, e.kind.String())
78
        }
2✔
UNCOV
79

×
UNCOV
80
        return nil
×
81
}
82

2✔
83
func (e *PubKeyParams) Valid() error {
84
        return e.valid(e.publicKey)
85
}
2✔
86

2✔
87
func (e *PubKeyParams) Select(label string, choices ...PubKeyChoice) error {
2✔
88
        var labels []string
89
        for _, c := range choices {
2✔
90
                labels = append(labels, c.Label)
2✔
91
        }
4✔
92
        sel, err := cli.Select(label, "", labels)
2✔
93
        if err != nil {
2✔
94
                return err
2✔
95
        }
2✔
UNCOV
96
        if sel == -1 {
×
97
                return fmt.Errorf("nothing selected")
×
98
        }
2✔
UNCOV
99
        e.publicKey = choices[sel].Key
×
UNCOV
100
        return nil
×
101
}
2✔
102

2✔
103
func (e *PubKeyParams) Edit() error {
104
        m := fmt.Sprintf("%s public key", e.flagName)
105
        if e.AllowWildcard {
2✔
106
                m = fmt.Sprintf("%s or '*' to match any %s", m, e.flagName)
2✔
107
        }
4✔
108
        sv, err := cli.Prompt(m, e.publicKey, cli.Val(e.valid))
2✔
109
        if err != nil {
2✔
110
                return err
2✔
111
        }
2✔
UNCOV
112
        e.publicKey = sv
×
UNCOV
113
        return nil
×
114
}
2✔
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