• 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

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

16
package cmd
17

18
import (
19
        "fmt"
20
        "sort"
21
        "strings"
22

23
        cli "github.com/nats-io/cliprompts/v2"
24
        "github.com/nats-io/jwt/v2"
25
        "github.com/nats-io/nsc/v2/cmd/store"
26
)
27

28
// GenericClaimsParams - TimeParams and tags
29
type GenericClaimsParams struct {
30
        TimeParams
31
        tags   []string
32
        rmTags []string
33
}
34

35
func (sp *GenericClaimsParams) Edit(current []string) error {
36
        var err error
37
        if err := sp.TimeParams.Edit(); err != nil {
2✔
38
                return err
2✔
39
        }
2✔
UNCOV
40
        sp.rmTags, err = sp.remove("tags", current)
×
UNCOV
41
        if err != nil {
×
42
                return err
2✔
43
        }
2✔
UNCOV
44
        sp.tags, err = sp.add("tags", current)
×
UNCOV
45
        if err != nil {
×
46
                return err
2✔
47
        }
2✔
UNCOV
48
        return nil
×
UNCOV
49
}
×
50

2✔
51
func (sp *GenericClaimsParams) add(label string, current []string) ([]string, error) {
52
        first := true
53
        var values []string
2✔
54
        for {
2✔
55
                m := fmt.Sprintf("add a %s", label)
2✔
56
                if !first || len(current) > 0 {
4✔
57
                        m = fmt.Sprintf("add another %s", label)
2✔
58
                }
4✔
59
                first = false
2✔
60
                ok, err := cli.Confirm(m, false)
2✔
61
                if err != nil {
2✔
62
                        return nil, err
2✔
63
                }
2✔
UNCOV
64
                if !ok {
×
UNCOV
65
                        break
×
66
                }
4✔
67
                v, err := cli.Prompt(fmt.Sprintf("enter a %s", label), "")
2✔
68
                if err != nil {
69
                        return nil, err
2✔
70
                }
2✔
UNCOV
71
                values = append(values, v)
×
UNCOV
72
        }
×
73
        return values, nil
2✔
74
}
75

2✔
76
func (sp *GenericClaimsParams) remove(label string, values []string) ([]string, error) {
77
        var remove []string
78
        if len(values) == 0 {
2✔
79
                return nil, nil
2✔
80
        }
4✔
81
        ok, err := cli.Confirm("remove tags", false)
2✔
82
        if err != nil {
2✔
83
                return nil, err
2✔
84
        }
2✔
UNCOV
85
        if ok {
×
UNCOV
86
                idx, err := cli.MultiSelect(fmt.Sprintf("select %s to remove", label), values)
×
87
                if err != nil {
4✔
88
                        return nil, err
2✔
89
                }
2✔
UNCOV
90
                for _, v := range idx {
×
UNCOV
91
                        remove = append(remove, values[v])
×
92
                }
4✔
93
        }
2✔
94
        return remove, nil
2✔
95
}
96

2✔
97
func (sp *GenericClaimsParams) Valid() error {
98
        if err := sp.TimeParams.Validate(); err != nil {
99
                return err
2✔
100
        }
2✔
UNCOV
101
        return nil
×
UNCOV
102
}
×
103

2✔
104
func (sp *GenericClaimsParams) Run(ctx ActionCtx, claim jwt.Claims, r *store.Report) error {
105
        cd := claim.Claims()
106
        if sp.TimeParams.IsStartChanged() {
2✔
107
                ov := cd.NotBefore
2✔
108
                cd.NotBefore, _ = sp.TimeParams.StartDate()
4✔
109
                if r != nil && ov != cd.NotBefore {
2✔
110
                        if cd.NotBefore == 0 {
2✔
111
                                r.AddOK("changed jwt start to not have a start date")
4✔
112
                        } else {
2✔
UNCOV
113
                                r.AddOK("changed jwt valid start to %s - %s", UnixToDate(cd.NotBefore), strings.ToLower(HumanizedDate(cd.NotBefore)))
×
114
                        }
2✔
115
                }
2✔
116
        }
2✔
117

118
        if sp.TimeParams.IsExpiryChanged() {
119
                ov := cd.Expires
120
                cd.Expires, _ = sp.TimeParams.ExpiryDate()
4✔
121
                if r != nil && ov != cd.Expires {
2✔
122
                        if cd.Expires == 0 {
2✔
123
                                r.AddOK("changed jwt expiry to never expire")
4✔
124
                        } else {
2✔
UNCOV
125
                                r.AddOK("changed jwt expiry to %s - %s", UnixToDate(cd.Expires), strings.ToLower(HumanizedDate(cd.Expires)))
×
126
                        }
2✔
127
                }
2✔
128
        }
2✔
129

130
        var tags *jwt.TagList
131

132
        switch claim.ClaimType() {
2✔
133
        case jwt.OperatorClaim:
2✔
134
                tags = &claim.(*jwt.OperatorClaims).Tags
2✔
135
        case jwt.ActivationClaim:
2✔
136
                tags = &claim.(*jwt.ActivationClaims).Tags
2✔
UNCOV
137
        case jwt.AccountClaim:
×
UNCOV
138
                tags = &claim.(*jwt.AccountClaims).Tags
×
139
        case jwt.UserClaim:
2✔
140
                tags = &claim.(*jwt.UserClaims).Tags
2✔
141
        default:
2✔
142
                panic("unhandled claim type")
2✔
UNCOV
143
        }
×
UNCOV
144

×
145
        tags.Add(sp.tags...)
146
        tags.Remove(sp.rmTags...)
147
        sort.Strings(*tags)
2✔
148

2✔
149
        if r != nil {
2✔
150
                for _, t := range sp.tags {
2✔
151
                        r.AddOK("added tag %q", strings.ToLower(t))
4✔
152
                }
4✔
153
                for _, t := range sp.rmTags {
2✔
154
                        r.AddOK("removed tag %q", strings.ToLower(t))
2✔
155
                }
4✔
156
        }
2✔
157
        return nil
2✔
158
}
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