Skip to content

Commit

Permalink
- Added support for custom cli descriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristoffer Ahl committed Apr 9, 2020
1 parent b542c86 commit 7e0bb98
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/centry/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewRuntime(inputArgs []string, context *Context) (*Runtime, error) {
runtime.cli = &cli.App{
Name: context.manifest.Config.Name,
HelpName: context.manifest.Config.Name,
Usage: "A tool for building declarative CLI's over bash scripts, written in go.", // TODO: Set from manifest config
Usage: context.manifest.Config.Description,
UsageText: "",
Version: context.manifest.Config.Version,

Expand Down
60 changes: 48 additions & 12 deletions cmd/centry/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ func TestMain(t *testing.T) {

g.Describe("output", func() {
out := execQuiet("")

g.It("should display the program name", func() {
expected := `NAME:
centry`
test.AssertStringContains(g, out.Stdout, expected)
})

g.It("should display the program description", func() {
expected := "A manifest file used for testing purposes"
test.AssertStringContains(g, out.Stdout, expected)
})

g.It("should display available commands", func() {
expected := `COMMANDS:
commandtest Command tests
Expand All @@ -275,6 +287,16 @@ func TestMain(t *testing.T) {

test.AssertStringContains(g, out.Stdout, expected)
})

g.Describe("default config output", func() {
g.It("should display the default program description", func() {
expected := `NAME:
name - A new cli application`
out := execQuiet("", "test/data/runtime_test_default_config.yaml")
test.AssertNoError(g, out.Error)
test.AssertStringContains(g, out.Stdout, expected)
})
})
})

g.Describe("command help output", func() {
Expand Down Expand Up @@ -334,10 +356,11 @@ OPTIONS:
out := execQuiet("helptest placeholder --help")
test.AssertStringContains(g, out.Stdout, expected)
})
})

g.Describe("placeholder subcommand help", func() {
g.It("should display full help", func() {
expected := `NAME:
g.Describe("placeholder subcommand help output", func() {
g.It("should display full help", func() {
expected := `NAME:
centry helptest placeholder subcommand1 - Description for placeholder subcommand1
USAGE:
Expand All @@ -347,14 +370,15 @@ OPTIONS:
--opt1 value Help text for opt1
--help, -h Show help (default: false)`

out := execQuiet("helptest placeholder subcommand1 --help")
test.AssertStringContains(g, out.Stdout, expected)
})
out := execQuiet("helptest placeholder subcommand1 --help")
test.AssertStringContains(g, out.Stdout, expected)
})
})
})
}

const defaultManifestPath string = "test/data/runtime_test.yaml"

type execResult struct {
Source string
ExitCode int
Expand All @@ -363,15 +387,27 @@ type execResult struct {
Stderr string
}

func execQuiet(source string) *execResult {
return execCentry(source, true)
func execQuiet(source string, params ...string) *execResult {
manifestPath := defaultManifestPath
if len(params) > 0 {
if params[0] != "" {
manifestPath = params[0]
}
}
return execCentry(source, true, manifestPath)
}

func execWithLogging(source string) *execResult {
return execCentry(source, false)
func execWithLogging(source string, params ...string) *execResult {
manifestPath := defaultManifestPath
if len(params) > 0 {
if params[0] != "" {
manifestPath = params[0]
}
}
return execCentry(source, false, manifestPath)
}

func execCentry(source string, quiet bool) *execResult {
func execCentry(source string, quiet bool, manifestPath string) *execResult {
var exitCode int
var runtimeErr error

Expand All @@ -380,7 +416,7 @@ func execCentry(source string, quiet bool) *execResult {
source = fmt.Sprintf("--quiet %s", source)
}
context := NewContext(CLI, io.Headless())
runtime, err := NewRuntime(strings.Split(fmt.Sprintf("test/data/runtime_test.yaml %s", source), " "), context)
runtime, err := NewRuntime(strings.Split(fmt.Sprintf("%s %s", manifestPath, source), " "), context)
if err != nil {
exitCode = 1
runtimeErr = err
Expand Down
1 change: 1 addition & 0 deletions examples/centry/centry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ options:

config:
name: centry
description: A tool for building declarative CLI's over bash scripts, written in go
version: 1.0.0
log:
level: info
Expand Down
7 changes: 4 additions & 3 deletions internal/pkg/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func (o Option) Annotation(namespace, key string) (*Annotation, error) {

// Config defines the structure for the configuration section
type Config struct {
Name string `yaml:"name,omitempty"`
Version string `yaml:"version,omitempty"`
Log LogConfig `yaml:"log,omitempty"`
Name string `yaml:"name,omitempty"`
Description string `yaml:"description,omitempty"`
Version string `yaml:"version,omitempty"`
Log LogConfig `yaml:"log,omitempty"`
}

// LogConfig defines the structure for log configuration section
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/config/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions schemas/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"description": { "type": "string", "minLength": 1 },
"version": { "type": "string", "minLength": 1 },
"log": {
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions test/data/manifest_test_valid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ options:

config:
name: centry
description: A description from manifest file
version: 1.0.0
log:
level: debug
Expand Down
1 change: 1 addition & 0 deletions test/data/runtime_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ options:

config:
name: centry
description: A manifest file used for testing purposes
version: 1.0.0
log:
level: debug
Expand Down
4 changes: 4 additions & 0 deletions test/data/runtime_test_default_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
commands: []
config:
name: name
version: version

0 comments on commit 7e0bb98

Please sign in to comment.