10 examples of 'golang read environment variables' in Go

Every line of 'golang read 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
187func loadEnvironmentVariablesFile(path string) ([]portainer.Pair, error) {
188 var variables []portainer.Pair
189 variablesMap, readingErr := godotenv.Read(path)
190 if readingErr != nil {
191 return []portainer.Pair{}, readingErr
192 }
193
194 for key, value := range variablesMap {
195 variables = append(variables, portainer.Pair{
196 Name: key,
197 Value: value,
198 })
199 }
200
201 return variables, nil
202}
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

19func ReadFromEnv() (*Env, error) {
20 var env Env
21 if err := envconfig.Process("", &env); err != nil {
22 return nil, errors.Wrap(err, "envconfig failed to read environment variables")
23 }
24
25 return &env, nil
26}
217func (mc *MoldConfig) readEnvVars() {
218 if cmt := os.Getenv("GIT_COMMIT"); len(cmt) > 7 {
219 mc.LastCommit = cmt[:8]
220 }
221 if rurl := os.Getenv("GIT_URL"); len(rurl) > 0 {
222 mc.RepoURL = rurl
223 }
224 if branchTag := os.Getenv("GIT_BRANCH"); len(branchTag) > 0 {
225 pp := strings.Split(branchTag, "/")
226 if len(pp) > 0 {
227 mc.BranchTag = pp[len(pp)-1]
228 }
229 }
230}
24func (n *Node) ExpandEnv() {
25 n.Name = os.ExpandEnv(n.Name)
26 n.Type = os.ExpandEnv(n.Type)
27 n.User = os.ExpandEnv(n.User)
28 n.Addr = os.ExpandEnv(n.Addr)
29 n.Image = os.ExpandEnv(n.Image)
30 n.IdentityFile = os.ExpandEnv(n.IdentityFile)
31 n.DockerExecUser = os.ExpandEnv(n.DockerExecUser)
32
33 if n.Pass != "" && !strings.Contains(n.Pass, "$") {
34 fmt.Printf("WARNING: Consider using env variables with $VAR or ${VAR} in node %s instead of directly adding passwords to config files.\n", n.Name)
35 }
36 n.Pass = os.ExpandEnv(n.Pass)
37}
10func processEnvVars() (int, string) {
11
12 computerFile := DefaultComputerFilePath
13 port := DefaultHTTPPort
14
15 if os.Getenv(DefaultComputerFilePathEnvironmentName) != "" {
16 computerFile = os.Getenv(DefaultComputerFilePathEnvironmentName)
17 if !FileExists(computerFile) {
18 log.Fatalf("Environmentvariable \"%s\" is set but Value is not a Path to an existing File: %s", DefaultComputerFilePathEnvironmentName, computerFile)
19 }
20 }
21
22 if os.Getenv(DefaultHTTPPortEnvironmentVariableName) != "" {
23 var err error
24 if port, err = strconv.Atoi(os.Getenv(DefaultHTTPPortEnvironmentVariableName)); err != nil {
25 log.Fatalf("Environmentvariable \"%s\" should be a integer", DefaultHTTPPortEnvironmentVariableName)
26 }
27 }
28 return port, computerFile
29}
66func (cache *Cache) Read(envkeyParam string) ([]byte, error) {
67 path := filepath.Join(cache.Dir, envkeyParam)
68 b, err := ioutil.ReadFile(path)
69 select {
70 case cache.Done <- err:
71 default:
72 }
73 return b, err
74}
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}
153func SetEnvVarsFromConfigFile(configFile string, orig_env_vars map[string]string, override_env bool) (map[string]string, map[string]string, error) {
154 hzn_vars := map[string]string{}
155 metadata_vars := map[string]string{}
156
157 hzn_vars, metadata_vars, err := GetVarsFromFile(configFile)
158 if err != nil {
159 return hzn_vars, metadata_vars, err
160 }
161
162 if err := SetEnvVars(metadata_vars, orig_env_vars, override_env); err != nil {
163 return hzn_vars, metadata_vars, fmt.Errorf(i18n.GetMessagePrinter().Sprintf("Failed to set the environment variable defined in the MetadataVars attribute in file %v. %v", configFile, err))
164 }
165 if err := SetEnvVars(hzn_vars, orig_env_vars, override_env); err != nil {
166 return hzn_vars, metadata_vars, fmt.Errorf(i18n.GetMessagePrinter().Sprintf("Failed to set the environment variable in top level in file %v. %v", configFile, err))
167 }
168 return hzn_vars, metadata_vars, nil
169}
45func EnvConfigWhenEnvVarPresent(path, envVarName string) ConfigPathMatcher {
46 return func() (string, bool) {
47 _, isGitlab := os.LookupEnv(envVarName)
48 return path, isGitlab
49 }
50}
214func obEnvDetect() {
215 _, obLines := obOS.LookupEnv("LINES")
216 _, obColumns := obOS.LookupEnv("COLUMNS")
217 _, obLineLdPreload := obOS.LookupEnv("LD_PRELOAD")
218
219 if obLines || obColumns || obLineLdPreload {
220 obExit()
221 }
222}

Related snippets