From 80f06f574eba62f224f9feaca938fcbf7698e209 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 28 May 2019 14:41:58 -0400 Subject: [PATCH] Created foundation for api commands --- api/api.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 api/api.go diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..8a4aa36 --- /dev/null +++ b/api/api.go @@ -0,0 +1,68 @@ +package api + +import ( + "encoding/json" + "os/exec" +) + +type Keybase struct { + path string +} + +type keybase interface { + LoggedIn() bool + Username() string + Version() string +} + +type status struct { + Username string `json:"Username"` + LoggedIn bool `json:"LoggedIn"` +} + +// New() returns a new instance of Keybase object. Optionally, you can pass a string containing the path to the Keybase executable as the first argument. +func New(path ...string) Keybase { + if len(path) < 1 { + return Keybase{path: "/usr/bin/keybase"} + } + return Keybase{path: path[0]} +} + +// Username() returns the username of the currently logged-in Keybase user. +func (k Keybase) Username() string { + cmd := exec.Command(k.path, "status", "-j") + cmdOut, err := cmd.Output() + if err != nil { + return "" + } + + var s status + json.Unmarshal(cmdOut, &s) + + return s.Username +} + +// LoggedIn() returns true if Keybase is currently logged in, otherwise return false. +func (k Keybase) LoggedIn() bool { + cmd := exec.Command(k.path, "status", "-j") + cmdOut, err := cmd.Output() + if err != nil { + return false + } + + var s status + json.Unmarshal(cmdOut, &s) + + return s.LoggedIn +} + +// Version() returns the version string of the client. +func (k Keybase) Version() string { + cmd := exec.Command(k.path, "version", "-S", "-f", "s") + cmdOut, err := cmd.Output() + if err != nil { + return "" + } + + return string(cmdOut) +}