Skip to content

Commit

Permalink
- Refactoring io.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristoffer Ahl committed May 17, 2019
1 parent ac7bfa3 commit 39f3e10
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
6 changes: 1 addition & 5 deletions cmd/centry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ func main() {
args := os.Args[1:]

// Create the context
context := centry.NewContext(centry.CLI, io.InputOutput{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
context := centry.NewContext(centry.CLI, io.Standard())

// Create the runtime
runtime := centry.Create(args, context)
Expand Down
6 changes: 1 addition & 5 deletions pkg/centry/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ func execCentry(source string, quiet bool) *execResult {
if quiet {
source = fmt.Sprintf("--quiet %s", source)
}
context := NewContext(CLI, io.InputOutput{
Stdin: nil,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
context := NewContext(CLI, io.Headless())
runtime := Create(strings.Split(fmt.Sprintf("../../test/data/main_test.yaml %s", source), " "), context)
exitCode = runtime.Execute()
})
Expand Down
9 changes: 2 additions & 7 deletions pkg/centry/serve_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package centry

import (
"bytes"
"encoding/json"
"net/http"
"strings"
Expand Down Expand Up @@ -68,7 +67,6 @@ func executeHandler(manifest *config.Manifest) func(w http.ResponseWriter, r *ht
response := api.ExecuteResponse{}

var body api.ExecuteRequest
var buf bytes.Buffer

decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&body)
Expand All @@ -81,11 +79,8 @@ func executeHandler(manifest *config.Manifest) func(w http.ResponseWriter, r *ht
args = append(args, strings.Fields(body.Args)...)

// Build
context := NewContext(API, io.InputOutput{
Stdin: nil,
Stdout: &buf,
Stderr: &buf,
})
io, buf := io.BufferedCombined()
context := NewContext(API, io)

context.commandEnabled = func(cmd config.Command) bool {
if cmd.Annotations == nil || cmd.Annotations[config.APIServeAnnotation] != "true" {
Expand Down
41 changes: 41 additions & 0 deletions pkg/io/io.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io

import (
"bytes"
"io"
"os"
)

// InputOutput holds the reader and writers used during execution
Expand All @@ -10,3 +12,42 @@ type InputOutput struct {
Stdout io.Writer
Stderr io.Writer
}

// Standard creates InputOutput for use from a terminal
func Standard() InputOutput {
return InputOutput{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}

// Headless creates InputOutput for use from a terminal that can't accept input
func Headless() InputOutput {
return InputOutput{
Stdin: nil,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}

// Buffered creates a buffered InputOutput object
func Buffered() (io InputOutput, stdout *bytes.Buffer, stderr *bytes.Buffer) {
var bufOut bytes.Buffer
var bufErr bytes.Buffer
return InputOutput{
Stdin: nil,
Stdout: &bufOut,
Stderr: &bufErr,
}, &bufOut, &bufErr
}

// BufferedCombined creates a buffered InputOutput object
func BufferedCombined() (InputOutput, *bytes.Buffer) {
var buf bytes.Buffer
return InputOutput{
Stdin: nil,
Stdout: &buf,
Stderr: &buf,
}, &buf
}
8 changes: 1 addition & 7 deletions pkg/shell/bash.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package shell

import (
"bytes"
"fmt"
"os/exec"
"path"
Expand Down Expand Up @@ -63,12 +62,7 @@ func (s *BashScript) FullPath() string {
func (s *BashScript) Functions() ([]string, error) {
callArgs := []string{"-c", fmt.Sprintf("source %s; declare -F", s.FullPath())}

var buf bytes.Buffer
io := io.InputOutput{
Stdin: nil,
Stdout: &buf,
Stderr: &buf,
}
io, buf := io.BufferedCombined()

err := NewBash().Run(io, callArgs)
if err != nil {
Expand Down

0 comments on commit 39f3e10

Please sign in to comment.