Skip to content

Commit

Permalink
Add env var override for default output/input format
Browse files Browse the repository at this point in the history
  • Loading branch information
alxarch committed Feb 12, 2019
1 parent 87b18ee commit 645e51f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
24 changes: 20 additions & 4 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,21 @@ ENV:
--max-stack <SIZE> Jsonnet VM max stack size (default 500)
EVAL:
-e, --eval <SNIPPET> Process values with Jsonnet
<SCRIPT> Evaluate a Jsonnet script for each value.
-x, --exec <SCRIPT> Same as above regardless of file extension.
-e, --eval <SNIPPET> Evaluate a Jsonnet snippet for each value.
If no INPUT is specified, values are read from stdin as YAML.
If FILE is "-" or "" values are read from stdin until EOF.
If FILE has no type option format is detected from extension.
If FILE has no type option, format is detected from extension:
.json -> JSON
.yaml, .yml -> YAML
.jsonnet -> Jsonnet script
.* -> YCAT_FORMAT environment variable or YAML
Default output format is YAML unless YCAT_OUTPUT environment variable is 'json'
`

var shortArgs = map[byte]string{
Expand All @@ -125,7 +134,8 @@ var shortArgs = map[byte]string{
'i': "import",
'v': "var",
'e': "eval",
'o': "output",
'x': "exec",
'o': "out",
'n': "null",
'a': "array",
'h': "help",
Expand Down Expand Up @@ -166,7 +176,10 @@ func (p *argParser) parseLong(name, value string, argv []string) ([]string, erro
return argv, err
}
p.addTask(p.eval.Snippet(filename, value))
case "output":
case "exec":
value, argv = shiftArgV(value, argv)
p.addFile(value, JSONNET)
case "out":
value, argv = shiftArgV(value, argv)
if p.output = OutputFromString(value); p.output == OutputInvalid {
return argv, fmt.Errorf("Invalid output format: %q", value)
Expand Down Expand Up @@ -271,6 +284,9 @@ func (p *argParser) addFile(path string, format Format) {
}

func (p *argParser) outputTask() (s StreamTask) {
if p.output == OutputInvalid {
p.output = DefaultOutput()
}
switch p.output {
case OutputJSON:
return StreamWriteJSON(p.stdout)
Expand Down
53 changes: 47 additions & 6 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,56 @@ func FormatFromString(s string) Format {
}
}

// EnvDefaultFormat is the name of the env var for file format detection
const EnvDefaultFormat = "YCAT_FORMAT"

// EnvDefaultOutput is the name of the env var for default file output
const EnvDefaultOutput = "YCAT_OUTPUT"

var (
defaultFormat = Auto
defaultOutput = OutputInvalid
)

// DefaultFormat returns the default format from environment
func DefaultFormat() Format {
if defaultFormat == Auto {
f := FormatFromString(os.Getenv(EnvDefaultFormat))
switch f {
case YAML, JSON:
defaultFormat = f
default:
defaultFormat = YAML
}
}
return defaultFormat
}

// DefaultOutput returns the default output from environment
func DefaultOutput() Output {
if defaultOutput == OutputInvalid {
f := OutputFromString(os.Getenv(EnvDefaultOutput))
switch f {
case OutputYAML, OutputJSON:
defaultOutput = f
default:
defaultOutput = OutputYAML
}
}
return defaultOutput
}

// DetectFormat detects an input format from the extension
func DetectFormat(filename string) Format {
switch path.Ext(filename) {
case ".json":
return JSON
case ".jsonnet", ".libsonnet":
case ".jsonnet":
return JSONNET
default:
case ".yaml", ".yml":
return YAML
default:
return DefaultFormat()
}
}

Expand All @@ -51,10 +92,10 @@ type Output int

// Output formats
const (
OutputInvalid Output = iota - 1
OutputInvalid Output = iota
OutputYAML
OutputJSON
OutputRaw // Only with --eval
// OutputRaw // Only with --eval
)

// OutputFromString converts a string to Output
Expand All @@ -64,8 +105,8 @@ func OutputFromString(s string) Output {
return OutputJSON
case "yaml", "y":
return OutputYAML
case "raw", "r":
return OutputRaw
// case "raw", "r":
// return OutputRaw
default:
return OutputInvalid
}
Expand Down

0 comments on commit 645e51f

Please sign in to comment.