view config.go @ 3:3032e9f78e4b

add new cmd
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Tue, 01 Dec 2020 21:30:45 +0900
parents 76695bcbe426
children af840bc25791
line wrap: on
line source

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"`
	DailyPATH string `yaml:"daily_path"`
}

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()
}