Skip to content
Snippets Groups Projects
Select Git revision
  • c1e005117170afb36ff7803af8c9a9732171e33e
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

config.go

Blame
    • Kevin Lyda's avatar
      c1e00511
      Add auto_token client option · c1e00511
      Kevin Lyda authored
      Add an auto_token client option to change the flow such that the
      token will be sent back to the client directly by the browser.
      There are a number of "TODO" messages in this commit which will
      hopefully be addressed in code review. I'm not sure if they're
      needed.
      c1e00511
      History
      Add auto_token client option
      Kevin Lyda authored
      Add an auto_token client option to change the flow such that the
      token will be sent back to the client directly by the browser.
      There are a number of "TODO" messages in this commit which will
      hopefully be addressed in code review. I'm not sure if they're
      needed.
    config.go 1.48 KiB
    package client
    
    import (
    	"os"
    
    	"github.com/mitchellh/go-homedir"
    	"github.com/spf13/pflag"
    	"github.com/spf13/viper"
    )
    
    // Config holds the client configuration.
    type Config struct {
    	CA                     string `mapstructure:"ca"`
    	Keytype                string `mapstructure:"key_type"`
    	Keysize                int    `mapstructure:"key_size"`
    	Validity               string `mapstructure:"validity"`
    	ValidateTLSCertificate bool   `mapstructure:"validate_tls_certificate"`
    	PublicFilePrefix       string `mapstructure:"key_file_prefix"`
    	AutoToken              bool   `mapstructure:"auto_token"`
    }
    
    func setDefaults() {
    	viper.BindPFlag("ca", pflag.Lookup("ca"))
    	viper.BindPFlag("key_type", pflag.Lookup("key_type"))
    	viper.BindPFlag("key_size", pflag.Lookup("key_size"))
    	viper.BindPFlag("validity", pflag.Lookup("validity"))
    	viper.BindPFlag("key_file_prefix", pflag.Lookup("key_file_prefix"))
    	viper.SetDefault("validateTLSCertificate", true)
    	viper.SetDefault("auto_token", false)
    }
    
    // ReadConfig reads the client configuration from a file into a Config struct.
    func ReadConfig(path string) (*Config, error) {
    	setDefaults()
    	if _, err := os.Stat(path); err == nil {
    		viper.SetConfigFile(path)
    		viper.SetConfigType("hcl")
    		if err := viper.ReadInConfig(); err != nil {
    			return nil, err
    		}
    	}
    	c := &Config{}
    	if err := viper.Unmarshal(c); err != nil {
    		return nil, err
    	}
    	p, err := homedir.Expand(c.PublicFilePrefix)
    	if err != nil {
    		return nil, err
    	}
    	c.PublicFilePrefix = p
    	return c, nil
    }