Mercurial > hg > Members > anatofuz > growsync
diff config.go @ 1:76695bcbe426
write cmd
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 01 Dec 2020 20:30:06 +0900 |
parents | |
children | 3032e9f78e4b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.go Tue Dec 01 20:30:06 2020 +0900 @@ -0,0 +1,51 @@ +package growsync + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "github.com/goccy/go-yaml" +) + +type growiConfig struct { + url string `yaml:"growi_url"` + userName string `yaml:"user_name"` + token string `yaml:"token"` + localRoot string `yaml:"local_root"` +} + +func parseConfig() (*growiConfig, error) { + configFilePATH, _ := getConfingPATH() + configBytes, err := ioutil.ReadFile(configFilePATH) + if err != nil { + return nil, err + } + config := growiConfig{} + + if err := yaml.Unmarshal(configBytes, &config); err != nil { + return nil, err + } + return &config, nil +} + +func getConfingPATH() (string, error) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + configFilePATH := filepath.Join(home, ".config", "growsync", "config.yaml") + if !fileCheck(configFilePATH) { + return "", fmt.Errorf("[ERROR] conf file not found") + } + return configFilePATH, nil +} + +func fileCheck(conf string) bool { + info, err := os.Stat(conf) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +}