-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (65 loc) · 1.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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/mitchellh/cli"
"github.com/pragkent/aliyun-disk/command"
_ "github.com/pragkent/aliyun-disk/logs"
"github.com/pragkent/aliyun-disk/volume"
)
func main() {
args := os.Args[1:]
meta := newMeta()
cli := &cli.CLI{
Args: args,
Commands: Commands(meta),
Version: Version,
HelpFunc: cli.BasicHelpFunc(Name),
HelpWriter: os.Stdout,
}
exitStatus, err := cli.Run()
if err != nil {
meta.Ui.Error(err.Error())
}
os.Exit(exitStatus)
}
func newMeta() *command.Meta {
ui := &cli.BasicUi{
Writer: os.Stdout,
ErrorWriter: os.Stderr,
Reader: os.Stdin,
}
driver := volume.NewDriver(getDriverConfig())
return &command.Meta{
Ui: ui,
Driver: driver,
}
}
func getDriverConfig() *volume.DriverConfig {
cfg := loadDriverConfig()
if cfg != nil {
return cfg
}
return &volume.DriverConfig{
Region: os.Getenv("ALIYUN_REGION"),
AccessKey: os.Getenv("ALIYUN_ACCESS_KEY"),
SecretKey: os.Getenv("ALIYUN_SECRET_KEY"),
Cluster: os.Getenv("ALIYUN_CLUSTER"),
}
}
func loadDriverConfig() *volume.DriverConfig {
const path = "/etc/kubernetes/cloud/cloud.json"
raw, err := ioutil.ReadFile(path)
if err != nil {
log.Printf("ioutil.ReadFile error: %v", err)
return nil
}
var cfg volume.DriverConfig
if err = json.Unmarshal(raw, &cfg); err != nil {
log.Printf("json.Unmarshal error: %v", err)
return nil
}
return &cfg
}