From 74a93b765c93f68bd2502bad857917dd04b9b485 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 27 Sep 2019 22:40:38 -0400 Subject: [PATCH] Consolidate the various functions that call keybase status so it only gets called once --- go.mod | 2 +- keybase.go | 43 ++++++++++--------------------------------- types.go | 4 +--- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/go.mod b/go.mod index 92510cc..05f8684 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module samhofi.us/x/keybase -go 1.12 +go 1.13 diff --git a/keybase.go b/keybase.go index 6706f1c..b43df25 100644 --- a/keybase.go +++ b/keybase.go @@ -25,11 +25,13 @@ func NewKeybase(path ...string) *Keybase { } else { k.Path = path[0] } + + s := k.status() k.Version = k.version() - k.LoggedIn = k.loggedIn() + k.LoggedIn = s.LoggedIn if k.LoggedIn { - k.Username = k.username() - k.Device = k.device() + k.Username = s.Username + k.Device = s.Device.Name } return k } @@ -66,43 +68,18 @@ func (k *Keybase) NewWallet() Wallet { } } -// username returns the username of the currently logged-in Keybase user. -func (k *Keybase) username() string { - cmdOut, err := k.Exec("status", "-j") - if err != nil { - return "" - } - - var s status - json.Unmarshal(cmdOut, &s) - - return s.Username -} - -// device returns the device name of the currently provisioned device. -func (k *Keybase) device() string { - cmdOut, err := k.Exec("status", "-j") - if err != nil { - return "" - } - - var s status - json.Unmarshal(cmdOut, &s) - - return s.Device.Name -} - -// loggedIn returns true if Keybase is currently logged in, otherwise returns false. -func (k *Keybase) loggedIn() bool { +// status returns the results of the `keybase status` command, which includes +// information about the client, and the currently logged-in Keybase user. +func (k *Keybase) status() status { cmdOut, err := k.Exec("status", "-j") if err != nil { - return false + return status{} } var s status json.Unmarshal(cmdOut, &s) - return s.LoggedIn + return s } // version returns the version string of the client. diff --git a/types.go b/types.go index 67f0844..c92166a 100644 --- a/types.go +++ b/types.go @@ -582,10 +582,8 @@ type keybase interface { NewTeam(name string) Team NewWallet() Wallet Run(handler func(ChatAPI), options ...RunOptions) - loggedIn() bool - username() string version() string - device() string + status() status } type status struct {