view config.go @ 5:af840bc25791

impl edit cmd
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 04 Dec 2020 18:27:09 +0900
parents 3032e9f78e4b
children c775cee5aac2
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 !existsFile(configFilePATH) {
		return "", fmt.Errorf("[ERROR] conf file not found")
	}
	return configFilePATH, nil
}