Consolidate the various functions that call keybase status so it only gets called once

This commit is contained in:
Sam
2019-09-27 22:40:38 -04:00
parent fc05a8d710
commit 74a93b765c
3 changed files with 12 additions and 37 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module samhofi.us/x/keybase
go 1.12
go 1.13

View File

@ -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 {
// 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 ""
return status{}
}
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 {
cmdOut, err := k.Exec("status", "-j")
if err != nil {
return false
}
var s status
json.Unmarshal(cmdOut, &s)
return s.LoggedIn
return s
}
// version returns the version string of the client.

View File

@ -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 {