4 Commits

Author SHA1 Message Date
0a8e68baf4 Remove print statement that was left in by accident... woops! 2020-05-12 18:50:55 -04:00
acc3e3ddec Fix ListConvsOnName 2020-05-12 14:45:50 -04:00
22396f8276 Add ListConvsOnName 2020-05-12 01:39:29 -04:00
c9a2bd80bc Add New() for creating new keybase objects.
This function accepts functional options, and allows you to set an alternate home dir for keybase
2020-05-11 22:48:30 -04:00
3 changed files with 125 additions and 5 deletions

44
chat.go
View File

@ -37,7 +37,15 @@ func getNewMessages(k *Keybase, subs *subscriptionChannels, execOptions []string
execString = append(execString, execOptions...) execString = append(execString, execOptions...)
} }
for { for {
execCmd := exec.Command(k.Path, execString...) cmd := make([]string, 0)
if k.HomePath != "" {
cmd = append(cmd, "--home", k.HomePath)
}
cmd = append(cmd, execString...)
execCmd := exec.Command(k.ExePath, cmd...)
stdOut, _ := execCmd.StdoutPipe() stdOut, _ := execCmd.StdoutPipe()
execCmd.Start() execCmd.Start()
scanner := bufio.NewScanner(stdOut) scanner := bufio.NewScanner(stdOut)
@ -744,3 +752,37 @@ func (k *Keybase) ListMembersOfConversation(convID chat1.ConvIDStr) (chat1.ChatM
} }
return k.ListMembers(opts) return k.ListMembers(opts)
} }
// ListConvsOnName returns a list of all conversations for a chat1.ChatChannel
func (k *Keybase) ListConvsOnName(channel chat1.ChatChannel) (*[]chat1.ConvSummary, error) {
type result struct {
Conversations []chat1.ConvSummary `json:"conversations"`
}
type res struct {
Result result `json:"result"`
Error *Error `json:"error,omitempty"`
}
var r res
arg := newListConvsOnNameArg(channel)
jsonBytes, _ := json.Marshal(arg)
cmdOut, err := k.Exec("chat", "api", "-m", string(jsonBytes))
if err != nil {
return nil, err
}
err = json.Unmarshal(cmdOut, &r)
if err != nil {
return nil, err
}
if r.Error != nil {
return nil, fmt.Errorf("%v", r.Error.Message)
}
return &r.Result.Conversations, nil
}

View File

@ -21,13 +21,33 @@ const (
CHAT string = "chat" CHAT string = "chat"
) )
// 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
}
// NewKeybase returns a new Keybase. Optionally, you can pass a string containing the path to the Keybase executable as the first argument. // NewKeybase returns a new Keybase. Optionally, you can pass a string containing the path to the Keybase executable as the first argument.
// This is deprecated and will be removed in a future update. Use New() instead.
func NewKeybase(path ...string) *Keybase { func NewKeybase(path ...string) *Keybase {
k := &Keybase{} k := &Keybase{}
if len(path) < 1 { if len(path) < 1 {
k.Path = "keybase" k.ExePath = "keybase"
} else { } else {
k.Path = path[0] k.ExePath = path[0]
} }
s := k.status() s := k.status()
@ -42,7 +62,15 @@ func NewKeybase(path ...string) *Keybase {
// Exec executes the given Keybase command // Exec executes the given Keybase command
func (k *Keybase) Exec(command ...string) ([]byte, error) { func (k *Keybase) Exec(command ...string) ([]byte, error) {
out, err := exec.Command(k.Path, command...).Output() cmd := make([]string, 0)
if k.HomePath != "" {
cmd = append(cmd, "--home", k.HomePath)
}
cmd = append(cmd, command...)
out, err := exec.Command(k.ExePath, cmd...).Output()
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }

View File

@ -20,6 +20,37 @@ type RunOptions struct {
FilterChannels []chat1.ChatChannel // Only subscribe to messages from specified channels FilterChannels []chat1.ChatChannel // Only subscribe to messages from specified channels
} }
// KeybaseOpt configures a Keybase
type KeybaseOpt interface {
apply(kb *Keybase)
}
// SetExePath sets the path to the Keybase executable
func SetExePath(path string) KeybaseOpt {
return setExePath{path}
}
type setExePath struct {
path string
}
func (o setExePath) apply(kb *Keybase) {
kb.ExePath = o.path
}
// SetHomePath sets the path to the Keybase home directory
func SetHomePath(path string) KeybaseOpt {
return setHomePath{path}
}
type setHomePath struct {
path string
}
func (o setHomePath) apply(kb *Keybase) {
kb.HomePath = o.path
}
type subscriptionType struct { type subscriptionType struct {
Type string `json:"type"` Type string `json:"type"`
} }
@ -207,6 +238,24 @@ func newListMembersArg(options ListMembersOptions) listMembersArg {
} }
} }
type listConvsOnNameParams struct {
Options chat1.ChatChannel
}
type listConvsOnNameArg struct {
Method string
Params listConvsOnNameParams
}
func newListConvsOnNameArg(channel chat1.ChatChannel) listConvsOnNameArg {
return listConvsOnNameArg{
Method: "listconvsonname",
Params: listConvsOnNameParams{
Options: channel,
},
}
}
// KVOptions holds a set of options to be passed to the KV methods // KVOptions holds a set of options to be passed to the KV methods
type KVOptions struct { type KVOptions struct {
Team *string `json:"team"` Team *string `json:"team"`
@ -954,7 +1003,8 @@ type userBlocks struct {
// Keybase holds basic information about the local Keybase executable // Keybase holds basic information about the local Keybase executable
type Keybase struct { type Keybase struct {
Path string HomePath string
ExePath string
Username string Username string
LoggedIn bool LoggedIn bool
Version string Version string