• 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

76.24
/cmd/addmapping.go
1
// Copyright 2021 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
        "errors"
18
        "fmt"
19
        "strings"
20

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

27
func createAddMappingCmd() *cobra.Command {
28
        var params AddMappingParams
29
        cmd := &cobra.Command{
2✔
30
                Use:          "mapping",
2✔
31
                Short:        "Add (or modify) a mapping entry",
2✔
32
                Args:         MaxArgs(0),
2✔
33
                Example:      params.longHelp(),
2✔
34
                SilenceUsage: true,
2✔
35
                RunE: func(cmd *cobra.Command, args []string) error {
2✔
36
                        return RunAction(cmd, args, &params)
2✔
37
                },
4✔
38
        }
2✔
39
        cmd.Flags().StringVarP((*string)(&params.from), "from", "f", "", "map from subject (required)")
2✔
40
        cmd.Flags().StringVarP((*string)(&params.to.Subject), "to", "t", "", "to subject (required)")
41
        cmd.Flags().Uint8VarP(&params.to.Weight, "weight", "", 0, "weight [1-100] of this mapping entry (default: 100)")
2✔
42
        cmd.Flags().StringVarP(&params.to.Cluster, "cluster", "", "", "in which cluster this mapping should apply")
2✔
43
        params.AccountContextParams.BindFlags(cmd)
2✔
44

2✔
45
        return cmd
2✔
46
}
2✔
47

2✔
48
func init() {
49
        addCmd.AddCommand(createAddMappingCmd())
50
}
2✔
51

2✔
52
type AddMappingParams struct {
2✔
53
        AccountContextParams
54
        SignerParams
55
        from  jwt.Subject
56
        to    jwt.WeightedMapping
57
        claim *jwt.AccountClaims
58
}
59

60
func (p *AddMappingParams) longHelp() string {
61
        s := `toolName add mapping --from "a" --to "b"
62
# to modify an entry, say to set a weight after the fact
2✔
63
toolName add mapping --from "a" --to "b" --weight 50
2✔
64
# to add two entries from one subject, set weights and execute multiple times
2✔
65
toolName add mapping --from "a" --to "c" --weight 50
2✔
66
`
2✔
67
        return strings.Replace(s, "toolName", GetToolName(), -1)
2✔
68
}
2✔
69

2✔
70
func (p *AddMappingParams) SetDefaults(ctx ActionCtx) error {
2✔
71
        if err := p.AccountContextParams.SetDefaults(ctx); err != nil {
72
                return err
2✔
73
        }
2✔
UNCOV
74
        p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx)
×
UNCOV
75

×
76
        return nil
2✔
77
}
2✔
78

2✔
79
func (p *AddMappingParams) PreInteractive(ctx ActionCtx) error {
80
        return nil
81
}
×
UNCOV
82

×
UNCOV
83
func (p *AddMappingParams) Load(ctx ActionCtx) error {
×
84
        var err error
85

2✔
86
        if err = p.AccountContextParams.Validate(ctx); err != nil {
2✔
87
                return err
2✔
88
        }
2✔
UNCOV
89

×
UNCOV
90
        p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name)
×
91
        if err != nil {
92
                return err
2✔
93
        }
2✔
UNCOV
94

×
UNCOV
95
        return nil
×
96
}
97

2✔
98
func (p *AddMappingParams) PostInteractive(_ ActionCtx) error {
99
        return nil
100
}
×
UNCOV
101

×
UNCOV
102
func (p *AddMappingParams) Validate(ctx ActionCtx) error {
×
103

104
        var err error
2✔
105
        if p.from == "" {
2✔
106
                ctx.CurrentCmd().SilenceUsage = false
2✔
107
                return errors.New("from subject is required")
2✔
108
        }
×
UNCOV
109
        if p.to.Subject == "" {
×
110
                return errors.New("to subject is required")
×
111
        }
2✔
UNCOV
112

×
UNCOV
113
        if p.claim.Mappings == nil {
×
114
                p.claim.Mappings = jwt.Mapping{}
115
        }
4✔
116

2✔
117
        m := p.to
2✔
118
        if v, ok := (p.claim.Mappings)[p.from]; ok {
119
                set := false
2✔
120
                for i, w := range v {
4✔
121
                        if w.Subject == m.Subject && w.Cluster == m.Cluster {
2✔
122
                                v[i] = m
4✔
123
                                set = true
2✔
124
                                break
×
UNCOV
125
                        }
×
UNCOV
126
                }
×
127
                if !set {
128
                        p.claim.Mappings[p.from] = append(v, m)
129
                }
4✔
130
        } else {
2✔
131
                p.claim.Mappings[p.from] = []jwt.WeightedMapping{m}
2✔
132
        }
2✔
133
        var vr jwt.ValidationResults
2✔
134
        p.claim.Mappings.Validate(&vr)
2✔
135
        if vr.IsBlocking(true) {
2✔
136
                return fmt.Errorf("mapping validation failed: %v", vr.Errors())
2✔
137
        }
4✔
138

2✔
139
        if err = p.SignerParams.Resolve(ctx); err != nil {
2✔
140
                return fmt.Errorf("mapping %s", err)
141
        }
2✔
UNCOV
142

×
UNCOV
143
        return nil
×
144
}
145

2✔
146
func (p *AddMappingParams) Run(ctx ActionCtx) (store.Status, error) {
147
        token, err := p.claim.Encode(p.signerKP)
148
        if err != nil {
2✔
149
                return nil, err
2✔
150
        }
2✔
UNCOV
151

×
UNCOV
152
        r := store.NewDetailedReport(false)
×
153
        StoreAccountAndUpdateStatus(ctx, token, r)
154
        if r.HasNoErrors() {
2✔
155
                r.AddOK("added mapping %s -> %s, weight %d%%", p.from, p.to.Subject, p.to.GetWeight())
2✔
156
        }
4✔
157
        return r, err
2✔
158
}
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