Browse Source

Add New() for creating new keybase objects.

This function accepts functional options, and allows you to set an alternate home dir for keybase
main v2.0.3
Sam Hofius 4 years ago
parent
commit
c9a2bd80bc
  1. 10
      chat.go
  2. 34
      keybase.go
  3. 34
      types.go

10
chat.go

@ -37,7 +37,15 @@ func getNewMessages(k *Keybase, subs *subscriptionChannels, execOptions []string @@ -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)

34
keybase.go

@ -21,13 +21,33 @@ const ( @@ -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 { @@ -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
}

34
types.go

@ -20,6 +20,37 @@ type RunOptions struct { @@ -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 { @@ -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

Loading…
Cancel
Save