-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
52 lines (46 loc) · 1.08 KB
/
target.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/tsuru/tsuru/cmd"
)
type targetSet struct{}
func (targetSet) Info() *cmd.Info {
return &cmd.Info{
Name: "target-set",
Usage: "target-set <target>",
Desc: "sets the remote tranor server",
MinArgs: 1,
}
}
func (targetSet) Run(ctx *cmd.Context, _ *cmd.Client) error {
err := downloadConfiguration(ctx.Args[0])
if err != nil {
return err
}
fmt.Fprintln(ctx.Stdout, "Target successfully defined!")
return nil
}
func downloadConfiguration(server string) error {
url := strings.TrimRight(server, "/") + "/config.json"
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
data, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("failed to download config file: %d - %s", resp.StatusCode, data)
}
config, err := parseConfig(resp.Body)
if err != nil {
return fmt.Errorf("invalid configuration returned by the remote target: %s", err)
}
err = writeConfigFile(config)
if err != nil {
return err
}
return config.writeTarget()
}