Skip to content
Snippets Groups Projects
Commit 9f8f0194 authored by Niall Sheridan's avatar Niall Sheridan
Browse files

Correct client behaviours for option handling

A config file is not required - don't error if one doesn't exist.

Don't overwrite default options with an empty string.
parent 12417f0d
No related branches found
No related tags found
No related merge requests found
package client package client
import ( import (
"os"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
...@@ -28,11 +30,13 @@ func setDefaults() { ...@@ -28,11 +30,13 @@ func setDefaults() {
// ReadConfig reads the client configuration from a file into a Config struct. // ReadConfig reads the client configuration from a file into a Config struct.
func ReadConfig(path string) (*Config, error) { func ReadConfig(path string) (*Config, error) {
setDefaults() setDefaults()
if _, err := os.Stat(path); err == nil {
viper.SetConfigFile(path) viper.SetConfigFile(path)
viper.SetConfigType("hcl") viper.SetConfigType("hcl")
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
return nil, err return nil, err
} }
}
c := &Config{} c := &Config{}
if err := viper.Unmarshal(c); err != nil { if err := viper.Unmarshal(c); err != nil {
return nil, err return nil, err
......
...@@ -59,9 +59,11 @@ func pemBlockForKey(priv interface{}) (*pem.Block, error) { ...@@ -59,9 +59,11 @@ func pemBlockForKey(priv interface{}) (*pem.Block, error) {
// Default is "rsa" // Default is "rsa"
func KeyType(keyType string) KeyOption { func KeyType(keyType string) KeyOption {
return func(o *options) { return func(o *options) {
if keyType != "" {
o.keytype = keyType o.keytype = keyType
} }
} }
}
// KeySize sets the size of the key in bits. // KeySize sets the size of the key in bits.
// RSA keys must be a minimum of 1024 bits. The default is 2048 bits. // RSA keys must be a minimum of 1024 bits. The default is 2048 bits.
......
...@@ -36,17 +36,17 @@ func main() { ...@@ -36,17 +36,17 @@ func main() {
c, err := client.ReadConfig(*cfg) c, err := client.ReadConfig(*cfg)
if err != nil { if err != nil {
log.Printf("Error parsing config file: %v\n", err) log.Printf("Configuration error: %v\n", err)
}
fmt.Printf("Your browser has been opened to visit %s\n", c.CA)
if err := browser.OpenURL(c.CA); err != nil {
fmt.Println("Error launching web browser. Go to the link in your web browser")
} }
fmt.Println("Generating new key pair") fmt.Println("Generating new key pair")
priv, pub, err := client.GenerateKey(client.KeyType(c.Keytype), client.KeySize(c.Keysize)) priv, pub, err := client.GenerateKey(client.KeyType(c.Keytype), client.KeySize(c.Keysize))
if err != nil { if err != nil {
log.Fatalln("Error generating key pair: ", err) log.Fatalln("Error generating key pair: ", err)
} }
fmt.Printf("Your browser has been opened to visit %s\n", c.CA)
if err := browser.OpenURL(c.CA); err != nil {
fmt.Println("Error launching web browser. Go to the link in your web browser")
}
fmt.Print("Enter token: ") fmt.Print("Enter token: ")
var token string var token string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment