diff --git a/README.md b/README.md index 18aad650..317a84cd 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,30 @@ Visit the [docs](https://docs.cast.ai/docs/getting-started) to connect your clus The helm chart for the CAST AI Kubernetes agent is published in the [castai/helm-charts](https://github.com/castai/helm-charts) repo. + +## Reading configuration from file + +You can pass configuration to agent via YAML file. + +Example file +```yaml +api: + key: "api key" + url: "api.cast.ai" +``` +or + +```yaml +api.key: "api key" +api.url: "api.cast.ai" +``` + +Shell example + +```shell +CONFIG_PATH= ./castai-agent +``` + ## Contributing ### Run the agent in your IDE diff --git a/internal/config/config.go b/internal/config/config.go index 1801c8e7..9c845b9b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "os" "strings" "sync" "time" @@ -150,6 +151,16 @@ func Get() Config { cfg = &Config{} bindEnvs(*cfg) + + if cfgFile := os.Getenv("CONFIG_PATH"); cfgFile != "" { + fmt.Println("Using config from a file", cfgFile) + viper.SetConfigType("yaml") + viper.SetConfigFile(cfgFile) + if err := viper.ReadInConfig(); err != nil { + panic(fmt.Errorf("reading default config: %v", err)) + } + } + if err := viper.Unmarshal(&cfg); err != nil { panic(fmt.Errorf("parsing configuration: %v", err)) }