Connecting Mizu to the application (#313)

This commit is contained in:
RoyUP9
2021-10-05 16:35:16 +03:00
committed by GitHub
parent 30fce5d765
commit d2d4ed5aee
15 changed files with 286 additions and 35 deletions

View File

@@ -54,16 +54,30 @@ func InitConfig(cmd *cobra.Command) error {
return nil
}
func GetConfigWithDefaults() (string, error) {
func GetConfigWithDefaults() (*ConfigStruct, error) {
defaultConf := ConfigStruct{}
if err := defaults.Set(&defaultConf); err != nil {
return "", err
return nil, err
}
configElem := reflect.ValueOf(&defaultConf).Elem()
setZeroForReadonlyFields(configElem)
return uiUtils.PrettyYaml(defaultConf)
return &defaultConf, nil
}
func WriteConfig(config *ConfigStruct) error {
template, err := uiUtils.PrettyYaml(config)
if err != nil {
return fmt.Errorf("failed converting config to yaml, err: %v", err)
}
data := []byte(template)
if err := ioutil.WriteFile(Config.ConfigFilePath, data, 0644); err != nil {
return fmt.Errorf("failed writing config, err: %v", err)
}
return nil
}
func mergeConfigFile(configFilePath string) error {

View File

@@ -21,6 +21,7 @@ type ConfigStruct struct {
Version configStructs.VersionConfig `yaml:"version"`
View configStructs.ViewConfig `yaml:"view"`
Logs configStructs.LogsConfig `yaml:"logs"`
Auth configStructs.AuthConfig `yaml:"auth"`
Config configStructs.ConfigConfig `yaml:"config,omitempty"`
AgentImage string `yaml:"agent-image,omitempty" readonly:""`
ImagePullPolicyStr string `yaml:"image-pull-policy" default:"Always"`

View File

@@ -0,0 +1,6 @@
package configStructs
type AuthConfig struct {
EnvName string `yaml:"env-name" default:"up9.app"`
Token string `yaml:"token"`
}

View File

@@ -3,6 +3,7 @@ package config_test
import (
"fmt"
"github.com/up9inc/mizu/cli/config"
"gopkg.in/yaml.v3"
"reflect"
"strings"
"testing"
@@ -15,10 +16,11 @@ func TestConfigWriteIgnoresReadonlyFields(t *testing.T) {
getFieldsWithReadonlyTag(configElem, &readonlyFields)
configWithDefaults, _ := config.GetConfigWithDefaults()
configWithDefaultsBytes, _ := yaml.Marshal(configWithDefaults)
for _, readonlyField := range readonlyFields {
t.Run(readonlyField, func(t *testing.T) {
readonlyFieldToCheck := fmt.Sprintf("\n%s:", readonlyField)
if strings.Contains(configWithDefaults, readonlyFieldToCheck) {
readonlyFieldToCheck := fmt.Sprintf(" %s:", readonlyField)
if strings.Contains(string(configWithDefaultsBytes), readonlyFieldToCheck) {
t.Errorf("unexpected result - readonly field: %v, config: %v", readonlyField, configWithDefaults)
}
})