Browse Source

split NewChatAPI to make it easier to mock the api for tests later

dxb/another-api-abstraction-attempt
Sam Hofius 2 years ago
parent
commit
7e054c0735
  1. 38
      chat.go

38
chat.go

@ -4,6 +4,7 @@ import ( @@ -4,6 +4,7 @@ import (
"bufio"
"context"
"fmt"
"io"
"sync"
"git.hugfreevikings.wtf/keybase/keybase/pkg/ctxreader"
@ -60,10 +61,31 @@ func NewChatAPI(ctx context.Context, opts Options) (*ChatAPI, error) { @@ -60,10 +61,31 @@ func NewChatAPI(ctx context.Context, opts Options) (*ChatAPI, error) {
if err != nil {
return nil, fmt.Errorf("failed to create chat api-listen command")
}
_, chatListenStdout, _ := chatListenAPI.GetPipes()
chatStdin, chatStdout, chatStderr := chatAPI.GetPipes()
_, chatListenStdout, _ := chatListenAPI.GetPipes()
// connect the pipes to the channels
err = connectChatPipes(ctx, &api, chatStdin, chatStdout, chatStderr, chatListenStdout)
if err != nil {
return nil, fmt.Errorf("failed to connect chat pipes: %v", err)
}
// start the api commands
err = chatAPI.Start()
if err != nil {
return nil, fmt.Errorf("failed to start chat api: %v", err)
}
err = chatListenAPI.Start()
if err != nil {
return nil, fmt.Errorf("failed to start chat listen-api: %v", err)
}
// create the goroutines
return &api, nil
}
// connectChatPipes creates goroutines to send messages from the keybase binary into the
// appropriate channels to be processed
func connectChatPipes(ctx context.Context, api *ChatAPI, chatStdin io.WriteCloser, chatStdout, chatStderr, chatListenStdout io.ReadCloser) error {
// listen reader
go func() {
cr := ctxreader.NewContextReader(ctx, chatListenStdout)
@ -106,17 +128,7 @@ func NewChatAPI(ctx context.Context, opts Options) (*ChatAPI, error) { @@ -106,17 +128,7 @@ func NewChatAPI(ctx context.Context, opts Options) (*ChatAPI, error) {
}
}()
// then start the cmds
err = chatAPI.Start()
if err != nil {
return nil, err
}
err = chatListenAPI.Start()
if err != nil {
return nil, err
}
return &api, nil
return nil
}
func (c *ChatAPI) SendRaw(ctx context.Context, msg []byte) ([]byte, error) {

Loading…
Cancel
Save