From 16eb2846959a74bd7fcf5e0406bef78c921ba74c Mon Sep 17 00:00:00 2001 From: Mark Hudnall Date: Mon, 15 Jun 2020 19:25:21 -0700 Subject: [PATCH] Add instructions for configuration + some error messages --- README.md | 37 +++++++++++++++++++++++++- main.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 79d0f26..ffaa5c2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,42 @@ -# plaid-cli +# plaid-cli 🤑 > Link accounts and get transactions from the command line. +plaid-cli is a CLI tool for working with the Plaid API. + +You can use plaid-cli to link bank accounts and pull transactions in multiple +output formats from the comfort of the command line. + +## Configuration + +To get started, you'll need Plaid API credentials, which you can get by visiting +https://dashboard.plaid.com/team/keys after signing up for free. + +plaid-cli will look at the following environment variables for API credentials: + +```sh +PLAID_CLIENT_ID= +PLAID_PUBLIC_KEY= +PLAID_SECRET= +PLAID_ENVIRONMENT=development +``` + +I recommend setting and exporting these on shell startup. + +API credentials can also be specified using a config file located at +~/.plaid-cli/config.toml: + +```toml +[plaid] +client_id = "" +public_key = "" +secret = "" +environment = "development" +``` + +After setting those API credentials, plaid-cli is ready to use! +You'll probably want to run 'plaid-cli link' next. + ## Usage
diff --git a/main.go b/main.go
index 3ce9d21..c7e7677 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"log"
 	"net/http"
+	"os"
 	"os/user"
 	"path/filepath"
 	"regexp"
@@ -44,13 +45,28 @@ func main() {
 		}
 	}
 
+	viper.SetEnvPrefix("")
+	viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
 	viper.AutomaticEnv()
 
+	viper.SetDefault("plaid.environment", "development")
+	plaidEnvStr := strings.ToLower(viper.GetString("plaid.environment"))
+
+	var plaidEnv plaid.Environment
+	switch plaidEnvStr {
+	case "development":
+		plaidEnv = plaid.Development
+	case "production":
+		plaidEnv = plaid.Production
+	default:
+		log.Fatalln("Invalid plaid environment. Valid plaid environments are 'development' or 'production'.")
+	}
+
 	opts := plaid.ClientOptions{
 		viper.GetString("plaid.client_id"),
 		viper.GetString("plaid.secret"),
 		viper.GetString("plaid.public_key"),
-		plaid.Development,
+		plaidEnv,
 		&http.Client{},
 	}
 
@@ -286,13 +302,70 @@ func main() {
 	transactionsCommand.Flags().StringVarP(&outputFormat, "output-format", "o", "json", "Output format")
 	transactionsCommand.Flags().StringVarP(&accountID, "account-id", "a", "", "Fetch transactions for this account ID only.")
 
-	rootCommand := &cobra.Command{Use: "plaid-cli"}
+	rootCommand := &cobra.Command{
+		Use:   "plaid-cli",
+		Short: "Link bank accounts and get transactions from the command line.",
+		Long: `plaid-cli 🤑
+
+plaid-cli is a CLI tool for working with the Plaid API.
+
+You can use plaid-cli to link bank accounts and pull transactions in multiple 
+output formats from the comfort of the command line.
+
+Configuration:
+  To get started, you'll need Plaid API credentials, which you can get by visiting
+  https://dashboard.plaid.com/team/keys after signing up for free.
+  
+  plaid-cli will look at the following environment variables for API credentials:
+  
+    PLAID_CLIENT_ID=
+    PLAID_PUBLIC_KEY=
+    PLAID_SECRET=
+    PLAID_ENVIRONMENT=development
+  
+  I recommend setting and exporting these on shell startup.
+  
+  API credentials can also be specified using a config file located at 
+  ~/.plaid-cli/config.toml:
+  
+    [plaid]
+    client_id = ""
+    public_key = ""
+    secret = ""
+    environment = "development"
+  
+  After setting those API credentials, plaid-cli is ready to use! 
+  You'll probably want to run 'plaid-cli link' next.
+  
+  Please see the README (https://github.com/landakram/plaid-cli/blob/master/README.md) 
+  for more detailed usage instructions.
+
+  Made by @landakram.
+`,
+	}
 	rootCommand.AddCommand(linkCommand)
 	rootCommand.AddCommand(tokensCommand)
 	rootCommand.AddCommand(aliasCommand)
 	rootCommand.AddCommand(aliasesCommand)
 	rootCommand.AddCommand(accountsCommand)
 	rootCommand.AddCommand(transactionsCommand)
+
+	if !viper.IsSet("plaid.client_id") {
+		log.Println("⚠️  PLAID_CLIENT_ID not set. Please see the configuration instructions below.")
+		rootCommand.Help()
+		os.Exit(1)
+	}
+	if !viper.IsSet("plaid.secret") {
+		log.Println("⚠️ PLAID_SECRET not set. Please see the configuration instructions below.")
+		rootCommand.Help()
+		os.Exit(1)
+	}
+	if !viper.IsSet("plaid.public_key") {
+		log.Println("⚠️ PLAID_PUBLIC_KEY not set. Please see the configuration instructions below.")
+		rootCommand.Help()
+		os.Exit(1)
+	}
+
 	rootCommand.Execute()
 }