-
Notifications
You must be signed in to change notification settings - Fork 1
/
setenv.go
61 lines (54 loc) · 1.27 KB
/
setenv.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
package main
import (
"fmt"
"github.com/mitchellh/go-homedir"
"github.com/orange-cloudfoundry/cfm/messages"
"io/ioutil"
"path/filepath"
)
type SetEnvCmd struct {
Target string `long:"target" short:"t" description:"Cloud foundry target"`
Alias string `long:"alias" short:"a" description:"alias of the target to use"`
Group string `long:"group" short:"g" description:"the group of targets to use"`
}
var setEnvCmd SetEnvCmd
func (c *SetEnvCmd) Execute(_ []string) error {
targets := findAllTargets()
group := c.Group
if group == "" {
group = getGroup()
}
var target Target
for _, t := range targets {
if (t.Api == c.Target || t.Alias == c.Alias) && t.Group == group {
target = t
break
}
}
if target.Api == "" {
return fmt.Errorf("Target not found")
}
b, err := ioutil.ReadFile(filepath.Join(convertToFolder(target), ".cf", "config.json"))
if err != nil {
return err
}
return ioutil.WriteFile(cfConfFile(), b, 0644)
}
func init() {
desc := `set your cf normal cli to targeted cloud foundry`
_, err := parser.AddCommand(
"set-cf-env",
desc,
desc,
&setEnvCmd)
if err != nil {
panic(err)
}
}
func cfConfFile() string {
h, err := homedir.Dir()
if err != nil {
messages.Fatal(err.Error())
}
return filepath.Join(h, ".cf", "config.json")
}