forked from vmware-archive/fly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
targets.go
161 lines (133 loc) · 3.17 KB
/
targets.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package rc
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/concourse/atc"
"gopkg.in/yaml.v2"
)
var ErrNoTargetSpecified = errors.New("no target specified")
type UnknownTargetError struct {
TargetName TargetName
}
func (err UnknownTargetError) Error() string {
return fmt.Sprintf("unknown target: %s", err.TargetName)
}
type TargetProps struct {
API string `yaml:"api"`
TeamName string `yaml:"team"`
Insecure bool `yaml:"insecure,omitempty"`
Token *TargetToken `yaml:"token,omitempty"`
CACert string `yaml:"ca_cert,omitempty"`
}
type TargetToken struct {
Type string `yaml:"type"`
Value string `yaml:"value"`
}
type targetDetailsYAML struct {
Targets map[TargetName]TargetProps
}
func flyrcPath() string {
return filepath.Join(userHomeDir(), ".flyrc")
}
func DeleteTarget(targetName TargetName) error {
flyTargets, err := LoadTargets()
if err != nil {
return err
}
if _, ok := flyTargets.Targets[targetName]; ok {
delete(flyTargets.Targets, targetName)
}
return writeTargets(flyrcPath(), flyTargets)
}
func SaveTarget(
targetName TargetName,
api string,
insecure bool,
teamName string,
token *TargetToken,
caCert string,
) error {
flyTargets, err := LoadTargets()
if err != nil {
return err
}
flyrc := flyrcPath()
newInfo := flyTargets.Targets[targetName]
newInfo.API = api
newInfo.Insecure = insecure
newInfo.Token = token
newInfo.TeamName = teamName
newInfo.CACert = caCert
flyTargets.Targets[targetName] = newInfo
return writeTargets(flyrc, flyTargets)
}
func selectTarget(selectedTarget TargetName) (TargetProps, error) {
if selectedTarget == "" {
return TargetProps{}, ErrNoTargetSpecified
}
flyTargets, err := LoadTargets()
if err != nil {
return TargetProps{}, err
}
target, ok := flyTargets.Targets[selectedTarget]
if !ok {
return TargetProps{}, UnknownTargetError{selectedTarget}
}
return target, nil
}
func userHomeDir() string {
home := os.Getenv("HOME")
if home != "" {
return home
}
if runtime.GOOS == "windows" {
home = os.Getenv("USERPROFILE")
if home != "" {
return home
}
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home != "" {
return home
}
}
panic("could not detect home directory for .flyrc")
}
func LoadTargets() (*targetDetailsYAML, error) {
var flyTargets *targetDetailsYAML
flyrc := flyrcPath()
if _, err := os.Stat(flyrc); err == nil {
flyTargetsBytes, err := ioutil.ReadFile(flyrc)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(flyTargetsBytes, &flyTargets)
if err != nil {
return nil, err
}
}
if flyTargets == nil {
return &targetDetailsYAML{Targets: map[TargetName]TargetProps{}}, nil
}
for name, targetProps := range flyTargets.Targets {
if targetProps.TeamName == "" {
targetProps.TeamName = atc.DefaultTeamName
flyTargets.Targets[name] = targetProps
}
}
return flyTargets, nil
}
func writeTargets(configFileLocation string, targetsToWrite *targetDetailsYAML) error {
yamlBytes, err := yaml.Marshal(targetsToWrite)
if err != nil {
return err
}
err = ioutil.WriteFile(configFileLocation, yamlBytes, os.ModePerm)
if err != nil {
return err
}
return nil
}