-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_config.go
78 lines (65 loc) · 2.03 KB
/
plan_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package provider
import (
"encoding/json"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"gopkg.in/nullstone-io/nullstone.v0/workspaces"
"gopkg.in/yaml.v3"
"os"
"path"
"strconv"
)
var (
activeWorkspaceYmlFilename = path.Join(".nullstone", "active-workspace.yml")
nullstoneJsonFilename = ".nullstone.json"
)
type PlanConfig workspaces.Manifest
func (c PlanConfig) WorkspaceTarget() types.WorkspaceTarget {
return types.WorkspaceTarget{
StackId: c.StackId,
BlockId: c.BlockId,
EnvId: c.EnvId,
}
}
// LoadPlanConfig loads nullstone context for the current workspace
// Originally, this was in a file named `.nullstone.json`, but moved to `.nullstone/active-workspace.yml`
// As a result, this function will attempt the following:
// 1. Load `.nullstone/active-workspace.yml`
// 2. If not found, load `.nullstone.json`
// 3. Fall back to environment variables for each attribute
func LoadPlanConfig() (PlanConfig, error) {
c := planConfigFromEnv()
// Attempt to load .nullstone/active-workspace.yml
if file, err := os.Open(activeWorkspaceYmlFilename); err == nil {
decoder := yaml.NewDecoder(file)
err2 := decoder.Decode(&c)
file.Close()
return c, err2
}
// Attempt to load .nullstone.json
if file, err := os.Open(nullstoneJsonFilename); err == nil {
decoder := json.NewDecoder(file)
err2 := decoder.Decode(&c)
file.Close()
return c, err2
}
// Just rely on config from env if no plan config files
return c, nil
}
func planConfigFromEnv() PlanConfig {
return PlanConfig{
OrgName: os.Getenv("NULLSTONE_ORG_NAME"),
StackId: readIntFromEnvVars("NULLSTONE_STACK_ID"),
StackName: os.Getenv("NULLSTONE_STACK_NAME"),
BlockId: readIntFromEnvVars("NULLSTONE_BLOCK_ID"),
BlockName: os.Getenv("NULLSTONE_BLOCK_NAME"),
BlockRef: os.Getenv("NULLSTONE_BLOCK_REF"),
EnvId: readIntFromEnvVars("NULLSTONE_ENV_ID"),
EnvName: os.Getenv("NULLSTONE_ENV_NAME"),
}
}
func readIntFromEnvVars(name string) int64 {
if val, err := strconv.ParseInt(os.Getenv(name), 10, 64); err == nil {
return val
}
return 0
}