github.com/getgauge/gauge@v1.6.9/config/configuration.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package config 8 9 import ( 10 "os" 11 "strconv" 12 "strings" 13 "time" 14 15 "github.com/getgauge/common" 16 logging "github.com/op/go-logging" 17 ) 18 19 const ( 20 gaugeRepositoryURL = "gauge_repository_url" 21 runnerConnectionTimeout = "runner_connection_timeout" 22 pluginConnectionTimeout = "plugin_connection_timeout" 23 pluginKillTimeOut = "plugin_kill_timeout" 24 runnerRequestTimeout = "runner_request_timeout" 25 ideRequestTimeout = "ide_request_timeout" 26 checkUpdates = "check_updates" 27 allowInsecureDownload = "allow_insecure_download" 28 29 defaultRunnerConnectionTimeout = time.Second * 25 30 defaultPluginConnectionTimeout = time.Second * 10 31 defaultPluginKillTimeout = time.Second * 4 32 defaultRefactorTimeout = time.Second * 10 33 defaultRunnerRequestTimeout = time.Second * 30 34 defaultIdeRequestTimeout = time.Second * 30 35 LayoutForTimeStamp = "Jan 2, 2006 at 3:04pm" 36 ) 37 38 var APILog = logging.MustGetLogger("gauge-api") 39 var ProjectRoot string 40 41 // RunnerConnectionTimeout gets timeout in milliseconds for making a connection to the language runner 42 func RunnerConnectionTimeout() time.Duration { 43 intervalString := getFromConfig(runnerConnectionTimeout) 44 return convertToTime(intervalString, defaultRunnerConnectionTimeout, runnerConnectionTimeout) 45 } 46 47 // PluginConnectionTimeout gets timeout in milliseconds for making a connection to plugins 48 func PluginConnectionTimeout() time.Duration { 49 intervalString := getFromConfig(pluginConnectionTimeout) 50 return convertToTime(intervalString, defaultPluginConnectionTimeout, pluginConnectionTimeout) 51 } 52 53 // PluginKillTimeout gets timeout in milliseconds for a plugin to stop after a kill message has been sent 54 func PluginKillTimeout() time.Duration { 55 intervalString := getFromConfig(pluginKillTimeOut) 56 return convertToTime(intervalString, defaultPluginKillTimeout, pluginKillTimeOut) 57 } 58 59 // CheckUpdates determines if update check is enabled 60 func CheckUpdates() bool { 61 allow := getFromConfig(checkUpdates) 62 return convertToBool(allow, checkUpdates, true) 63 } 64 65 // RefactorTimeout returns the default timeout value for a refactoring request. 66 func RefactorTimeout() time.Duration { 67 return defaultRefactorTimeout 68 } 69 70 // Timeout in milliseconds for requests from the language runner. 71 func RunnerRequestTimeout() time.Duration { 72 intervalString := os.Getenv(runnerRequestTimeout) 73 if intervalString == "" { 74 intervalString = getFromConfig(runnerRequestTimeout) 75 } 76 return convertToTime(intervalString, defaultRunnerRequestTimeout, runnerRequestTimeout) 77 } 78 79 // Timeout in milliseconds for requests from the grpc language runner. 80 func IdeRequestTimeout() time.Duration { 81 intervalString := os.Getenv(ideRequestTimeout) 82 if intervalString == "" { 83 intervalString = getFromConfig(ideRequestTimeout) 84 } 85 return convertToTime(intervalString, defaultIdeRequestTimeout, ideRequestTimeout) 86 } 87 88 // AllowInsecureDownload determines if insecure download is enabled 89 func AllowInsecureDownload() bool { 90 allow := getFromConfig(allowInsecureDownload) 91 return convertToBool(allow, allowInsecureDownload, false) 92 } 93 94 // GaugeRepositoryUrl fetches the repository URL to locate plugins 95 func GaugeRepositoryUrl() string { 96 return getFromConfig(gaugeRepositoryURL) 97 } 98 99 // SetProjectRoot sets project root location in ENV. 100 func SetProjectRoot(args []string) error { 101 if ProjectRoot != "" { 102 return setCurrentProjectEnvVariable() 103 } 104 value := "" 105 if len(args) != 0 { 106 value = args[0] 107 } 108 root, err := common.GetProjectRootFromSpecPath(value) 109 if err != nil { 110 return err 111 } 112 ProjectRoot = root 113 return setCurrentProjectEnvVariable() 114 } 115 116 func setCurrentProjectEnvVariable() error { 117 return common.SetEnvVariable(common.GaugeProjectRootEnv, ProjectRoot) 118 } 119 120 func convertToTime(value string, defaultValue time.Duration, name string) time.Duration { 121 intValue, err := strconv.Atoi(value) 122 if err != nil { 123 APILog.Warningf("Incorrect value for %s in property file. Cannot convert %s to time", name, value) 124 return defaultValue 125 } 126 return time.Millisecond * time.Duration(intValue) 127 } 128 129 func convertToBool(value, property string, defaultValue bool) bool { 130 boolValue, err := strconv.ParseBool(strings.TrimSpace(value)) 131 if err != nil { 132 APILog.Warningf("Incorrect value for %s in property file. Cannot convert %s to boolean.", property, value) 133 return defaultValue 134 } 135 return boolValue 136 } 137 138 var getFromConfig = func(propertyName string) string { 139 config, err := common.GetGaugeConfigurationFor(common.GaugePropertiesFile) 140 if err != nil { 141 APILog.Warningf("Failed to get configuration from Gauge properties file. Error: %s", err.Error()) 142 return "" 143 } 144 return config[propertyName] 145 }