From c7d0ed8fdb5a47e9d6065952e67dbe2abd2f3837 Mon Sep 17 00:00:00 2001 From: Mark Hudnall Date: Sat, 13 Jun 2020 23:47:39 -0700 Subject: [PATCH] Prompt for an alias after linking --- cmd/plaid-cli/main.go | 71 +++++++++++++++++++++++++++++++++++++------ go.mod | 1 + go.sum | 14 +++++++++ 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/cmd/plaid-cli/main.go b/cmd/plaid-cli/main.go index 4123287..8a6230b 100644 --- a/cmd/plaid-cli/main.go +++ b/cmd/plaid-cli/main.go @@ -10,9 +10,11 @@ import ( "net/http" "os/user" "path/filepath" + "regexp" "strings" "github.com/landakram/plaid-cli/pkg/plaid_cli" + "github.com/manifoldco/promptui" "github.com/plaid/plaid-go/plaid" "github.com/spf13/cobra" @@ -90,6 +92,45 @@ func main() { if err != nil { log.Fatalln(err) } + + log.Println("Institution linked!") + log.Println(fmt.Sprintf("Item ID: %s", tokenPair.ItemID)) + + if alias, ok := data.BackAliases[tokenPair.ItemID]; ok { + log.Println(fmt.Sprintf("Alias: %s", alias)) + return + } + + validate := func(input string) error { + matched, err := regexp.Match(`^\w+$`, []byte(input)) + if err != nil { + return err + } + + if !matched && input != "" { + return errors.New("Valid characters: [0-9A-Za-z_]") + } + + return nil + } + + log.Println("You can give the institution a friendly alias and use that instead of the item ID in most commands.") + prompt := promptui.Prompt{ + Label: "Alias (default: none)", + Validate: validate, + } + + input, err := prompt.Run() + if err != nil { + log.Fatalln(err) + } + + if input != "" { + err = SetAlias(data, tokenPair.ItemID, input) + if err != nil { + log.Fatalln(err) + } + } }, } @@ -119,19 +160,14 @@ func main() { aliasCommand := &cobra.Command{ Use: "alias [ITEM-ID] [NAME]", - Short: "Give a linked bank account a name.", + Short: "Give a linked institution a friendly name.", + Long: "Give a linked institution a friendly name. You can use this name instead of the idem ID in most commands.", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { itemID := args[0] - name := args[1] - - if _, ok := data.Tokens[itemID]; !ok { - log.Fatalf("No access token found for item ID `%s`. Try re-linking your account with `plaid-cli link`.\n", itemID) - } + alias := args[1] - data.Aliases[name] = itemID - data.BackAliases[itemID] = name - err = data.Save() + err := SetAlias(data, itemID, alias) if err != nil { log.Fatalln(err) } @@ -350,6 +386,23 @@ func (w *CSVSerializer) serialize(txs []plaid.Transaction) ([]byte, error) { return b.Bytes(), err } +func SetAlias(data *plaid_cli.Data, itemID string, alias string) error { + if _, ok := data.Tokens[itemID]; !ok { + return errors.New(fmt.Sprintf("No access token found for item ID `%s`. Try re-linking your account with `plaid-cli link`.", itemID)) + } + + data.Aliases[alias] = itemID + data.BackAliases[itemID] = alias + err := data.Save() + if err != nil { + return err + } + + log.Println(fmt.Sprintf("Aliased %s to %s.", itemID, alias)) + + return nil +} + type JSONSerializer struct{} func (w *JSONSerializer) serialize(txs []plaid.Transaction) ([]byte, error) { diff --git a/go.mod b/go.mod index b96a2a0..1aea430 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.14 require ( github.com/fsnotify/fsnotify v1.4.9 // indirect + github.com/manifoldco/promptui v0.7.0 // indirect github.com/mitchellh/mapstructure v1.3.2 // indirect github.com/pelletier/go-toml v1.8.0 // indirect github.com/plaid/plaid-go v0.0.0-20200529200923-9627743aa512 diff --git a/go.sum b/go.sum index f2ffa98..b04a20a 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,10 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= @@ -104,6 +108,8 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -112,11 +118,18 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw= +github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/manifoldco/promptui v0.7.0 h1:3l11YT8tm9MnwGFQ4kETwkzpAwY2Jt9lCrumCUW4+z4= +github.com/manifoldco/promptui v0.7.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= @@ -260,6 +273,7 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=