OSDN Git Service

move oss (#585)
[bytom/vapor.git] / toolbar / osssync / upload / config.go
1 package upload
2
3 import (
4         "encoding/json"
5         "os"
6
7         "github.com/bytom/vapor/errors"
8 )
9
10 // Config represent root of config
11 type Config struct {
12         OssConfig *OssConfig `json:"oss_config"`
13         VaporURL  string     `json:"vapor_url"`
14 }
15
16 // Oss logs cfg
17 type Login struct {
18         Endpoint        string `json:"endpoint"`
19         AccessKeyID     string `json:"access_key_id"`
20         AccessKeySecret string `json:"access_key_secret"`
21 }
22
23 // Oss cfg
24 type OssConfig struct {
25         Login     *Login `json:"login"`
26         Bucket    string `json:"bucket"`
27         Directory string `json:"directory"`
28 }
29
30 // LoadConfig read path file to the config object for Upload from Vapor to OSS
31 func LoadConfig(config interface{}) error {
32         if len(os.Args) <= 1 {
33                 return errors.New("Please setup the config file path as Args[1]")
34         }
35         return LoadConfigByPath(os.Args[1], config)
36 }
37
38 // LoadConfigByPath read path file to the config object
39 func LoadConfigByPath(path string, config interface{}) error {
40         configFile, err := os.Open(path)
41         if err != nil {
42                 return errors.Wrap(err, "fail to open config file")
43         }
44
45         defer configFile.Close()
46         return json.NewDecoder(configFile).Decode(config)
47 }