Skip to content

Commit

Permalink
Set default path for manifest file, overrides using flag or environme…
Browse files Browse the repository at this point in the history
…nt variable.
  • Loading branch information
kristofferahl committed Feb 26, 2021
1 parent afda43b commit 86cdc3f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 14 deletions.
8 changes: 8 additions & 0 deletions cmd/centry/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import (
"strings"
)

func environmentOrDefault(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
value = defaultValue
}
return value
}

func overrideFromEnvironment(runtime *Runtime) {
context := runtime.context
cli := runtime.cli
Expand Down
55 changes: 49 additions & 6 deletions cmd/centry/runtime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"strings"

"github.com/kristofferahl/go-centry/internal/pkg/config"
Expand All @@ -26,15 +27,18 @@ func NewRuntime(inputArgs []string, context *Context) (*Runtime, error) {
runtime := &Runtime{
cli: nil,
context: context,
file: "",
file: "./centry.yaml",
args: []string{},
events: []string{},
}

// Args
if len(inputArgs) >= 1 {
runtime.file = inputArgs[0]
runtime.args = inputArgs[1:]
// Env manifest file
err := initFromEnvironment(runtime)

// Args and manifest file
err = initFromArgs(runtime, inputArgs)
if err != nil {
return nil, err
}

// Load manifest
Expand Down Expand Up @@ -97,6 +101,45 @@ func NewRuntime(inputArgs []string, context *Context) (*Runtime, error) {
return runtime, nil
}

func initFromEnvironment(runtime *Runtime) error {
file := environmentOrDefault("CENTRY_FILE", "")
if file != "" {
runtime.file = file
runtime.events = append(runtime.events, fmt.Sprintf("manifest file path set (path=%s source=%s)", runtime.file, "environment"))
}
return nil
}

func initFromArgs(runtime *Runtime, inputArgs []string) error {
if len(inputArgs) >= 1 && inputArgs[0] == "--centry-file" {
runtime.file = ""
if len(inputArgs) >= 2 {
runtime.file = inputArgs[1]
runtime.args = inputArgs[2:]
}

if runtime.file == "" {
return fmt.Errorf("A value must be specified for --centry-file")
}

runtime.events = append(runtime.events, fmt.Sprintf("manifest file path set (path=%s source=%s)", runtime.file, "flag"))
} else if len(inputArgs) >= 1 && strings.HasPrefix(inputArgs[0], "--centry-file=") {
flagvalue := strings.Split(inputArgs[0], "=")
runtime.file = strings.Join(flagvalue[1:], "=")
runtime.args = inputArgs[1:]

if runtime.file == "" {
return fmt.Errorf("A value must be specified for --centry-file")
}

runtime.events = append(runtime.events, fmt.Sprintf("manifest file path set (path=%s source=%s)", runtime.file, "flag"))
} else {
runtime.args = inputArgs
}

return nil
}

// Execute runs the CLI and exits with a code
func (runtime *Runtime) Execute() int {
args := append([]string{""}, runtime.args...)
Expand Down Expand Up @@ -133,7 +176,7 @@ func handleBefore(runtime *Runtime, c *cli.Context) error {
// Print runtime events
logger := runtime.context.log.GetLogger()
for _, e := range runtime.events {
logger.Debugf(e)
logger.Debugf("[runtime-event] %s", e)
}

return nil
Expand Down
63 changes: 56 additions & 7 deletions cmd/centry/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,56 @@ func TestMain(t *testing.T) {
os.Chdir("../../")

g.Describe("runtime", func() {
g.It("returns error when manifest fails to load", func() {
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil")
g.Assert(err != nil).IsTrue("expected error")
g.Assert(strings.HasPrefix(err.Error(), "Failed to read manifest file")).IsTrue("expected error message")
g.Describe("manifest file", func() {
g.It("tries to use ./centry.yaml as the default file", func() {
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil, %v", runtime)
g.Assert(err != nil).IsTrue("expected error, %v", err)
g.Assert(err.Error()).Eql("Manifest file not found (path=./centry.yaml)")
})

g.It("tries to use file specified CENTRY_FILE environment variable", func() {
os.Setenv("CENTRY_FILE", "./centry-environment.yaml")
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil, %v", runtime)
g.Assert(err != nil).IsTrue("expected error, %v", err)
g.Assert(err.Error()).Eql("Manifest file not found (path=./centry-environment.yaml)")
os.Setenv("CENTRY_FILE", "")
})

g.It("tries to use file specified by --centry-file flag", func() {
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{"--centry-file", "./centry-flag.yaml"}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil, %v", runtime)
g.Assert(err != nil).IsTrue("expected error, %v", err)
g.Assert(err.Error()).Eql("Manifest file not found (path=./centry-flag.yaml)")
})

g.It("tries to use file specified by --centry-file= flag", func() {
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{"--centry-file=./centry-flag-equals.yaml"}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil, %v", runtime)
g.Assert(err != nil).IsTrue("expected error, %v", err)
g.Assert(err.Error()).Eql("Manifest file not found (path=./centry-flag-equals.yaml)")
})

g.It("tries to use file specified by --centry-file= flag even when it contains equal signs", func() {
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{"--centry-file=./foo=bar.yaml"}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil, %v", runtime)
g.Assert(err != nil).IsTrue("expected error, %v", err)
g.Assert(err.Error()).Eql("Manifest file not found (path=./foo=bar.yaml)")
})

g.It("gives an error when --centry-file flag is missing it's value", func() {
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime([]string{"--centry-file"}, context)
g.Assert(runtime == nil).IsTrue("expected runtime to be nil, %v", runtime)
g.Assert(err != nil).IsTrue("expected error, %v", err)
g.Assert(err.Error()).Eql("A value must be specified for --centry-file")
})
})
})

Expand All @@ -32,6 +76,7 @@ func TestMain(t *testing.T) {
expected := "Loading init.sh\nLoading helpers.sh"
os.Setenv("OUTPUT_DEBUG", "true")
out := execQuiet("scripttest")
test.AssertNoError(g, out.Error)
test.AssertStringContains(g, out.Stdout, expected)
os.Unsetenv("OUTPUT_DEBUG")
})
Expand Down Expand Up @@ -476,8 +521,12 @@ func execCentry(source string, quiet bool, manifestPath string) *execResult {
if quiet {
source = fmt.Sprintf("--centry-quiet %s", source)
}
if manifestPath != "" {
source = fmt.Sprintf("--centry-file %s %s", manifestPath, source)
}
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime(strings.Split(fmt.Sprintf("%s %s", manifestPath, source), " "), context)
os.Args = strings.Split(fmt.Sprintf("program %s", source), " ")
runtime, err := NewRuntime(os.Args[1:], context)
if err != nil {
exitCode = 1
runtimeErr = err
Expand Down
2 changes: 1 addition & 1 deletion scripts/run-centry
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ main() {

rm -f ./centry &>/dev/null || true
go build -o ./centry ./cmd/centry/
./centry ./examples/centry/centry.yaml "$@"
./centry --centry-file ./examples/centry/centry.yaml "$@"
}

main "$@"

0 comments on commit 86cdc3f

Please sign in to comment.