David Haukeness 3 years ago
parent
commit
40f1c93b57
  1. 5
      go.mod
  2. 12
      go.sum
  3. 93
      libkeybase.go

5
go.mod

@ -2,4 +2,7 @@ module git.hugfreevikings.wtf/keybase/libkeybase @@ -2,4 +2,7 @@ module git.hugfreevikings.wtf/keybase/libkeybase
go 1.16
require samhofi.us/x/keybase/v2 v2.1.1 // indirect
require (
github.com/urfave/cli/v2 v2.3.0 // indirect
samhofi.us/x/keybase/v2 v2.1.1 // indirect
)

12
go.sum

@ -1,2 +1,14 @@ @@ -1,2 +1,14 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
samhofi.us/x/keybase/v2 v2.1.1 h1:XPWrmdbJCrNcsW3sRuR6WuALYOZt7O+av0My6YoehqE=
samhofi.us/x/keybase/v2 v2.1.1/go.mod h1:lJivwhzMSV+WUg+XUbatszStjjFVcuLGl+xcQpqQ5GQ=

93
libkeybase.go

@ -4,8 +4,8 @@ import ( @@ -4,8 +4,8 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"os/exec"
"sync"
"time"
)
@ -48,25 +48,26 @@ func (r RunOptions) buildBaseCommand(args ...string) []string { @@ -48,25 +48,26 @@ func (r RunOptions) buildBaseCommand(args ...string) []string {
// apiPrimitive is re-used between ApiWriter and ApiReader
type apiPrimitive struct {
cmd *exec.Cmd
opts RunOptions
input io.WriteCloser
output io.ReadCloser
Input chan string
Output chan string
stop bool
die chan bool
sync.Mutex
cmd *exec.Cmd
opts RunOptions
Input chan string
Output chan string
stop bool
running bool
die chan bool
}
// Stop halts the subprocess
func (a apiPrimitive) Stop() {
func (a *apiPrimitive) Stop() {
a.cmd.Process.Kill()
a.stop = true
a.die <- true
a.running = false
}
// Start begins the subprocess call
func (a apiPrimitive) Start(args ...string) (err error) {
// _start begins the subprocess call
func (a *apiPrimitive) _start(args ...string) (err error) {
// build the base command (homedir and stuff) and add the args to it
cmdStrings := a.opts.buildBaseCommand(args...)
// set up the command execution
@ -81,8 +82,7 @@ func (a apiPrimitive) Start(args ...string) (err error) { @@ -81,8 +82,7 @@ func (a apiPrimitive) Start(args ...string) (err error) {
if err != nil {
return
}
// buffer the channels to communicate with this process
a.Input = make(chan string, a.opts.ChannelCapacity)
// buffer the output string (input should only need one)
a.Output = make(chan string, a.opts.ChannelCapacity)
// now we need to start a select where anything in the channel goes to stdin
go func() {
@ -119,6 +119,7 @@ func (a apiPrimitive) Start(args ...string) (err error) { @@ -119,6 +119,7 @@ func (a apiPrimitive) Start(args ...string) (err error) {
}
}()
// if you've made it this far, return the IO channels
a.running = true
return nil
}
@ -126,7 +127,71 @@ type apiWriter struct { @@ -126,7 +127,71 @@ type apiWriter struct {
apiPrimitive
}
func (a *apiWriter) start() (err error) {
err = a._start("api", "listen")
return
}
func (a *apiWriter) write(in string) (out string, err error) {
if !a.running {
return "", fmt.Errorf("api is not running")
}
// write the input
a.Input <- in
// now wait for the output
out = <-a.Output
return
}
type apiReader struct {
apiPrimitive
}
func (a *apiReader) start() (err error) {
err = a._start("chat", "api-listen")
return
}
func (a *apiReader) listen() (out chan string, err error) {
if !a.running {
err = fmt.Errorf("apiReader is not running")
}
out = a.Output
return
}
// API is the basic primitive of the API, holding the actual application calls and pipes
type API struct {
writer apiWriter
reader apiReader
Timeout time.Duration
}
func (a *API) Start() error {
err := a.reader.start()
if err != nil {
return err
}
err = a.writer.start()
if err != nil {
return err
}
return nil
}
func (a *API) Stop() {
a.writer.Stop()
a.reader.Stop()
}
func (a *API) Write(in string) (out string, err error) {
a.writer.Lock()
out, err = a.writer.write(in)
a.writer.Unlock()
return
}
func (a *API) Listen() (out chan string, err error) {
out, err = a.reader.listen()
return
}

Loading…
Cancel
Save