10 examples of 'golang get environment variables' in Go

Every line of 'golang get environment variables' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
71func addEnvironmentVariable(cmd *cobra.Command, args []string) {
72 var err error
73 if len(args) != 5 {
74 sdk.Exit("Wrong usage: %s\n", cmd.Short)
75 }
76 projectKey := args[0]
77 envName := args[1]
78 varName := args[2]
79 varValue := args[3]
80 varType := args[4]
81
82 if *force {
83 variables, errSh := sdk.ShowEnvironmentVariable(projectKey, envName)
84 if errSh != nil {
85 sdk.Exit("Error: cannot show existing variables for environment %s (%s)\n", envName, err)
86 }
87
88 varExist := false
89 for _, v := range variables {
90 if v.Name == varName {
91 varExist = true
92 break
93 }
94 }
95 if !varExist {
96 err = sdk.AddEnvironmentVariable(projectKey, envName, varName, varValue, varType)
97 } else {
98 err = sdk.UpdateEnvironmentVariable(projectKey, envName, varName, varName, varValue, varType)
99 }
100
101 } else {
102 err = sdk.AddEnvironmentVariable(projectKey, envName, varName, varValue, varType)
103 }
104
105 if err != nil {
106 sdk.Exit("Error: cannot add variable %s in environment %s (%s)\n", varName, envName, err)
107 }
108 fmt.Printf("OK\n")
109}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

18func Environ(c *yaml.Config, envs map[string]string) error {
19 var images []*yaml.Container
20 images = append(images, c.Pipeline...)
21 images = append(images, c.Services...)
22
23 for _, p := range images {
24 if p.Environment == nil {
25 p.Environment = map[string]string{}
26 }
27 for k, v := range envs {
28 if v == "" {
29 continue
30 }
31 p.Environment[k] = v
32 }
33 if httpProxy != "" {
34 p.Environment["HTTP_PROXY"] = httpProxy
35 p.Environment["http_proxy"] = strings.ToUpper(httpProxy)
36 }
37 if httpsProxy != "" {
38 p.Environment["HTTPS_PROXY"] = httpsProxy
39 p.Environment["https_proxy"] = strings.ToUpper(httpsProxy)
40 }
41 if noProxy != "" {
42 p.Environment["NO_PROXY"] = noProxy
43 p.Environment["no_proxy"] = strings.ToUpper(noProxy)
44 }
45 }
46 return nil
47}
18func getEnv() map[string]string {
19 env := make(map[string]string)
20 for _, kv := range os.Environ() {
21 parts := strings.SplitN(kv, "=", 2)
22 env[parts[0]] = parts[1]
23 }
24 return env
25}
26func getEnv() map[string]interface{} {
27 env := os.Environ()
28 res := make(map[string]interface{}, len(env))
29
30 for _, s := range env {
31 for j := 0; j < len(s); j++ {
32 if s[j] == '=' {
33 key, value := s[:j], s[j+1:]
34 res[key] = value
35 break
36 }
37 }
38 }
39 return res
40}
15func envAction(c *cli.Context) error {
16 cfg := config.LoadConfig()
17
18 args := c.Args()
19 if len(args) == 0 {
20 return nil
21 }
22 osEnv := os.Environ()
23
24 envMap := make(map[string]string, len(cfg.Rancher.Environment)+len(osEnv))
25 for k, v := range cfg.Rancher.Environment {
26 envMap[k] = v
27 }
28 for k, v := range util.KVPairs2Map(osEnv) {
29 envMap[k] = v
30 }
31
32 if cmd, err := exec.LookPath(args[0]); err != nil {
33 log.Fatal(err)
34 } else {
35 args[0] = cmd
36 }
37 if err := syscall.Exec(args[0], args, util.Map2KVPairs(envMap)); err != nil {
38 log.Fatal(err)
39 }
40
41 return nil
42}
297func getEnv64(vcInstallDir string) []string {
298 return getEnvForVcTools(vcInstallDir, "x86_amd64")
299}
26func getEnvFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
27 if args == nil || args.Len() == 0 {
28 return starlark.None, nil
29 }
30 key, ok := args.Index(0).(starlark.String)
31 if !ok {
32 return starlark.None, fmt.Errorf("os.getenv: invalid env key")
33 }
34
35 return starlark.String(os.Getenv(string(key))), nil
36}
217func env(name string, val interface{}) string {
218 return fmt.Sprintf("%s=%v", name, val)
219}
182func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
183 var sshOpts []string
184
185 if sshKeyFile != "" {
186 // We have an SSH key temp file configured, tell ssh about this.
187 sshOpts = append(sshOpts, "-i", sshKeyFile)
188 }
189
190 cmd.Env = append(os.Environ(),
191 // Set the ssh command to use for clones.
192 "GIT_SSH_COMMAND=ssh "+strings.Join(sshOpts, " "),
193 )
194}
49func Getenv(key string) string {
50 v, _ := Getenverror(key)
51 return v
52}

Related snippets