/
config.go
58 lines (48 loc) · 2.03 KB
/
config.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
package tart
import (
"errors"
"fmt"
"github.com/caarlos0/env/v8"
)
var ErrConfigFromEnvironmentFailed = errors.New("failed to load config from environment")
const (
// GitLab CI/CD environment adds "CUSTOM_ENV_" prefix[1] to prevent
// conflicts with system environment variables.
//
// [1]: https://docs.gitlab.com/runner/executors/custom.html#stages
envPrefixGitLabRunner = "CUSTOM_ENV_"
// The prefix that we use to avoid confusion with Cirrus CI Cloud variables
// and remove repetition from the Config's struct declaration.
envPrefixTartExecutor = "TART_EXECUTOR_"
// EnvTartExecutorInternalBuildsDir is an internal environment variable
// that does not use the "CUSTOM_ENV_" prefix, thus preventing the override
// by the user.
EnvTartExecutorInternalBuildsDir = "TART_EXECUTOR_INTERNAL_BUILDS_DIR"
// EnvTartExecutorInternalCacheDir is an internal environment variable
// that does not use the "CUSTOM_ENV_" prefix, thus preventing the override
// by the user.
EnvTartExecutorInternalCacheDir = "TART_EXECUTOR_INTERNAL_CACHE_DIR"
)
type Config struct {
SSHUsername string `env:"SSH_USERNAME" envDefault:"admin"`
SSHPassword string `env:"SSH_PASSWORD" envDefault:"admin"`
SSHPort uint16 `env:"SSH_PORT" envDefault:"22"`
Softnet bool `env:"SOFTNET"`
Headless bool `env:"HEADLESS" envDefault:"true"`
AlwaysPull bool `env:"ALWAYS_PULL" envDefault:"true"`
InsecurePull bool `env:"INSECURE_PULL" envDefault:"false"`
PullConcurrency uint8 `env:"PULL_CONCURRENCY"`
HostDir bool `env:"HOST_DIR"`
Shell string `env:"SHELL"`
InstallGitlabRunner string `env:"INSTALL_GITLAB_RUNNER"`
Timezone string `env:"TIMEZONE"`
}
func NewConfigFromEnvironment() (Config, error) {
var config Config
if err := env.ParseWithOptions(&config, env.Options{
Prefix: envPrefixGitLabRunner + envPrefixTartExecutor,
}); err != nil {
return config, fmt.Errorf("%w: %v", ErrConfigFromEnvironmentFailed, err)
}
return config, nil
}