• 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.13
/cmd/describeuser.go
1
// Copyright 2018-2025 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
        "github.com/nats-io/nsc/v2/cmd/store"
20

21
        "github.com/nats-io/jwt/v2"
22
        "github.com/spf13/cobra"
23
)
24

25
func createDescribeUserCmd() *cobra.Command {
26
        var params DescribeUserParams
27
        cmd := &cobra.Command{
2✔
28
                Use:          "user",
2✔
29
                Short:        "Describes an user",
2✔
30
                Args:         cobra.MaximumNArgs(1),
2✔
31
                SilenceUsage: true,
2✔
32
                RunE: func(cmd *cobra.Command, args []string) error {
2✔
33
                        return RunAction(cmd, args, &params)
2✔
34
                },
4✔
35
        }
2✔
36
        cmd.Flags().StringVarP(&params.outputFile, "output-file", "o", "--", "output file, '--' is stdout")
2✔
37
        cmd.Flags().StringVarP(&params.user, "name", "n", "", "user name")
38
        params.AccountContextParams.BindFlags(cmd)
2✔
39

2✔
40
        return cmd
2✔
41
}
2✔
42

2✔
43
func init() {
44
        describeCmd.AddCommand(createDescribeUserCmd())
45
}
2✔
46

2✔
47
type DescribeUserParams struct {
2✔
48
        AccountContextParams
49
        BaseDescribe
50
        user string
51
}
52

53
func (p *DescribeUserParams) SetDefaults(ctx ActionCtx) error {
54
        p.user = NameFlagOrArgument(p.user, ctx)
55
        return p.AccountContextParams.SetDefaults(ctx)
56
}
57

2✔
58
func (p *DescribeUserParams) PreInteractive(ctx ActionCtx) error {
2✔
59
        var err error
2✔
60
        if err = p.AccountContextParams.Edit(ctx); err != nil {
2✔
61
                return err
62
        }
2✔
63
        if p.user == "" {
2✔
64
                p.user, err = PickUser(ctx.StoreCtx(), p.AccountContextParams.Name)
2✔
65
                if err != nil {
2✔
66
                        return err
×
67
                }
×
68
        }
4✔
69
        return nil
2✔
70
}
2✔
UNCOV
71

×
UNCOV
72
func (p *DescribeUserParams) Load(ctx ActionCtx) error {
×
73
        var err error
74
        if err = p.AccountContextParams.Validate(ctx); err != nil {
2✔
75
                return err
76
        }
77

2✔
78
        if p.user == "" {
2✔
79
                n := ctx.StoreCtx().DefaultUser(p.AccountContextParams.Name)
2✔
80
                if n != nil {
2✔
UNCOV
81
                        p.user = *n
×
UNCOV
82
                }
×
83
        }
84

4✔
85
        if p.user == "" {
2✔
86
                return fmt.Errorf("user is required")
4✔
87
        }
2✔
88

2✔
89
        p.raw, err = ctx.StoreCtx().Store.ReadRawUserClaim(p.AccountContextParams.Name, p.user)
90
        if err != nil {
91
                return err
4✔
92
        }
2✔
93
        return p.Init()
2✔
94
}
95

4✔
96
func (p *DescribeUserParams) PostInteractive(_ ActionCtx) error {
2✔
97
        return nil
2✔
UNCOV
98
}
×
UNCOV
99

×
100
func (p *DescribeUserParams) Validate(ctx ActionCtx) error {
4✔
101
        return p.AccountContextParams.Validate(ctx)
2✔
102
}
2✔
UNCOV
103

×
UNCOV
104
func (p *DescribeUserParams) Run(ctx ActionCtx) (store.Status, error) {
×
105
        if Raw || Json || JsonPath != "" {
4✔
106
                return p.Describe(ctx)
2✔
107
        }
2✔
UNCOV
108

×
UNCOV
109
        d, err := p.Raw(false)
×
110
        if err != nil {
111
                return nil, err
112
        }
2✔
113
        uc, err := jwt.DecodeUserClaims(string(d))
2✔
114
        if err != nil {
4✔
115
                return nil, err
2✔
116
        }
2✔
117
        ac, err := ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name)
2✔
118
        if err != nil {
119
                return nil, err
2✔
120
        }
121
        v := NewUserDescriber(*uc).Describe()
122
        if s, ok := ac.SigningKeys.GetScope(uc.Issuer); ok && s != nil {
2✔
123
                v = fmt.Sprintf("%s\n%s", v, NewScopedSkDescriber(s.(*jwt.UserScope)).Describe())
2✔
124
        }
2✔
125
        if IsStdOut(p.outputFile) {
126
                _, err = fmt.Fprintln(ctx.CurrentCmd().OutOrStdout(), v)
2✔
127
        } else {
2✔
128
                err = WriteFile(p.outputFile, []byte(v))
2✔
129
        }
130
        if err != nil {
2✔
131
                return nil, err
4✔
132
        }
4✔
133
        if !IsStdOut(p.outputFile) {
2✔
134
                k := "description"
2✔
135
                if Raw {
2✔
136
                        k = "jwt"
×
137
                }
×
138
                return store.OKStatus("wrote %s %s to %#q", string(p.kind), k, AbbrevHomePaths(p.outputFile)), nil
139
        }
2✔
140
        return nil, nil
2✔
141
}
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