2020-02-24 18:07:27 +03:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
2020-02-26 16:42:32 +03:00
|
|
|
"strings"
|
2020-02-24 18:07:27 +03:00
|
|
|
|
|
|
|
conf "github.com/micro/go-micro/v2/config"
|
|
|
|
"github.com/micro/go-micro/v2/config/source/file"
|
2020-02-28 15:58:27 +03:00
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2020-02-24 18:07:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileName for global micro config
|
|
|
|
const FileName = ".micro"
|
|
|
|
|
2020-02-28 15:58:27 +03:00
|
|
|
// config is a singleton which is required to ensure
|
|
|
|
// each function call doesn't load the .micro file
|
|
|
|
// from disk
|
|
|
|
var config = newConfig()
|
|
|
|
|
2020-02-24 18:07:27 +03:00
|
|
|
// Get a value from the .micro file
|
2020-03-31 15:30:14 +03:00
|
|
|
func Get(path ...string) (string, error) {
|
|
|
|
tk := config.Get(path...).String("")
|
2020-02-28 15:58:27 +03:00
|
|
|
return strings.TrimSpace(tk), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a value in the .micro file
|
2020-03-31 15:30:14 +03:00
|
|
|
func Set(value string, path ...string) error {
|
2020-02-24 18:07:27 +03:00
|
|
|
// get the filepath
|
|
|
|
fp, err := filePath()
|
|
|
|
if err != nil {
|
2020-02-28 15:58:27 +03:00
|
|
|
return err
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 15:58:27 +03:00
|
|
|
// set the value
|
2020-03-31 15:30:14 +03:00
|
|
|
config.Set(value, path...)
|
2020-02-24 18:07:27 +03:00
|
|
|
|
2020-02-28 15:58:27 +03:00
|
|
|
// write to the file
|
|
|
|
return ioutil.WriteFile(fp, config.Bytes(), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func filePath() (string, error) {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
2020-02-24 18:07:27 +03:00
|
|
|
return "", err
|
|
|
|
}
|
2020-02-28 15:58:27 +03:00
|
|
|
return filepath.Join(usr.HomeDir, FileName), nil
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 15:58:27 +03:00
|
|
|
// newConfig returns a loaded config
|
|
|
|
func newConfig() conf.Config {
|
2020-02-24 18:07:27 +03:00
|
|
|
// get the filepath
|
|
|
|
fp, err := filePath()
|
|
|
|
if err != nil {
|
2020-02-28 15:58:27 +03:00
|
|
|
log.Error(err)
|
|
|
|
return conf.DefaultConfig
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the file if it does not exist
|
|
|
|
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
|
|
|
ioutil.WriteFile(fp, []byte{}, 0644)
|
2020-02-24 21:07:11 +03:00
|
|
|
} else if err != nil {
|
2020-02-28 15:58:27 +03:00
|
|
|
log.Error(err)
|
|
|
|
return conf.DefaultConfig
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a new config
|
|
|
|
c, err := conf.NewConfig(
|
|
|
|
conf.WithSource(
|
|
|
|
file.NewSource(
|
|
|
|
file.WithPath(fp),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-02-28 15:58:27 +03:00
|
|
|
log.Error(err)
|
|
|
|
return conf.DefaultConfig
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// load the config
|
|
|
|
if err := c.Load(); err != nil {
|
2020-02-28 15:58:27 +03:00
|
|
|
log.Error(err)
|
|
|
|
return conf.DefaultConfig
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 15:58:27 +03:00
|
|
|
// return the conf
|
|
|
|
return c
|
2020-02-24 18:07:27 +03:00
|
|
|
}
|