Skip to content

Commit

Permalink
move real impls to cmd to avoid coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh authored and knative-prow-robot committed Jan 29, 2019
1 parent 8c7a384 commit 7b4b2ed
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 154 deletions.
78 changes: 74 additions & 4 deletions cmd/entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ package main

import (
"flag"
"log"
"os"
"os/exec"
"syscall"
"time"

"github.com/knative/build-pipeline/pkg/entrypoint"
)
Expand All @@ -36,10 +41,75 @@ func main() {
WaitFile: *waitFile,
PostFile: *postFile,
Args: flag.Args(),
Waiter: &entrypoint.RealWaiter{},
Runner: &entrypoint.RealRunner{},
PostWriter: &entrypoint.RealPostWriter{},
Waiter: &RealWaiter{},
Runner: &RealRunner{},
PostWriter: &RealPostWriter{},
}.Go()
}

// TODO(jasonhall): Test that original exit code is propagated and that stdout/stderr are collected -- needs e2e tests.
// TODO(jasonhall): Test that original exit code is propagated and that
// stdout/stderr are collected -- needs e2e tests.

// RealWaiter actually waits for files, by polling.
type RealWaiter struct{ waitFile string }

var _ entrypoint.Waiter = (*RealWaiter)(nil)

func (*RealWaiter) Wait(file string) {
if file == "" {
return
}
for ; ; time.Sleep(time.Second) {
if _, err := os.Stat(file); err == nil {
return
} else if !os.IsNotExist(err) {
log.Fatalf("Waiting for %q: %v", file, err)
}
}
}

// RealRunner actually runs commands.
type RealRunner struct{}

var _ entrypoint.Runner = (*RealRunner)(nil)

func (*RealRunner) Run(args ...string) {
if len(args) == 0 {
return
}
name, args := args[0], args[1:]

cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// Copied from https://stackoverflow.com/questions/10385551/get-exit-code-go
// This works on both Unix and Windows. Although
// package syscall is generally platform dependent,
// WaitStatus is defined for both Unix and Windows and
// in both cases has an ExitStatus() method with the
// same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
os.Exit(status.ExitStatus())
}
log.Fatalf("Error executing command (ExitError): %v", err)
}
log.Fatalf("Error executing command: %v", err)
}
}

// RealPostWriter actually writes files.
type RealPostWriter struct{}

var _ entrypoint.PostWriter = (*RealPostWriter)(nil)

func (*RealPostWriter) Write(file string) {
if file == "" {
return
}
if _, err := os.Create(file); err != nil {
log.Fatalf("Creating %q: %v", file, err)
}
}
17 changes: 17 additions & 0 deletions pkg/entrypoint/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ type Entrypointer struct {
PostWriter PostWriter
}

// Waiter encapsulates waiting for files to exist.
type Waiter interface {
// Wait blocks until the specified file exists.
Wait(file string)
}

// Runner encapsulates running commands.
type Runner interface {
Run(args ...string)
}

// PostWriter encapsulates writing a file when complete.
type PostWriter interface {
// Write writes to the path when complete.
Write(file string)
}

// Go optionally waits for a file, runs the command, and writes a
// post file.
func (e Entrypointer) Go() {
Expand Down
42 changes: 0 additions & 42 deletions pkg/entrypoint/postwriter.go

This file was deleted.

61 changes: 0 additions & 61 deletions pkg/entrypoint/runner.go

This file was deleted.

47 changes: 0 additions & 47 deletions pkg/entrypoint/waiter.go

This file was deleted.

0 comments on commit 7b4b2ed

Please sign in to comment.