diff --git a/conf/public_conf.go b/conf/public_conf.go new file mode 100644 index 0000000..56153da --- /dev/null +++ b/conf/public_conf.go @@ -0,0 +1,56 @@ +package conf + +import ( + "log" + + "github.com/spf13/viper" +) + +const ( + linuxPath = "/etc/wheat-cache/" + + devPath = "./conf" + devPathBin = "../conf" +) + +func init() { + setDefaultConfValue() + err := LoadConf("") + switch err.(type) { + case nil: + case viper.ConfigFileNotFoundError: + formatPath := []string{linuxPath, devPath, devPath} + log.Fatalf("The profile could not be read, read path:%v", formatPath) + default: + log.Fatalf("The resolution of the profile failed, err: %v", err) + + } +} + +func setDefaultConfValue() { + // 设置一些默认值 + viper.Set("version", "base-01") +} + +func LoadConf(path string) error { + if path != "" { + viper.AddConfigPath(path) + } + viper.SetConfigName("wheat-cache") + + // 添加默认读取地址 + // linux + viper.AddConfigPath(linuxPath) + + // 开发环境 + viper.AddConfigPath(devPath) + viper.AddConfigPath(devPathBin) + + viper.SetConfigType("yaml") + + err := viper.ReadInConfig() + if err != nil { + return err + } + return nil +} diff --git a/conf/wheat-cache.yaml b/conf/wheat-cache.yaml new file mode 100644 index 0000000..4475627 --- /dev/null +++ b/conf/wheat-cache.yaml @@ -0,0 +1,8 @@ +version: 'v1.0' + +env: 'dev' + +storage: + host: '127.0.0.1' + port: 5890 + diff --git a/go.mod b/go.mod index d654f1b..8dd54cf 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.16 require ( github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.8.1 + github.com/stretchr/testify v1.7.0 + golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 ) diff --git a/go.sum b/go.sum index 795c424..4cb7eeb 100644 --- a/go.sum +++ b/go.sum @@ -397,8 +397,9 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg= +golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/makefile b/makefile index 93023d6..b4525c1 100644 --- a/makefile +++ b/makefile @@ -3,8 +3,12 @@ STORAGE_PATH = $(BASE_PATH)/storage BASE_OUT = $(BASE_PATH)/bin dcgen: - python3 ./shell/proto.py + @python3 ./shell/proto.py .PHONY : build build: - cd storage && go build -o $(BASE_OUT)/storage \ No newline at end of file + @cd storage && go build -o $(BASE_OUT)/storage + +.PHONY: install +install: + @make build \ No newline at end of file