github.com/OpsMx/go-app-base@v0.0.24/birger/config.go (about)

     1  // Copyright 2022 OpsMx, Inc
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package birger
    16  
    17  import (
    18  	"log"
    19  	"os"
    20  )
    21  
    22  type Config struct {
    23  	URL                    string `json:"url,omitempty" yaml:"url,omitempty"`
    24  	Token                  string `json:"token,omitempty" yaml:"token,omitempty"`
    25  	UpdateFrequencySeconds int    `json:"updateFrequencySeconds,omitempty" yaml:"updateFrequencySeconds,omitempty"`
    26  }
    27  
    28  var defaultConfig = Config{
    29  	UpdateFrequencySeconds: 30,
    30  }
    31  
    32  func (cc *Config) applyDefaults() {
    33  	if cc.Token == "" {
    34  		t, found := os.LookupEnv("CONTROLLER_TOKEN")
    35  		if !found {
    36  			log.Fatal("no token in config, nor CONTROLLER_TOKEN envar")
    37  		}
    38  		cc.Token = t
    39  	}
    40  	if cc.UpdateFrequencySeconds == 0 {
    41  		cc.UpdateFrequencySeconds = defaultConfig.UpdateFrequencySeconds
    42  	}
    43  }