2019-08-26 12:00:31 -07:00
|
|
|
package kbchat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-02-08 07:50:16 -08:00
|
|
|
"io/ioutil"
|
2019-08-26 12:00:31 -07:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2020-02-08 07:50:16 -08:00
|
|
|
|
|
|
|
"github.com/keybase/go-keybase-chat-bot/kbchat/types/chat1"
|
|
|
|
"github.com/keybase/go-keybase-chat-bot/kbchat/types/stellar1"
|
2019-08-26 12:00:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// API is the main object used for communicating with the Keybase JSON API
|
|
|
|
type API struct {
|
|
|
|
sync.Mutex
|
2020-02-08 07:50:16 -08:00
|
|
|
apiInput io.Writer
|
|
|
|
apiOutput *bufio.Reader
|
|
|
|
apiCmd *exec.Cmd
|
|
|
|
username string
|
|
|
|
runOpts RunOptions
|
|
|
|
subscriptions []*NewSubscription
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUsername(runOpts RunOptions) (username string, err error) {
|
|
|
|
p := runOpts.Command("status")
|
|
|
|
output, err := p.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err = p.Start(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
doneCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(output)
|
|
|
|
if !scanner.Scan() {
|
|
|
|
doneCh <- errors.New("unable to find Keybase username")
|
|
|
|
return
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
text := scanner.Text()
|
|
|
|
toks := strings.Fields(text)
|
2019-08-26 12:00:31 -07:00
|
|
|
if len(toks) != 2 {
|
2020-02-08 07:50:16 -08:00
|
|
|
doneCh <- fmt.Errorf("invalid Keybase username output: %q", text)
|
2019-08-26 12:00:31 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
username = toks[1]
|
|
|
|
doneCh <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err = <-doneCh:
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
return "", errors.New("unable to run Keybase command")
|
|
|
|
}
|
|
|
|
|
|
|
|
return username, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type OneshotOptions struct {
|
|
|
|
Username string
|
|
|
|
PaperKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RunOptions struct {
|
|
|
|
KeybaseLocation string
|
|
|
|
HomeDir string
|
|
|
|
Oneshot *OneshotOptions
|
|
|
|
StartService bool
|
2020-02-08 07:50:16 -08:00
|
|
|
// Have the bot send/receive typing notifications
|
|
|
|
EnableTyping bool
|
|
|
|
// Disable bot lite mode
|
|
|
|
DisableBotLiteMode bool
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r RunOptions) Location() string {
|
|
|
|
if r.KeybaseLocation == "" {
|
|
|
|
return "keybase"
|
|
|
|
}
|
|
|
|
return r.KeybaseLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RunOptions) Command(args ...string) *exec.Cmd {
|
|
|
|
var cmd []string
|
|
|
|
if r.HomeDir != "" {
|
|
|
|
cmd = append(cmd, "--home", r.HomeDir)
|
|
|
|
}
|
|
|
|
cmd = append(cmd, args...)
|
|
|
|
return exec.Command(r.Location(), cmd...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start fires up the Keybase JSON API in stdin/stdout mode
|
|
|
|
func Start(runOpts RunOptions) (*API, error) {
|
|
|
|
api := &API{
|
|
|
|
runOpts: runOpts,
|
|
|
|
}
|
|
|
|
if err := api.startPipes(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return api, nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 07:50:16 -08:00
|
|
|
func (a *API) Command(args ...string) *exec.Cmd {
|
|
|
|
return a.runOpts.Command(args...)
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:00:31 -07:00
|
|
|
func (a *API) auth() (string, error) {
|
|
|
|
username, err := getUsername(a.runOpts)
|
|
|
|
if err == nil {
|
|
|
|
return username, nil
|
|
|
|
}
|
|
|
|
if a.runOpts.Oneshot == nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
username = ""
|
|
|
|
// If a paper key is specified, then login with oneshot mode (logout first)
|
|
|
|
if a.runOpts.Oneshot != nil {
|
|
|
|
if username == a.runOpts.Oneshot.Username {
|
|
|
|
// just get out if we are on the desired user already
|
|
|
|
return username, nil
|
|
|
|
}
|
|
|
|
if err := a.runOpts.Command("logout", "-f").Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := a.runOpts.Command("oneshot", "--username", a.runOpts.Oneshot.Username, "--paperkey",
|
|
|
|
a.runOpts.Oneshot.PaperKey).Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
username = a.runOpts.Oneshot.Username
|
|
|
|
return username, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("unable to auth")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) startPipes() (err error) {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
if a.apiCmd != nil {
|
2020-02-08 07:50:16 -08:00
|
|
|
if err := a.apiCmd.Process.Kill(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
a.apiCmd = nil
|
|
|
|
|
|
|
|
if a.runOpts.StartService {
|
2020-02-08 07:50:16 -08:00
|
|
|
args := []string{fmt.Sprintf("-enable-bot-lite-mode=%v", a.runOpts.DisableBotLiteMode), "service"}
|
|
|
|
if err := a.runOpts.Command(args...).Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.username, err = a.auth(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
|
|
|
|
cmd := a.runOpts.Command("chat", "notification-settings", fmt.Sprintf("-disable-typing=%v", !a.runOpts.EnableTyping))
|
|
|
|
if err = cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:00:31 -07:00
|
|
|
a.apiCmd = a.runOpts.Command("chat", "api")
|
|
|
|
if a.apiInput, err = a.apiCmd.StdinPipe(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
output, err := a.apiCmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.apiCmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.apiOutput = bufio.NewReader(output)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errAPIDisconnected = errors.New("chat API disconnected")
|
|
|
|
|
|
|
|
func (a *API) getAPIPipesLocked() (io.Writer, *bufio.Reader, error) {
|
|
|
|
// this should only be called inside a lock
|
|
|
|
if a.apiCmd == nil {
|
|
|
|
return nil, nil, errAPIDisconnected
|
|
|
|
}
|
|
|
|
return a.apiInput, a.apiOutput, nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 07:50:16 -08:00
|
|
|
func (a *API) GetUsername() string {
|
|
|
|
return a.username
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
2020-02-08 07:50:16 -08:00
|
|
|
func (a *API) doSend(arg interface{}) (resp SendResponse, err error) {
|
2019-08-26 12:00:31 -07:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
bArg, err := json.Marshal(arg)
|
|
|
|
if err != nil {
|
|
|
|
return SendResponse{}, err
|
|
|
|
}
|
|
|
|
input, output, err := a.getAPIPipesLocked()
|
|
|
|
if err != nil {
|
|
|
|
return SendResponse{}, err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(input, string(bArg)); err != nil {
|
|
|
|
return SendResponse{}, err
|
|
|
|
}
|
|
|
|
responseRaw, err := output.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
return SendResponse{}, err
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
if err := json.Unmarshal(responseRaw, &resp); err != nil {
|
|
|
|
return resp, fmt.Errorf("failed to decode API response: %s", err)
|
|
|
|
} else if resp.Error != nil {
|
|
|
|
return resp, errors.New(resp.Error.Message)
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
return resp, nil
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) doFetch(apiInput string) ([]byte, error) {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
input, output, err := a.getAPIPipesLocked()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := io.WriteString(input, apiInput); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
byteOutput, err := output.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return byteOutput, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscriptionMessage contains a message and conversation object
|
|
|
|
type SubscriptionMessage struct {
|
2020-02-08 07:50:16 -08:00
|
|
|
Message chat1.MsgSummary
|
|
|
|
Conversation chat1.ConvSummary
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionConversation struct {
|
|
|
|
Conversation chat1.ConvSummary
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionWalletEvent struct {
|
2020-02-08 07:50:16 -08:00
|
|
|
Payment stellar1.PaymentDetailsLocal
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewSubscription has methods to control the background message fetcher loop
|
|
|
|
type NewSubscription struct {
|
2020-02-08 07:50:16 -08:00
|
|
|
sync.Mutex
|
|
|
|
|
2019-08-26 12:00:31 -07:00
|
|
|
newMsgsCh <-chan SubscriptionMessage
|
2020-02-08 07:50:16 -08:00
|
|
|
newConvsCh <-chan SubscriptionConversation
|
2019-08-26 12:00:31 -07:00
|
|
|
newWalletCh <-chan SubscriptionWalletEvent
|
|
|
|
errorCh <-chan error
|
2020-02-08 07:50:16 -08:00
|
|
|
running bool
|
2019-08-26 12:00:31 -07:00
|
|
|
shutdownCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read blocks until a new message arrives
|
2020-02-08 07:50:16 -08:00
|
|
|
func (m *NewSubscription) Read() (SubscriptionMessage, error) {
|
2019-08-26 12:00:31 -07:00
|
|
|
select {
|
|
|
|
case msg := <-m.newMsgsCh:
|
|
|
|
return msg, nil
|
|
|
|
case err := <-m.errorCh:
|
|
|
|
return SubscriptionMessage{}, err
|
2020-02-08 07:50:16 -08:00
|
|
|
case <-m.shutdownCh:
|
|
|
|
return SubscriptionMessage{}, errors.New("Subscription shutdown")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *NewSubscription) ReadNewConvs() (SubscriptionConversation, error) {
|
|
|
|
select {
|
|
|
|
case conv := <-m.newConvsCh:
|
|
|
|
return conv, nil
|
|
|
|
case err := <-m.errorCh:
|
|
|
|
return SubscriptionConversation{}, err
|
|
|
|
case <-m.shutdownCh:
|
|
|
|
return SubscriptionConversation{}, errors.New("Subscription shutdown")
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read blocks until a new message arrives
|
2020-02-08 07:50:16 -08:00
|
|
|
func (m *NewSubscription) ReadWallet() (SubscriptionWalletEvent, error) {
|
2019-08-26 12:00:31 -07:00
|
|
|
select {
|
|
|
|
case msg := <-m.newWalletCh:
|
|
|
|
return msg, nil
|
|
|
|
case err := <-m.errorCh:
|
|
|
|
return SubscriptionWalletEvent{}, err
|
2020-02-08 07:50:16 -08:00
|
|
|
case <-m.shutdownCh:
|
|
|
|
return SubscriptionWalletEvent{}, errors.New("Subscription shutdown")
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown terminates the background process
|
2020-02-08 07:50:16 -08:00
|
|
|
func (m *NewSubscription) Shutdown() {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
if m.running {
|
|
|
|
close(m.shutdownCh)
|
|
|
|
m.running = false
|
|
|
|
}
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListenOptions struct {
|
|
|
|
Wallet bool
|
2020-02-08 07:50:16 -08:00
|
|
|
Convs bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type PaymentHolder struct {
|
|
|
|
Payment stellar1.PaymentDetailsLocal `json:"notification"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TypeHolder struct {
|
|
|
|
Type string `json:"type"`
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListenForNewTextMessages proxies to Listen without wallet events
|
2020-02-08 07:50:16 -08:00
|
|
|
func (a *API) ListenForNewTextMessages() (*NewSubscription, error) {
|
2019-08-26 12:00:31 -07:00
|
|
|
opts := ListenOptions{Wallet: false}
|
|
|
|
return a.Listen(opts)
|
|
|
|
}
|
|
|
|
|
2020-02-08 07:50:16 -08:00
|
|
|
func (a *API) registerSubscription(sub *NewSubscription) {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
a.subscriptions = append(a.subscriptions, sub)
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:00:31 -07:00
|
|
|
// Listen fires of a background loop and puts chat messages and wallet
|
|
|
|
// events into channels
|
2020-02-08 07:50:16 -08:00
|
|
|
func (a *API) Listen(opts ListenOptions) (*NewSubscription, error) {
|
|
|
|
newMsgsCh := make(chan SubscriptionMessage, 100)
|
|
|
|
newConvsCh := make(chan SubscriptionConversation, 100)
|
2019-08-26 12:00:31 -07:00
|
|
|
newWalletCh := make(chan SubscriptionWalletEvent, 100)
|
|
|
|
errorCh := make(chan error, 100)
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
2020-02-08 07:50:16 -08:00
|
|
|
sub := &NewSubscription{
|
|
|
|
newMsgsCh: newMsgsCh,
|
|
|
|
newConvsCh: newConvsCh,
|
2019-08-26 12:00:31 -07:00
|
|
|
newWalletCh: newWalletCh,
|
|
|
|
shutdownCh: shutdownCh,
|
|
|
|
errorCh: errorCh,
|
2020-02-08 07:50:16 -08:00
|
|
|
running: true,
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
a.registerSubscription(sub)
|
2019-08-26 12:00:31 -07:00
|
|
|
pause := 2 * time.Second
|
|
|
|
readScanner := func(boutput *bufio.Scanner) {
|
|
|
|
for {
|
|
|
|
boutput.Scan()
|
|
|
|
t := boutput.Text()
|
|
|
|
var typeHolder TypeHolder
|
|
|
|
if err := json.Unmarshal([]byte(t), &typeHolder); err != nil {
|
|
|
|
errorCh <- err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch typeHolder.Type {
|
|
|
|
case "chat":
|
2020-02-08 07:50:16 -08:00
|
|
|
var notification chat1.MsgNotification
|
|
|
|
if err := json.Unmarshal([]byte(t), ¬ification); err != nil {
|
2019-08-26 12:00:31 -07:00
|
|
|
errorCh <- err
|
|
|
|
break
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
if notification.Error != nil {
|
|
|
|
log.Printf("error message received: %s", *notification.Error)
|
|
|
|
} else if notification.Msg != nil {
|
|
|
|
subscriptionMessage := SubscriptionMessage{
|
|
|
|
Message: *notification.Msg,
|
|
|
|
Conversation: chat1.ConvSummary{
|
|
|
|
Id: notification.Msg.ConvID,
|
|
|
|
Channel: notification.Msg.Channel,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newMsgsCh <- subscriptionMessage
|
|
|
|
}
|
|
|
|
case "chat_conv":
|
|
|
|
var notification chat1.ConvNotification
|
|
|
|
if err := json.Unmarshal([]byte(t), ¬ification); err != nil {
|
|
|
|
errorCh <- err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if notification.Error != nil {
|
|
|
|
log.Printf("error message received: %s", *notification.Error)
|
|
|
|
} else if notification.Conv != nil {
|
|
|
|
subscriptionConv := SubscriptionConversation{
|
|
|
|
Conversation: *notification.Conv,
|
|
|
|
}
|
|
|
|
newConvsCh <- subscriptionConv
|
2019-08-26 12:00:31 -07:00
|
|
|
}
|
|
|
|
case "wallet":
|
|
|
|
var holder PaymentHolder
|
|
|
|
if err := json.Unmarshal([]byte(t), &holder); err != nil {
|
|
|
|
errorCh <- err
|
|
|
|
break
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
subscriptionPayment := SubscriptionWalletEvent(holder)
|
2019-08-26 12:00:31 -07:00
|
|
|
newWalletCh <- subscriptionPayment
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
attempts := 0
|
|
|
|
maxAttempts := 1800
|
|
|
|
go func() {
|
|
|
|
for {
|
2020-02-08 07:50:16 -08:00
|
|
|
select {
|
|
|
|
case <-shutdownCh:
|
|
|
|
log.Printf("Listen: received shutdown")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:00:31 -07:00
|
|
|
if attempts >= maxAttempts {
|
|
|
|
panic("Listen: failed to auth, giving up")
|
|
|
|
}
|
|
|
|
attempts++
|
|
|
|
if _, err := a.auth(); err != nil {
|
|
|
|
log.Printf("Listen: failed to auth: %s", err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmdElements := []string{"chat", "api-listen"}
|
|
|
|
if opts.Wallet {
|
|
|
|
cmdElements = append(cmdElements, "--wallet")
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
if opts.Convs {
|
|
|
|
cmdElements = append(cmdElements, "--convs")
|
|
|
|
}
|
2019-08-26 12:00:31 -07:00
|
|
|
p := a.runOpts.Command(cmdElements...)
|
|
|
|
output, err := p.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Listen: failed to listen: %s", err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
continue
|
|
|
|
}
|
2020-02-08 07:50:16 -08:00
|
|
|
stderr, err := p.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Listen: failed to listen to stderr: %s", err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-26 12:00:31 -07:00
|
|
|
boutput := bufio.NewScanner(output)
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
log.Printf("Listen: failed to make listen scanner: %s", err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
attempts = 0
|
|
|
|
go readScanner(boutput)
|
|
|
|
<-done
|
2020-02-08 07:50:16 -08:00
|
|
|
if err := p.Wait(); err != nil {
|
|
|
|
stderrBytes, rerr := ioutil.ReadAll(stderr)
|
|
|
|
if rerr != nil {
|
|
|
|
stderrBytes = []byte("failed to get stderr")
|
|
|
|
}
|
|
|
|
log.Printf("Listen: failed to Wait for command: %s (```%s```)", err, stderrBytes)
|
|
|
|
}
|
2019-08-26 12:00:31 -07:00
|
|
|
time.Sleep(pause)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) LogSend(feedback string) error {
|
|
|
|
feedback = "go-keybase-chat-bot log send\n" +
|
|
|
|
"username: " + a.GetUsername() + "\n" +
|
|
|
|
feedback
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"log", "send",
|
|
|
|
"--no-confirm",
|
|
|
|
"--feedback", feedback,
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're determining whether the service is already running by running status
|
|
|
|
// with autofork disabled.
|
|
|
|
if err := a.runOpts.Command("--no-auto-fork", "status"); err != nil {
|
|
|
|
// Assume that there's no service running, so log send as standalone
|
|
|
|
args = append([]string{"--standalone"}, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.runOpts.Command(args...).Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) Shutdown() error {
|
2020-02-08 07:50:16 -08:00
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
for _, sub := range a.subscriptions {
|
|
|
|
sub.Shutdown()
|
|
|
|
}
|
|
|
|
|
2019-08-26 12:00:31 -07:00
|
|
|
if a.runOpts.Oneshot != nil {
|
|
|
|
err := a.runOpts.Command("logout", "--force").Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.runOpts.StartService {
|
|
|
|
err := a.runOpts.Command("ctl", "stop", "--shutdown").Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|