Consolidate the various functions that call keybase status so it only gets called once
This commit is contained in:
2
go.mod
2
go.mod
@ -1,3 +1,3 @@
|
|||||||
module samhofi.us/x/keybase
|
module samhofi.us/x/keybase
|
||||||
|
|
||||||
go 1.12
|
go 1.13
|
||||||
|
|||||||
43
keybase.go
43
keybase.go
@ -25,11 +25,13 @@ func NewKeybase(path ...string) *Keybase {
|
|||||||
} else {
|
} else {
|
||||||
k.Path = path[0]
|
k.Path = path[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s := k.status()
|
||||||
k.Version = k.version()
|
k.Version = k.version()
|
||||||
k.LoggedIn = k.loggedIn()
|
k.LoggedIn = s.LoggedIn
|
||||||
if k.LoggedIn {
|
if k.LoggedIn {
|
||||||
k.Username = k.username()
|
k.Username = s.Username
|
||||||
k.Device = k.device()
|
k.Device = s.Device.Name
|
||||||
}
|
}
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
@ -66,43 +68,18 @@ func (k *Keybase) NewWallet() Wallet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// username returns the username of the currently logged-in Keybase user.
|
// status returns the results of the `keybase status` command, which includes
|
||||||
func (k *Keybase) username() string {
|
// information about the client, and the currently logged-in Keybase user.
|
||||||
|
func (k *Keybase) status() status {
|
||||||
cmdOut, err := k.Exec("status", "-j")
|
cmdOut, err := k.Exec("status", "-j")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return status{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var s status
|
var s status
|
||||||
json.Unmarshal(cmdOut, &s)
|
json.Unmarshal(cmdOut, &s)
|
||||||
|
|
||||||
return s.Username
|
return s
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// version returns the version string of the client.
|
// version returns the version string of the client.
|
||||||
|
|||||||
4
types.go
4
types.go
@ -582,10 +582,8 @@ type keybase interface {
|
|||||||
NewTeam(name string) Team
|
NewTeam(name string) Team
|
||||||
NewWallet() Wallet
|
NewWallet() Wallet
|
||||||
Run(handler func(ChatAPI), options ...RunOptions)
|
Run(handler func(ChatAPI), options ...RunOptions)
|
||||||
loggedIn() bool
|
|
||||||
username() string
|
|
||||||
version() string
|
version() string
|
||||||
device() string
|
status() status
|
||||||
}
|
}
|
||||||
|
|
||||||
type status struct {
|
type status struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user