Skip to content

Commit 850482a

Browse files
authored
Merge pull request #7 from bugout-dev/fix-trap-output
Updated "bugout trap" to respect stdin
2 parents 7a238da + 463304d commit 850482a

5 files changed

Lines changed: 57 additions & 21 deletions

File tree

cmd/bugout/cmdutils/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,12 @@ func CompositePopulator(populators ...cobra.PositionalArgs) cobra.PositionalArgs
7474
return nil
7575
}
7676
}
77+
78+
func BugoutURL() string {
79+
defaultBugoutURL := "https://bugout.dev"
80+
bugoutURL := os.Getenv("BUGOUT_URL")
81+
if bugoutURL == "" {
82+
bugoutURL = defaultBugoutURL
83+
}
84+
return bugoutURL
85+
}

cmd/bugout/trap/command.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package trapcmd
22

33
import (
4-
"encoding/json"
54
"errors"
5+
"fmt"
66

77
"github.com/spf13/cobra"
88

@@ -40,7 +40,7 @@ Specify the wrapped command using "--" followed by the command:
4040
return nil
4141
},
4242
RunE: func(cmd *cobra.Command, args []string) error {
43-
result, err := RunWrappedCommand(args)
43+
result, err := RunWrappedCommand(cmd, args)
4444
if err != nil {
4545
return err
4646
}
@@ -57,8 +57,8 @@ Specify the wrapped command using "--" followed by the command:
5757
return err
5858
}
5959

60-
encodeErr := json.NewEncoder(cmd.OutOrStdout()).Encode(response)
61-
return encodeErr
60+
cmd.ErrOrStderr().Write([]byte(fmt.Sprintf("\n\nBugout entry created at: %s/journals/%s/%s\n", cmdutils.BugoutURL(), journalID, response.Id)))
61+
return nil
6262
},
6363
}
6464

cmd/bugout/trap/invoke.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
package trapcmd
22

33
import (
4-
"bytes"
4+
"io"
55
"os/exec"
6+
"strings"
7+
"sync"
8+
9+
"github.com/spf13/cobra"
610
)
711

812
type InvocationResult struct {
9-
ExitCode int
10-
OutBuffer *bytes.Buffer
11-
ErrBuffer *bytes.Buffer
13+
ExitCode int
14+
Stdout string
15+
Stderr string
1216
}
1317

14-
func RunWrappedCommand(invocation []string) (InvocationResult, error) {
18+
func RunWrappedCommand(trapCmd *cobra.Command, invocation []string) (*InvocationResult, error) {
19+
var wg sync.WaitGroup
1520
cmd := exec.Command(invocation[0], invocation[1:]...)
16-
var outBuffer, errBuffer bytes.Buffer
17-
cmd.Stdout = &outBuffer
18-
cmd.Stderr = &errBuffer
21+
cmd.Stdin = trapCmd.InOrStdin()
22+
23+
outReader, stdoutPipeErr := cmd.StdoutPipe()
24+
if stdoutPipeErr != nil {
25+
return &InvocationResult{}, stdoutPipeErr
26+
}
27+
errReader, stderrPipeErr := cmd.StderrPipe()
28+
if stderrPipeErr != nil {
29+
return &InvocationResult{}, stderrPipeErr
30+
}
31+
32+
var outBuilder, errBuilder strings.Builder
33+
stdoutReader := io.TeeReader(outReader, &outBuilder)
34+
stderrReader := io.TeeReader(errReader, &errBuilder)
35+
36+
wg.Add(1)
37+
go func() {
38+
defer wg.Done()
39+
io.Copy(trapCmd.OutOrStdout(), stdoutReader)
40+
}()
41+
wg.Add(1)
42+
go func() {
43+
defer wg.Done()
44+
io.Copy(trapCmd.ErrOrStderr(), stderrReader)
45+
}()
1946

2047
exitCode := 0
2148

@@ -31,6 +58,6 @@ func RunWrappedCommand(invocation []string) (InvocationResult, error) {
3158
err = runErr
3259
}
3360
}
34-
35-
return InvocationResult{ExitCode: exitCode, OutBuffer: &outBuffer, ErrBuffer: &errBuffer}, err
61+
wg.Wait()
62+
return &InvocationResult{ExitCode: exitCode, Stdout: outBuilder.String(), Stderr: errBuilder.String()}, err
3663
}

cmd/bugout/trap/render.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ type TrapEntry struct {
1717
Context spire.EntryContext
1818
}
1919

20-
func Render(invocation []string, result InvocationResult, title string, tags []string, showEnv bool) TrapEntry {
20+
func Render(invocation []string, result *InvocationResult, title string, tags []string, showEnv bool) TrapEntry {
2121
renderedTitle := RenderTitle(invocation, result, title, tags, showEnv)
2222
renderedTags := RenderTags(invocation, result, title, tags, showEnv)
2323
renderedContent := RenderContent(invocation, result, title, tags, showEnv)
2424
return TrapEntry{Title: renderedTitle, Content: renderedContent, Tags: renderedTags, Context: spire.EntryContext{ContextType: "trap"}}
2525
}
2626

27-
func RenderTitle(invocation []string, result InvocationResult, title string, tags []string, showEnv bool) string {
27+
func RenderTitle(invocation []string, result *InvocationResult, title string, tags []string, showEnv bool) string {
2828
if title != "" {
2929
return title
3030
}
3131
return fmt.Sprintf("Command: %s (exited with code %d)", invocation[0], result.ExitCode)
3232
}
3333

34-
func RenderTags(invocation []string, result InvocationResult, title string, tags []string, showEnv bool) []string {
34+
func RenderTags(invocation []string, result *InvocationResult, title string, tags []string, showEnv bool) []string {
3535
trapTags := []string{
3636
"trap",
3737
"cli",
@@ -43,7 +43,7 @@ func RenderTags(invocation []string, result InvocationResult, title string, tags
4343
return finalTags
4444
}
4545

46-
func RenderContent(invocation []string, result InvocationResult, title string, tags []string, showEnv bool) string {
46+
func RenderContent(invocation []string, result *InvocationResult, title string, tags []string, showEnv bool) string {
4747
quotedInvocation := make([]string, len(invocation))
4848
for i, component := range invocation {
4949
quotedInvocation[i] = strconv.Quote(component)
@@ -59,8 +59,8 @@ func RenderContent(invocation []string, result InvocationResult, title string, t
5959
var content string = strings.Join([]string{
6060
fmt.Sprintf("## invocation\n```\n%s\n```\n", strings.Join(quotedInvocation, " ")),
6161
fmt.Sprintf("## exit code\n`%d`\n", result.ExitCode),
62-
fmt.Sprintf("## stdout\n```\n%s\n```\n", result.OutBuffer.String()),
63-
fmt.Sprintf("## stderr\n```\n%s\n```\n", result.ErrBuffer.String()),
62+
fmt.Sprintf("## stdout\n```\n%s\n```\n", result.Stdout),
63+
fmt.Sprintf("## stderr\n```\n%s\n```\n", result.Stderr),
6464
}, "\n")
6565

6666
if showEnv {

pkg/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package bugout
22

3-
const Version string = "0.2.0"
3+
const Version string = "0.3.0"

0 commit comments

Comments
 (0)