github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/env/env.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package env
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  
    25  	"github.com/dmotylev/goproperties"
    26  	"github.com/getgauge/common"
    27  	"github.com/getgauge/gauge/config"
    28  	"github.com/getgauge/gauge/logger"
    29  )
    30  
    31  var envVars map[string]string
    32  
    33  var currentEnv = "default"
    34  
    35  // LoadEnv first generates the map of the env vars that needs to be set.
    36  // It starts by populating the map with the env passed by the user in --env flag.
    37  // It then adds the default values of the env vars which are required by Gauge,
    38  // but are not present in the map.
    39  //
    40  // Finally, all the env vars present in the map are actually set in the shell.
    41  func LoadEnv(envName string) {
    42  	envVars = make(map[string]string)
    43  	currentEnv = envName
    44  
    45  	err := loadEnvDir(currentEnv)
    46  	if err != nil {
    47  		logger.Fatalf("Failed to load env. %s", err.Error())
    48  	}
    49  
    50  	if currentEnv != "default" {
    51  		err := loadEnvDir("default")
    52  		if err != nil {
    53  			logger.Fatalf("Failed to load env. %s", err.Error())
    54  		}
    55  	}
    56  
    57  	loadDefaultEnvVars()
    58  
    59  	err = setEnvVars()
    60  	if err != nil {
    61  		logger.Fatalf("Failed to load env. %s", err.Error())
    62  	}
    63  }
    64  
    65  func loadDefaultEnvVars() {
    66  	addEnvVar("gauge_reports_dir", "reports")
    67  	addEnvVar("overwrite_reports", "true")
    68  	addEnvVar("screenshot_on_failure", "true")
    69  	addEnvVar("logs_directory", "logs")
    70  }
    71  
    72  func loadEnvDir(envName string) error {
    73  	envDirPath := filepath.Join(config.ProjectRoot, common.EnvDirectoryName, envName)
    74  	if !common.DirExists(envDirPath) {
    75  		if envName != "default" {
    76  			return fmt.Errorf("%s environment does not exist", envName)
    77  		}
    78  		return nil
    79  	}
    80  
    81  	return filepath.Walk(envDirPath, loadEnvFile)
    82  }
    83  
    84  func loadEnvFile(path string, info os.FileInfo, err error) error {
    85  	if !isPropertiesFile(path) {
    86  		return nil
    87  	}
    88  
    89  	properties, err := properties.Load(path)
    90  	if err != nil {
    91  		return fmt.Errorf("Failed to parse: %s. %s", path, err.Error())
    92  	}
    93  
    94  	for property, value := range properties {
    95  		addEnvVar(property, value)
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  func addEnvVar(name, value string) {
   102  	if _, ok := envVars[name]; !ok {
   103  		envVars[name] = value
   104  	}
   105  }
   106  
   107  func isPropertiesFile(path string) bool {
   108  	return filepath.Ext(path) == ".properties"
   109  }
   110  
   111  func setEnvVars() error {
   112  	for name, value := range envVars {
   113  		if !isPropertySet(name) {
   114  			err := common.SetEnvVariable(name, value)
   115  			if err != nil {
   116  				return fmt.Errorf("%s", err.Error())
   117  			}
   118  		}
   119  	}
   120  	return nil
   121  }
   122  
   123  func isPropertySet(property string) bool {
   124  	return len(os.Getenv(property)) > 0
   125  }
   126  
   127  // CurrentEnv returns the value of currentEnv
   128  func CurrentEnv() string {
   129  	return currentEnv
   130  }