package config // package serves to store/load config files for server import ( _ "fmt" "github.com/spf13/viper" "log" "os/exec" "bytes" "strings" ) type serverconfig struct { URL string Token string Bucket string Orginization string } func ReadServerConfig() *serverconfig { viper.SetConfigName("database") viper.SetConfigType("yaml") viper.AddConfigPath("../../internal/configs") viper.SetDefault("Orginization","ForeLight") viper.SetDefault("URL","http://localhost:8086") var C serverconfig err := viper.Unmarshal(&C) if err != nil { log.Fatal(err) } if C.Token == "" { // token unset cmd := exec.Command("cat","admin_token") var out bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &out cmd.Stderr = &stderr if err := cmd.Run(); err != nil { log.Fatal(err) } outstring := out.String() C.Token = strings.Trim(outstring," \n") viper.Set("token",C.Token) viper.WriteConfig() } return &C } func (s *serverconfig) GetUrl() string { return s.URL } func (s *serverconfig) GetOrg() string { return s.Orginization } func (s *serverconfig) GetBucket() string { return s.Bucket } func (s *serverconfig) GetToken() string { return s.Token }