-
Notifications
You must be signed in to change notification settings - Fork 40
/
env.go
125 lines (106 loc) · 3.72 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of getgauge/html-report.
// getgauge/html-report is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// getgauge/html-report is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with getgauge/html-report. If not, see <http://www.gnu.org/licenses/>.
package env
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/getgauge/common"
)
const (
DefaultReportsDir = "reports"
GaugeReportsDirEnvName = "gauge_reports_dir" // directory where reports are generated by plugins
OverwriteReportsEnvProperty = "overwrite_reports"
UseNestedSpecs = "use_nested_specs"
SaveExecutionResult = "save_execution_result"
pluginKillTimeout = "plugin_kill_timeout"
)
func GetCurrentExecutableDir() (string, string) {
ex, err := os.Executable()
if err != nil {
log.Fatalf(err.Error())
}
target, err := filepath.EvalSymlinks(ex)
if err != nil {
return path.Dir(ex), filepath.Base(ex)
}
return filepath.Dir(target), filepath.Base(ex)
}
// CreateDirectory creates given directory if it doesn't exist
func CreateDirectory(dir string) {
if err := os.MkdirAll(dir, common.NewDirectoryPermissions); err != nil {
fmt.Printf("Failed to create directory %s: %s\n", dir, err)
os.Exit(1)
}
}
var GetProjectRoot = func() string {
projectRoot := os.Getenv(common.GaugeProjectRootEnv)
if projectRoot == "" {
fmt.Printf("Environment variable '%s' is not set. \n", common.GaugeProjectRootEnv)
os.Exit(1)
}
return projectRoot
}
func AddDefaultPropertiesToProject() {
defaultPropertiesFile := getDefaultPropertiesFile()
reportsDirProperty := &(common.Property{
Comment: "The path to the gauge reports directory. Should be either relative to the project directory or an absolute path",
Name: GaugeReportsDirEnvName,
DefaultValue: DefaultReportsDir})
overwriteReportProperty := &(common.Property{
Comment: "Set as false if gauge reports should not be overwritten on each execution. A new time-stamped directory will be created on each execution.",
Name: OverwriteReportsEnvProperty,
DefaultValue: "true"})
if !common.FileExists(defaultPropertiesFile) {
fmt.Printf("Failed to setup html report plugin in project. Default properties file does not exist at %s. \n", defaultPropertiesFile)
return
}
if err := common.AppendProperties(defaultPropertiesFile, reportsDirProperty, overwriteReportProperty); err != nil {
fmt.Printf("Failed to setup html report plugin in project: %s \n", err)
return
}
fmt.Println("Succesfully added configurations for html-report to env/default/default.properties")
}
func getDefaultPropertiesFile() string {
return filepath.Join(GetProjectRoot(), "env", "default", "default.properties")
}
func ShouldOverwriteReports() bool {
envValue := os.Getenv(OverwriteReportsEnvProperty)
if strings.ToLower(envValue) == "true" {
return true
}
return false
}
func ShouldUseNestedSpecs() bool {
envValue := os.Getenv(UseNestedSpecs)
if strings.ToLower(envValue) == "true" {
return true
}
return false
}
// PluginKillTimeout returns the plugin_kill_timeout in seconds
var PluginKillTimeout = func() int {
e := os.Getenv(pluginKillTimeout)
if e == "" {
return 0
}
v, err := strconv.Atoi(e)
if err != nil {
return 0
}
return v / 1000
}