From 4dfb29f2e07fe6fb90544ba66cf0a3df187072d9 Mon Sep 17 00:00:00 2001
From: Niall Sheridan <nsheridan@gmail.com>
Date: Sat, 28 May 2016 22:30:42 +0100
Subject: [PATCH] Use flags as defaults, allow them to override config file

---
 README.md             |  2 +-
 cmd/cashier/config.go |  9 +++++----
 cmd/cashier/main.go   | 13 +++++++++----
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index 0eab9879..e35cdf0a 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,7 @@ The user can now ssh to the production machine, and continue to ssh to any machi
 
 # Usage
 Cashier comes in two parts, a [cli](cmd/cashier) and a [server](cmd/cashierd).
-The client is configured using a [HCL](https://github.com/hashicorp/hcl) configuration file - [example](example-client.cfg).
+The client is configured using either a [HCL](https://github.com/hashicorp/hcl) configuration file - [example](example-client.cfg) - or command-line flags.
 The server is configured using a JSON configuration file - [example](example-server.json).
 
 For the server you need the following:
diff --git a/cmd/cashier/config.go b/cmd/cashier/config.go
index 1196cbdb..eed98e1a 100644
--- a/cmd/cashier/config.go
+++ b/cmd/cashier/config.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"github.com/spf13/pflag"
 	"github.com/spf13/viper"
 )
 
@@ -13,10 +14,10 @@ type config struct {
 }
 
 func setDefaults() {
-	viper.SetDefault("ca", "http://localhost:10000")
-	viper.SetDefault("key_type", "rsa")
-	viper.SetDefault("key_size", 2048)
-	viper.SetDefault("validity", "24h")
+	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.SetDefault("validateTLSCertificate", true)
 }
 
diff --git a/cmd/cashier/main.go b/cmd/cashier/main.go
index 564664c0..768ebcdf 100644
--- a/cmd/cashier/main.go
+++ b/cmd/cashier/main.go
@@ -4,7 +4,6 @@ import (
 	"bytes"
 	"crypto/tls"
 	"encoding/json"
-	"flag"
 	"fmt"
 	"io/ioutil"
 	"log"
@@ -17,13 +16,18 @@ import (
 
 	"github.com/nsheridan/cashier/lib"
 	"github.com/pkg/browser"
+	"github.com/spf13/pflag"
 	"golang.org/x/crypto/ssh"
 	"golang.org/x/crypto/ssh/agent"
 )
 
 var (
-	u, _ = user.Current()
-	cfg  = flag.String("config", path.Join(u.HomeDir, ".cashier.cfg"), "Path to config file")
+	u, _     = user.Current()
+	cfg      = pflag.String("config", path.Join(u.HomeDir, ".cashier.conf"), "Path to config file")
+	ca       = pflag.String("ca", "http://localhost:10000", "CA server")
+	keysize  = pflag.Int("key_size", 2048, "Key size. Ignored for ed25519 keys")
+	validity = pflag.Duration("validity", time.Hour*24, "Key validity")
+	keytype  = pflag.String("key_type", "rsa", "Type of private key to generate - rsa, ecdsa or ed25519")
 )
 
 func installCert(a agent.Agent, cert *ssh.Certificate, key key) error {
@@ -102,7 +106,8 @@ func sign(pub ssh.PublicKey, token string, conf *config) (*ssh.Certificate, erro
 }
 
 func main() {
-	flag.Parse()
+	pflag.Parse()
+
 	c, err := readConfig(*cfg)
 	if err != nil {
 		log.Fatalf("Error parsing config file: %v\n", err)
-- 
GitLab