github.com/kbehouse/nsc@v0.0.6/cmd/env.go (about) 1 /* 2 * Copyright 2018-2020 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 "os" 21 22 "github.com/kbehouse/nsc/cmd/store" 23 "github.com/spf13/cobra" 24 "github.com/xlab/tablewriter" 25 ) 26 27 func envSet(varName string) string { 28 _, ok := os.LookupEnv(varName) 29 return yn(ok) 30 } 31 32 func yn(v bool) string { 33 if v { 34 return "Yes" 35 } 36 return "No" 37 38 } 39 40 func createEnvCmd() *cobra.Command { 41 var params SetContextParams 42 cmd := &cobra.Command{ 43 Use: "env", 44 Short: fmt.Sprintf("Prints and manage the %s environment", GetToolName()), 45 Args: MaxArgs(0), 46 SilenceErrors: false, 47 SilenceUsage: false, 48 Example: "env", 49 RunE: func(cmd *cobra.Command, args []string) error { 50 if NscCwdOnly && (params.StoreRoot != "" || params.Operator != "" || params.Account != "") { 51 return fmt.Errorf("$%s is set - change your cwd to change context", NscCwdOnlyEnv) 52 } 53 if err := params.Run(cmd); err != nil { 54 return err 55 } 56 params.PrintEnv(cmd) 57 return nil 58 }, 59 } 60 61 cmd.Flags().StringVarP(¶ms.StoreRoot, "store", "s", "", "set store directory") 62 cmd.Flags().StringVarP(¶ms.Operator, "operator", "o", "", "set operator name") 63 cmd.Flags().StringVarP(¶ms.Account, "account", "a", "", "set account name") 64 65 return cmd 66 } 67 68 func init() { 69 GetRootCmd().AddCommand(createEnvCmd()) 70 } 71 72 type SetContextParams struct { 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 81 return nil 82 } 83 current := GetConfig() 84 if err := current.ContextConfig.Update(p.StoreRoot, p.Operator, p.Account); err != nil { 85 return err 86 } 87 return current.Save() 88 } 89 90 func (p *SetContextParams) PrintEnv(cmd *cobra.Command) { 91 conf := GetConfig() 92 table := tablewriter.CreateTable() 93 table.AddTitle("NSC Environment") 94 table.AddHeaders("Setting", "Set", "Effective Value") 95 table.AddRow("$"+NscCwdOnlyEnv, envSet(NscCwdOnlyEnv), "If set, default operator/account from cwd only") 96 table.AddRow("$"+NscNoGitIgnoreEnv, envSet(NscNoGitIgnoreEnv), "If set, no .gitignore files written") 97 table.AddRow("$"+store.NKeysPathEnv, envSet(store.NKeysPathEnv), AbbrevHomePaths(store.GetKeysDir())) 98 table.AddRow("$"+homeEnv, envSet(homeEnv), AbbrevHomePaths(toolHome)) 99 table.AddRow("Config", "", AbbrevHomePaths(conf.configFile())) 100 table.AddRow("$"+NscRootCasNatsEnv, envSet(NscRootCasNatsEnv), 101 "If set, root CAs in the referenced file will be used for nats connections") 102 table.AddRow("", "", "If not set, will default to the system trust store") 103 table.AddRow("$"+NscTlsKeyNatsEnv, envSet(NscTlsKeyNatsEnv), 104 "If set, the tls key in the referenced file will be used for nats connections") 105 table.AddRow("$"+NscTlsCertNatsEnv, envSet(NscTlsCertNatsEnv), 106 "If set, the tls cert in the referenced file will be used for nats connections") 107 table.AddSeparator() 108 r := conf.StoreRoot 109 if r == "" { 110 r = "Not Set" 111 } 112 table.AddRow("From CWD", "", yn(GetCwdCtx() != nil)) 113 table.AddRow("Stores Dir", "", AbbrevHomePaths(r)) 114 table.AddRow("Default Operator", "", conf.Operator) 115 table.AddRow("Default Account", "", conf.Account) 116 caFile := rootCAsFile 117 if caFile == "" { 118 caFile = "Default: System Trust Store" 119 } else { 120 caFile = "File: " + caFile 121 } 122 table.AddRow("Root CAs to trust", "", caFile) 123 cmd.Println(table.Render()) 124 }