code.oscarkilo.com/klex-git/config/config.go

..
config.go
// config deals with the klex.json file in the root of the client Git repo.
package config

import (
  "fmt"
  "io/ioutil"
  "os"
  "path"
  "strings"

  "oscarkilo.com/klex-git/util"
)

type Config struct {
  ProjectName string `json:"project_name"`
  OwnerUsername string `json:"owner_username"`
  ReaderUsername string `json:"reader_username"`
  DatasetsDir string `json:"datasets_dir"`
  FunctionsDir string `json:"functions_dir"`
  PipelinesDir string `json:"pipelines_dir"`
  KlexUrl string `json:"klex_url"`
  ApiKeyFile string `json:"api_key_file"`

  // GitRoot is the parent directory of klex.json.
  GitRoot string `json:"-"`

  // ApiKey is read from the location specified above.
  ApiKey string `json:"-"`
}

// ReadConfig reads the klex.json file from this Git repo's root.
func ReadConfig() (*Config, error) {
  root, err := util.FindRoot()
  if err != nil {
    return nil, err
  }
  file := path.Join(root, "klex.json")
  var config Config
  err = util.ReadJsonFile(file, &config)

  config.GitRoot = root

  if config.ApiKeyFile != "" {
    apiFile := path.Join(root, config.ApiKeyFile)
    buf, err := ioutil.ReadFile(apiFile)
    if err != nil {
      return nil, err
    }
    config.ApiKey = strings.TrimSpace(string(buf))
  }
  if os.Getenv("KLEX_API_KEY") != "" {
    if config.ApiKey != "" {
      fmt.Printf("KLEX_API_KEY env var overrides api_key_file in klex.json\n")
    }
    config.ApiKey = os.Getenv("KLEX_API_KEY")
  }
  if config.ApiKey == "" {
    return nil, fmt.Errorf("Failed to read api_key_file='%s' and KLEX_API_KEY env var", config.ApiKeyFile)
  }

  return &config, err
}