diff --git a/chat.go b/chat.go index 407711c..007195e 100644 --- a/chat.go +++ b/chat.go @@ -37,7 +37,15 @@ func getNewMessages(k *Keybase, subs *subscriptionChannels, execOptions []string execString = append(execString, execOptions...) } 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() execCmd.Start() scanner := bufio.NewScanner(stdOut) diff --git a/keybase.go b/keybase.go index e2e4987..5b90868 100644 --- a/keybase.go +++ b/keybase.go @@ -21,13 +21,33 @@ const ( 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. +// This is deprecated and will be removed in a future update. Use New() instead. func NewKeybase(path ...string) *Keybase { k := &Keybase{} if len(path) < 1 { - k.Path = "keybase" + k.ExePath = "keybase" } else { - k.Path = path[0] + k.ExePath = path[0] } s := k.status() @@ -42,7 +62,15 @@ func NewKeybase(path ...string) *Keybase { // Exec executes the given Keybase command 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 { return []byte{}, err } diff --git a/types.go b/types.go index 8d40d52..12a82ad 100644 --- a/types.go +++ b/types.go @@ -20,6 +20,37 @@ type RunOptions struct { 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 string `json:"type"` } @@ -954,7 +985,8 @@ type userBlocks struct { // Keybase holds basic information about the local Keybase executable type Keybase struct { - Path string + HomePath string + ExePath string Username string LoggedIn bool Version string