...

Source file src/github.com/okta/okta-jwt-verifier-golang/utils/parseEnv.go

Documentation: github.com/okta/okta-jwt-verifier-golang/utils

     1  /*******************************************************************************
     2   * Copyright 2018 - Present Okta, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   ******************************************************************************/
    16  
    17  package utils
    18  
    19  import (
    20  	"bufio"
    21  	"log"
    22  	"os"
    23  	"strings"
    24  )
    25  
    26  func ParseEnvironment() {
    27  	if _, err := os.Stat(".env"); os.IsNotExist(err) {
    28  		log.Printf("Environment Variable file (.env) is not present.  Relying on Global Environment Variables")
    29  	}
    30  
    31  	setEnvVariable("CLIENT_ID", os.Getenv("CLIENT_ID"))
    32  	setEnvVariable("ISSUER", os.Getenv("ISSUER"))
    33  	setEnvVariable("USERNAME", os.Getenv("USERNAME"))
    34  	setEnvVariable("PASSWORD", os.Getenv("PASSWORD"))
    35  	if os.Getenv("CLIENT_ID") == "" {
    36  		log.Printf("Could not resolve a CLIENT_ID environment variable.")
    37  		os.Exit(1)
    38  	}
    39  
    40  	if os.Getenv("ISSUER") == "" {
    41  		log.Printf("Could not resolve a ISSUER environment variable.")
    42  		os.Exit(1)
    43  	}
    44  }
    45  
    46  func setEnvVariable(env string, current string) {
    47  	if current != "" {
    48  		return
    49  	}
    50  
    51  	file, _ := os.Open(".env")
    52  	defer file.Close()
    53  
    54  	lookInFile := bufio.NewScanner(file)
    55  	lookInFile.Split(bufio.ScanLines)
    56  
    57  	for lookInFile.Scan() {
    58  		parts := strings.Split(lookInFile.Text(), "=")
    59  		key, value := parts[0], parts[1]
    60  		if key == env {
    61  			os.Setenv(key, value)
    62  		}
    63  	}
    64  }
    65  

View as plain text