From 0a6f33e429c70cd74c81ad51a8b9da49c75c7105 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 26 Jul 2019 15:33:55 -0400 Subject: [PATCH] First pass at adding Team API stuff --- keybase.go | 8 ++++++++ team.go | 35 ++++++++++++++++++++++++++++++++++ types.go | 56 ++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 team.go diff --git a/keybase.go b/keybase.go index a84c892..c31fdb7 100644 --- a/keybase.go +++ b/keybase.go @@ -41,6 +41,14 @@ func (k *Keybase) NewChat(channel Channel) Chat { } } +// NewTeam returns a new Team instance +func (k *Keybase) NewTeam(name string) Team { + return Team{ + keybase: k, + Name: name, + } +} + // username returns the username of the currently logged-in Keybase user. func (k *Keybase) username() string { cmd := exec.Command(k.Path, "status", "-j") diff --git a/team.go b/team.go new file mode 100644 index 0000000..e439332 --- /dev/null +++ b/team.go @@ -0,0 +1,35 @@ +package keybase + +import ( + "encoding/json" + "fmt" + "os/exec" +) + +// teamAPIOut sends JSON requests to the team API and returns its response. +func teamAPIOut(keybasePath string, w TeamAPI) (TeamAPI, error) { + jsonBytes, _ := json.Marshal(w) + + cmd := exec.Command(keybasePath, "team", "api", "-m", string(jsonBytes)) + cmdOut, err := cmd.Output() + if err != nil { + return TeamAPI{}, err + } + + var r TeamAPI + json.Unmarshal(cmdOut, &r) + + return r, nil +} + +// CreateSubteam creates a subteam +func (t Team) CreateSubteam(name string) (TeamAPI, error) { + m := TeamAPI{ + Params: &tParams{}, + } + m.Method = "create-team" + m.Params.Options.Team = fmt.Sprintf("%s.%s", t.Name, name) + + r, err := teamAPIOut(t.keybase.Path, m) + return r, err +} diff --git a/types.go b/types.go index 725814c..a7c2864 100644 --- a/types.go +++ b/types.go @@ -240,20 +240,6 @@ type SendPayment struct { PaymentID string `json:"paymentID"` } -// Keybase holds basic information about the local Keybase executable -type Keybase struct { - Path string - Username string - LoggedIn bool - Version string -} - -// Chat holds basic information about a specific conversation -type Chat struct { - keybase *Keybase - Channel Channel -} - // WalletAPI holds data for sending to API type WalletAPI struct { Method string `json:"method,omitempty"` @@ -322,6 +308,37 @@ type wResult struct { Unread bool `json:"unread"` } +// TeamAPI holds information sent and received to/from the team api +type TeamAPI struct { + Method string `json:"method"` + Params *tParams `json:"params"` + Result *tResult `json:"result"` +} +type tOptions struct { + Team string `json:"team"` +} +type tParams struct { + Options tOptions `json:"options"` +} +type tResult struct { + ChatSent bool `json:"chatSent"` + CreatorAdded bool `json:"creatorAdded"` +} + +// Keybase holds basic information about the local Keybase executable +type Keybase struct { + Path string + Username string + LoggedIn bool + Version string +} + +// Chat holds basic information about a specific conversation +type Chat struct { + keybase *Keybase + Channel Channel +} + type chat interface { Send(message ...string) (ChatAPI, error) Edit(messageID int, message ...string) (ChatAPI, error) @@ -334,7 +351,18 @@ type chatAPI interface { Previous(count ...int) (*ChatAPI, error) } +// Team holds basic information about a team +type Team struct { + keybase *Keybase + Name string +} + +type team interface { + CreateSubteam(name string) (TeamAPI, error) +} + type keybase interface { + NewTeam(name string) Team NewChat(channel Channel) Chat Run(handler func(ChatAPI), options ...RunOptions) ChatList() ([]conversation, error)