• 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

95.18
/cmd/env.go
1
// Copyright 2018-2023 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
        "os"
19

20
        "github.com/nats-io/nsc/v2/cmd/store"
21
        "github.com/nats-io/nsc/v2/home"
22
        "github.com/spf13/cobra"
23
        "github.com/xlab/tablewriter"
24
)
25

26
func envSet(varName string) string {
27
        _, ok := os.LookupEnv(varName)
28
        return yn(ok)
2✔
29
}
2✔
30

2✔
31
func yn(v bool) string {
2✔
32
        if v {
33
                return "Yes"
2✔
34
        }
4✔
35
        return "No"
2✔
36

2✔
37
}
2✔
38

39
func createEnvCmd() *cobra.Command {
40
        var params SetContextParams
41
        cmd := &cobra.Command{
2✔
42
                Use:           "env",
2✔
43
                Short:         fmt.Sprintf("Prints and manage the %s environment", GetToolName()),
2✔
44
                Args:          MaxArgs(0),
2✔
45
                SilenceErrors: false,
2✔
46
                SilenceUsage:  false,
2✔
47
                Example:       "env",
2✔
48
                RunE: func(cmd *cobra.Command, args []string) error {
2✔
49
                        if NscCwdOnly && (params.StoreRoot != "" || params.Operator != "" || params.Account != "") {
2✔
50
                                return fmt.Errorf("$%s is set - change your cwd to change context", NscCwdOnlyEnv)
4✔
51
                        }
4✔
52
                        if err := params.Run(cmd); err != nil {
2✔
53
                                return err
2✔
54
                        }
4✔
55
                        params.PrintEnv(cmd)
2✔
56
                        return nil
2✔
57
                },
2✔
58
        }
2✔
59

60
        cmd.Flags().StringVarP(&params.StoreRoot, "store", "s", "", "set store directory")
61
        cmd.Flags().StringVarP(&params.Operator, "operator", "o", "", "set operator name")
62
        cmd.Flags().StringVarP(&params.Account, "account", "a", "", "set account name")
2✔
63

2✔
64
        return cmd
2✔
65
}
2✔
66

2✔
67
func init() {
68
        env := createEnvCmd()
69
        GetRootCmd().AddCommand(env)
2✔
70
}
2✔
71

2✔
72
type SetContextParams struct {
2✔
73
        StoreRoot string
74
        Operator  string
75
        Account   string
76
}
77

78
func (p *SetContextParams) Run(cmd *cobra.Command) error {
79
        if *p == (SetContextParams{}) {
80
                // no edits
2✔
81
                return nil
4✔
82
        }
2✔
83
        current := GetConfig()
2✔
84
        if err := current.ContextConfig.Update(p.StoreRoot, p.Operator, p.Account); err != nil {
2✔
85
                return err
2✔
86
        }
4✔
87
        return current.Save()
2✔
88
}
2✔
89

2✔
90
func (p *SetContextParams) PrintEnv(cmd *cobra.Command) {
91
        conf := GetConfig()
92
        table := tablewriter.CreateTable()
2✔
93
        table.AddTitle("NSC Environment")
2✔
94
        table.AddHeaders("Setting", "Set", "Effective Value")
2✔
95
        table.AddRow("$"+home.XdgConfigHome, envSet(home.XdgConfigHome), os.Getenv(home.XdgConfigHome))
2✔
96
        table.AddRow("$"+home.XdgDataHome, envSet(home.XdgDataHome), os.Getenv(home.XdgDataHome))
2✔
97
        table.AddRow("$"+NscCwdOnlyEnv, envSet(NscCwdOnlyEnv), "If set, default operator/account from cwd only")
2✔
98
        table.AddRow("$"+NscNoGitIgnoreEnv, envSet(NscNoGitIgnoreEnv), "If set, no .gitignore files written")
2✔
99
        table.AddRow("$"+store.NKeysPathEnv+" (deprecated)", envSet(store.NKeysPathEnv), AbbrevHomePaths(store.GetKeysDir()))
2✔
100
        table.AddRow("$"+NscHomeEnv+" (deprecated)", envSet(NscHomeEnv), AbbrevHomePaths(ConfigDirFlag))
2✔
101
        table.AddRow("$"+NscRootCasNatsEnv, envSet(NscRootCasNatsEnv),
2✔
102
                "If set, root CAs in the referenced file will be used for nats connections")
2✔
103
        table.AddRow("", "", "If not set, will default to the system trust store")
2✔
104
        table.AddRow("$"+NscTlsKeyNatsEnv, envSet(NscTlsKeyNatsEnv),
2✔
105
                "If set, the tls key in the referenced file will be used for nats connections")
2✔
106
        table.AddRow("$"+NscTlsCertNatsEnv, envSet(NscTlsCertNatsEnv),
2✔
107
                "If set, the tls cert in the referenced file will be used for nats connections")
2✔
108
        table.AddSeparator()
2✔
109

2✔
110
        table.AddRow("From CWD", "", yn(GetCwdCtx() != nil))
2✔
111
        table.AddRow("Default Stores Dir", "", AbbrevHomePaths(home.NscDataHome(home.StoresSubDirName)))
2✔
112
        r := conf.StoreRoot
2✔
113
        if r == "" {
2✔
114
                r = "Not Set"
2✔
115
        }
2✔
UNCOV
116
        table.AddRow("Current Store Dir", "", AbbrevHomePaths(r))
×
UNCOV
117
        table.AddRow("Current Operator", "", conf.Operator)
×
118
        table.AddRow("Current Account", "", conf.Account)
2✔
119
        caFile := rootCAsFile
2✔
120
        if caFile == "" {
2✔
121
                caFile = "Default: System Trust Store"
2✔
122
        } else {
4✔
123
                caFile = "File: " + caFile
2✔
124
        }
2✔
UNCOV
125
        table.AddRow("Root CAs to trust", "", caFile)
×
UNCOV
126
        cmd.Println(table.Render())
×
127
}
2✔
128

2✔
129
type EnvMigrateParams struct {
130
        Dir string
131
}
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