Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions shell/assertions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package shell_test

import (
"strings"
"testing"
)

type assertions struct {
t *testing.T
}

func (a assertions) NoError(err error) {
if err != nil {
a.t.Error(err)
}
}

func (a assertions) Contains(haystack, needle string) {
if !strings.Contains(haystack, needle) {
a.t.Errorf("wanted to \ncontain: %#v\n in: %#v",
needle, haystack)
}
}

func (a assertions) Equal(want, got string) {
if got != want {
a.t.Errorf("want: %#v\n got:%#v", want, got)
}
}
Comment on lines +17 to +45
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use testify for that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid adding dependencies, as that opens a pandora box, of whether to vendor them or not.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this pandora box has to be opened at some point right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily. In the ideal world I would see ALL contents of this repo, gradually being deprecated, and replaced by dedicated tools, and Golang code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly, to develop this golang code (eg embed other tools) you'll probably need managing dependencies anyway right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, But maybe in other repos then knative/hack?!? IDK

189 changes: 189 additions & 0 deletions shell/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package shell

import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
)

const (
defaultLabelOut = "[OUT]"
defaultLabelErr = "[ERR]"
executeMode = 0700
)

// ErrNoProjectLocation is returned if user didnt provided the project location.
var ErrNoProjectLocation = errors.New("project location isn't provided")

// NewExecutor creates a new executor from given config.
func NewExecutor(config ExecutorConfig) Executor {
configureDefaultValues(&config)
return &streamingExecutor{
ExecutorConfig: config,
}
}

// RunScript executes a shell script with args.
func (s *streamingExecutor) RunScript(script Script, args ...string) error {
err := validate(s.ExecutorConfig)
if err != nil {
return err
}
cnt := script.scriptContent(s.ProjectLocation, args)
return withTempScript(cnt, func(bin string) error {
return stream(bin, s.ExecutorConfig, script.Label)
})
}

// RunFunction executes a shell function with args.
func (s *streamingExecutor) RunFunction(fn Function, args ...string) error {
err := validate(s.ExecutorConfig)
if err != nil {
return err
}
cnt := fn.scriptContent(s.ProjectLocation, args)
return withTempScript(cnt, func(bin string) error {
return stream(bin, s.ExecutorConfig, fn.Label)
})
}

type streamingExecutor struct {
ExecutorConfig
}

func validate(config ExecutorConfig) error {
if config.ProjectLocation == nil {
return ErrNoProjectLocation
}
return nil
}

func configureDefaultValues(config *ExecutorConfig) {
if config.Out == nil {
config.Out = os.Stdout
}
if config.Err == nil {
config.Err = os.Stderr
}
if config.LabelOut == "" {
config.LabelOut = defaultLabelOut
}
if config.LabelErr == "" {
config.LabelErr = defaultLabelErr
}
if config.Environ == nil {
config.Environ = os.Environ()
}
if !config.SkipDate && config.DateFormat == "" {
config.DateFormat = time.StampMilli
}
if config.PrefixFunc == nil {
config.PrefixFunc = defaultPrefixFunc
}
}

func stream(bin string, cfg ExecutorConfig, label string) error {
c := exec.Command(bin)
c.Env = cfg.Environ
c.Stdout = NewPrefixer(cfg.Out, prefixFunc(StreamTypeOut, label, cfg))
c.Stderr = NewPrefixer(cfg.Err, prefixFunc(StreamTypeErr, label, cfg))
return c.Run()
}

func prefixFunc(st StreamType, label string, cfg ExecutorConfig) func() string {
return func() string {
return cfg.PrefixFunc(st, label, cfg)
}
}

func defaultPrefixFunc(st StreamType, label string, cfg ExecutorConfig) string {
sep := " "
var buf []string
if !cfg.SkipDate {
dt := time.Now().Format(cfg.DateFormat)
buf = append(buf, dt)
}
buf = append(buf, label)
switch st {
case StreamTypeOut:
buf = append(buf, cfg.LabelOut)
case StreamTypeErr:
buf = append(buf, cfg.LabelErr)
}
return strings.Join(buf, sep) + sep
}

func withTempScript(contents string, fn func(bin string) error) error {
tmpfile, err := ioutil.TempFile("", "shellout-*.sh")
if err != nil {
return err
}
_, err = tmpfile.WriteString(contents)
if err != nil {
return err
}
err = tmpfile.Chmod(executeMode)
if err != nil {
return err
}
err = tmpfile.Close()
if err != nil {
return err
}
defer func() {
// clean up
_ = os.Remove(tmpfile.Name())
}()

return fn(tmpfile.Name())
}

func (fn *Function) scriptContent(location ProjectLocation, args []string) string {
return fmt.Sprintf(`#!/usr/bin/env bash

set -Eeuo pipefail

cd "%s"
source %s

%s %s
`, location.RootPath(), fn.ScriptPath, fn.FunctionName, quoteArgs(args))
}

func (sc *Script) scriptContent(location ProjectLocation, args []string) string {
return fmt.Sprintf(`#!/usr/bin/env bash

set -Eeuo pipefail

cd "%s"
%s %s
`, location.RootPath(), sc.ScriptPath, quoteArgs(args))
}

func quoteArgs(args []string) string {
quoted := make([]string, len(args))
for i, arg := range args {
quoted[i] = "\"" + strings.ReplaceAll(arg, "\"", "\\\"") + "\""
}
return strings.Join(quoted, " ")
}
160 changes: 160 additions & 0 deletions shell/executor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package shell_test

import (
"bytes"
"testing"

"knative.dev/hack/shell"
)

func TestNewExecutor(t *testing.T) {
assert := assertions{t: t}
tests := []testcase{
helloWorldTestCase(t),
abortTestCase(t),
failExampleCase(t),
missingProjectLocationCase(),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var outB, errB bytes.Buffer
tt.config.Out = &outB
tt.config.Err = &errB
executor := shell.NewExecutor(tt.config)
err := tt.op(executor)
if err != nil && !tt.wants.failed {
t.Errorf("%s: \n got: %#v\nfailed: %#v", tt.name, err, tt.failed)
}

for _, wantOut := range tt.wants.outs {
assert.Contains(outB.String(), wantOut)
}
for _, wantErr := range tt.wants.errs {
assert.Contains(errB.String(), wantErr)
}
})
}
}

func TestExecutorDefaults(t *testing.T) {
assert := assertions{t: t}
loc, err := shell.NewProjectLocation("..")
assert.NoError(err)
exec := shell.NewExecutor(shell.ExecutorConfig{
ProjectLocation: loc,
})
err = exec.RunFunction(fn("true"))
assert.NoError(err)
}

func helloWorldTestCase(t *testing.T) testcase {
return testcase{
"echo Hello, World!",
config(t, func(cfg *shell.ExecutorConfig) {
cfg.SkipDate = true
}),
func(exec shell.Executor) error {
return exec.RunFunction(fn("echo"), "Hello, World!")
},
wants{
outs: []string{
"echo [OUT] Hello, World!",
},
},
}
}

func abortTestCase(t *testing.T) testcase {
return testcase{
"abort function",
config(t, func(cfg *shell.ExecutorConfig) {}),
func(exec shell.Executor) error {
return exec.RunFunction(fn("abort"), "reason")
},
wants{
failed: true,
},
}
}

func failExampleCase(t *testing.T) testcase {
return testcase{
"fail-example.sh",
config(t, func(cfg *shell.ExecutorConfig) {}),
func(exec shell.Executor) error {
return exec.RunScript(shell.Script{
Label: "fail-example.sh",
ScriptPath: "shell/fail-example.sh",
}, "expected err")
},
wants{
failed: true,
errs: []string{
"expected err",
},
},
}
}

func missingProjectLocationCase() testcase {
return testcase{
"missing project location",
shell.ExecutorConfig{},
func(exec shell.Executor) error {
return exec.RunFunction(fn("id"))
},
wants{
failed: true,
},
}
}

type wants struct {
failed bool
outs []string
errs []string
}

type testcase struct {
name string
config shell.ExecutorConfig
op func(exec shell.Executor) error
wants
}

func config(t *testing.T, customize func(cfg *shell.ExecutorConfig)) shell.ExecutorConfig {
assert := assertions{t: t}
loc, err := shell.NewProjectLocation("..")
assert.NoError(err)
cfg := shell.ExecutorConfig{
ProjectLocation: loc,
}
customize(&cfg)
return cfg
}

func fn(name string) shell.Function {
return shell.Function{
Script: shell.Script{
Label: name,
ScriptPath: "library.sh",
},
FunctionName: name,
}
}
Loading