You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.4 KiB
162 lines
3.4 KiB
6 years ago
|
package keybase
|
||
6 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
5 years ago
|
"fmt"
|
||
6 years ago
|
"os/exec"
|
||
5 years ago
|
"strings"
|
||
4 years ago
|
|
||
|
"samhofi.us/x/keybase/v2/types/chat1"
|
||
6 years ago
|
)
|
||
|
|
||
4 years ago
|
// Used for testing
|
||
|
var execCommand = exec.Command
|
||
|
|
||
6 years ago
|
// Possible MemberTypes
|
||
|
const (
|
||
|
TEAM string = "team"
|
||
|
USER string = "impteamnative"
|
||
|
)
|
||
|
|
||
|
// Possible TopicTypes
|
||
|
const (
|
||
|
DEV string = "dev"
|
||
|
CHAT string = "chat"
|
||
|
)
|
||
|
|
||
4 years ago
|
// New returns a new Keybase
|
||
|
func New(opts ...KeybaseOpt) *Keybase {
|
||
|
k := &Keybase{ExePath: "keybase"}
|
||
|
|
||
|
for _, opt := range opts {
|
||
|
opt.apply(k)
|
||
|
}
|
||
|
|
||
|
s := k.status()
|
||
|
k.Version = k.version()
|
||
|
k.LoggedIn = s.LoggedIn
|
||
|
if k.LoggedIn {
|
||
|
k.Username = s.Username
|
||
|
k.Device = s.Device.Name
|
||
|
}
|
||
|
|
||
|
return k
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewKeybase returns a new Keybase. Optionally, you can pass a string containing the path to the Keybase executable as the first argument.
|
||
4 years ago
|
// This is deprecated and will be removed in a future update. Use New() instead.
|
||
6 years ago
|
func NewKeybase(path ...string) *Keybase {
|
||
|
k := &Keybase{}
|
||
6 years ago
|
if len(path) < 1 {
|
||
4 years ago
|
k.ExePath = "keybase"
|
||
6 years ago
|
} else {
|
||
4 years ago
|
k.ExePath = path[0]
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
s := k.status()
|
||
6 years ago
|
k.Version = k.version()
|
||
5 years ago
|
k.LoggedIn = s.LoggedIn
|
||
6 years ago
|
if k.LoggedIn {
|
||
5 years ago
|
k.Username = s.Username
|
||
|
k.Device = s.Device.Name
|
||
6 years ago
|
}
|
||
|
return k
|
||
|
}
|
||
|
|
||
4 years ago
|
// Exec executes the given Keybase command
|
||
|
func (k *Keybase) Exec(command ...string) ([]byte, error) {
|
||
|
cmd := make([]string, 0)
|
||
5 years ago
|
|
||
4 years ago
|
if k.HomePath != "" {
|
||
|
cmd = append(cmd, "--home", k.HomePath)
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
cmd = append(cmd, command...)
|
||
5 years ago
|
|
||
4 years ago
|
out, err := execCommand(k.ExePath, cmd...).Output()
|
||
5 years ago
|
if err != nil {
|
||
|
return []byte{}, err
|
||
|
}
|
||
|
return out, nil
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewChat returns a new Chat instance
|
||
4 years ago
|
func (k *Keybase) NewChat(channel chat1.ChatChannel) Chat {
|
||
6 years ago
|
return Chat{
|
||
|
keybase: k,
|
||
|
Channel: channel,
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewTeam returns a new Team instance
|
||
|
func (k *Keybase) NewTeam(name string) Team {
|
||
|
return Team{
|
||
|
keybase: k,
|
||
|
Name: name,
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// NewWallet returns a new Wallet instance
|
||
|
func (k *Keybase) NewWallet() Wallet {
|
||
|
return Wallet{
|
||
|
keybase: k,
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// 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 {
|
||
5 years ago
|
cmdOut, err := k.Exec("status", "-j")
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
return status{}
|
||
6 years ago
|
}
|
||
|
|
||
|
var s status
|
||
|
json.Unmarshal(cmdOut, &s)
|
||
|
|
||
5 years ago
|
return s
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// version returns the version string of the client.
|
||
6 years ago
|
func (k *Keybase) version() string {
|
||
5 years ago
|
cmdOut, err := k.Exec("version", "-S", "-f", "s")
|
||
6 years ago
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
return string(cmdOut)
|
||
|
}
|
||
5 years ago
|
|
||
|
// UserLookup pulls information about users.
|
||
5 years ago
|
// The following fields are currently returned: basics, profile, proofs_summary, devices -- See https://keybase.io/docs/api/1.0/call/user/lookup for more info.
|
||
5 years ago
|
func (k *Keybase) UserLookup(users ...string) (UserAPI, error) {
|
||
5 years ago
|
var fields = []string{"basics", "profile", "proofs_summary", "devices"}
|
||
5 years ago
|
|
||
|
cmdOut, err := k.Exec("apicall", "--arg", fmt.Sprintf("usernames=%s", strings.Join(users, ",")), "--arg", fmt.Sprintf("fields=%s", strings.Join(fields, ",")), "user/lookup")
|
||
|
if err != nil {
|
||
|
return UserAPI{}, err
|
||
|
}
|
||
|
|
||
|
var r UserAPI
|
||
|
if err := json.Unmarshal(cmdOut, &r); err != nil {
|
||
|
return UserAPI{}, err
|
||
|
}
|
||
|
|
||
|
return r, nil
|
||
|
}
|
||
5 years ago
|
|
||
|
// UserCard pulls the information that is typically displayed when you open a user's profile.
|
||
|
func (k *Keybase) UserCard(user string) (UserCardAPI, error) {
|
||
|
cmdOut, err := k.Exec("apicall", "--arg", "username="+user, "user/card")
|
||
|
if err != nil {
|
||
|
return UserCardAPI{}, err
|
||
|
}
|
||
|
|
||
|
var r UserCardAPI
|
||
|
if err := json.Unmarshal(cmdOut, &r); err != nil {
|
||
|
return UserCardAPI{}, err
|
||
|
}
|
||
|
|
||
|
return r, nil
|
||
|
}
|