-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
114 lines (102 loc) · 2.43 KB
/
main.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
package main
import (
"bytes"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/mitchellh/go-homedir"
"github.com/orange-cloudfoundry/cfm/messages"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
const cfmHomeDir = ".cfm"
type Options struct {
Help func() `long:"help" short:"h" description:""`
}
var options Options
var parser = flags.NewParser(&options, flags.HelpFlag|flags.PassDoubleDash|flags.IgnoreUnknown)
func help() {
cmd := exec.Command("cf", os.Args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func main() {
options.Help = help
os.MkdirAll(cfmHome(), 0755)
_, err := exec.LookPath("cf")
if err != nil {
messages.Fatal("Can't found cf cli please install before: https://github.com/cloudfoundry/cli")
}
_, err = parser.ParseArgs(os.Args[1:])
if err == nil {
return
}
if err != nil {
errFlag, ok := err.(*flags.Error)
if !ok || errFlag.Type != flags.ErrUnknownCommand && errFlag.Type != flags.ErrUnknownFlag {
if os.Args[1] == "--help" || os.Args[1] == "-h" {
help()
fmt.Println(err.Error())
return
}
messages.Fatal(err.Error())
}
}
if os.Args[1] == "--help" || os.Args[1] == "-h" {
return
}
targets := findTargets()
if len(targets) == 0 {
messages.Fatal("please set targets with command set-targets: cfm set-targets https://my.cf.1.com https://my.cf.2.com")
}
stdin := createStdin()
inError := false
for _, target := range targets {
messages.Printf("Runnning command on cf api '%s':\n", messages.C.Cyan(target))
err := runCfCommand(os.Args[1:], convertToFolder(target), stdin)
if err != nil {
inError = true
}
messages.Println("-------")
messages.Println("")
if stdinReader, ok := stdin.(*bytes.Reader); ok {
stdinReader.Seek(0, 0)
}
}
if inError {
os.Exit(1)
}
}
func cfmHome() string {
h, err := homedir.Dir()
if err != nil {
messages.Fatal(err.Error())
}
return filepath.Join(h, cfmHomeDir)
}
func createStdin() io.Reader {
stat, err := os.Stdin.Stat()
if err != nil {
return &bytes.Buffer{}
}
if stat.Size() == 0 {
return &bytes.Buffer{}
}
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return &bytes.Buffer{}
}
return bytes.NewReader(b)
}
func runCfCommand(args []string, configDir string, stdin io.Reader) error {
cmd := exec.Command("cf", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = stdin
env := append(os.Environ(), fmt.Sprintf("CF_HOME=%s", configDir))
cmd.Env = env
return cmd.Run()
}