From 9f8f0194a3c21e640a5b917f86bf204c014d730d Mon Sep 17 00:00:00 2001
From: Niall Sheridan <nsheridan@gmail.com>
Date: Wed, 8 Aug 2018 21:52:34 +0100
Subject: [PATCH] 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.
---
 client/config.go    | 12 ++++++++----
 client/keys.go      |  4 +++-
 cmd/cashier/main.go | 10 +++++-----
 3 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/client/config.go b/client/config.go
index eae3bfa6..4536994b 100644
--- a/client/config.go
+++ b/client/config.go
@@ -1,6 +1,8 @@
 package client
 
 import (
+	"os"
+
 	"github.com/mitchellh/go-homedir"
 	"github.com/spf13/pflag"
 	"github.com/spf13/viper"
@@ -28,10 +30,12 @@ func setDefaults() {
 // ReadConfig reads the client configuration from a file into a Config struct.
 func ReadConfig(path string) (*Config, error) {
 	setDefaults()
-	viper.SetConfigFile(path)
-	viper.SetConfigType("hcl")
-	if err := viper.ReadInConfig(); err != nil {
-		return nil, err
+	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 {
diff --git a/client/keys.go b/client/keys.go
index b488ea22..f573136b 100644
--- a/client/keys.go
+++ b/client/keys.go
@@ -59,7 +59,9 @@ func pemBlockForKey(priv interface{}) (*pem.Block, error) {
 // Default is "rsa"
 func KeyType(keyType string) KeyOption {
 	return func(o *options) {
-		o.keytype = keyType
+		if keyType != "" {
+			o.keytype = keyType
+		}
 	}
 }
 
diff --git a/cmd/cashier/main.go b/cmd/cashier/main.go
index 2b243648..f0ba2f1a 100644
--- a/cmd/cashier/main.go
+++ b/cmd/cashier/main.go
@@ -36,17 +36,17 @@ func main() {
 
 	c, err := client.ReadConfig(*cfg)
 	if err != nil {
-		log.Printf("Error parsing config file: %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")
+		log.Printf("Configuration error: %v\n", err)
 	}
 	fmt.Println("Generating new key pair")
 	priv, pub, err := client.GenerateKey(client.KeyType(c.Keytype), client.KeySize(c.Keysize))
 	if err != nil {
 		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: ")
 	var token string
-- 
GitLab