view config.go @ 14:1e21b0c29775

remove create page msg
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Thu, 17 Dec 2020 11:12:04 +0900
parents c775cee5aac2
children
line wrap: on
line source

package growsync

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/goccy/go-yaml"
	"golang.org/x/xerrors"
)

type growiConfig struct {
	URL       string `yaml:"growi_url"`
	UserName  string `yaml:"user_name"`
	TOKEN     string `yaml:"token"`
	LocalRoot string `yaml:"local_root"`
	DailyPATH string `yaml:"daily_path"`
}

func parseConfig() (*growiConfig, error) {
	configFilePATH, _ := getConfingPATH()
	configBytes, err := ioutil.ReadFile(configFilePATH)
	if err != nil {
		return nil, xerrors.Errorf("failed read %s : %+w", configFilePATH, err)
	}
	config := growiConfig{}

	if err := yaml.Unmarshal(configBytes, &config); err != nil {
		return nil, xerrors.Errorf("failed unmarshal yaml at %s, %+w", configFilePATH, err)
	}
	return &config, nil
}

func getConfingPATH() (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", xerrors.Errorf("[error] failed get user homedir %+w", err)
	}
	configFilePATH := filepath.Join(home, ".config", "growsync", "config.yaml")
	if !existsFile(configFilePATH) {
		return "", fmt.Errorf("[ERROR] conf file not found")
	}
	return configFilePATH, nil
}