Skip to content

Commit

Permalink
adding -r --raw flag (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien authored Aug 9, 2017
1 parent 0cce875 commit ccdf744
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .assets/tests/expected_result.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
author: Julien Neuhart
comment: A simple file for testing purpose
usa:
info: Some satellites launched by the USA (1950s)
satellites:
Expand Down
2 changes: 2 additions & 0 deletions .assets/tests/template.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
author: {{ .RawData.author }}
comment: {{ .RawData.comment }}
usa:
info: {{ .EnvFiles.default.USA }}
satellites:
Expand Down
2 changes: 2 additions & 0 deletions .assets/tests/template_many.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
author: {{ .RawData.author }}
comment: {{ .RawData.comment }}
usa:
info: {{ .EnvFiles.usa.USA }}
satellites:
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ As you can see, it works the same way as the `-v` flag:
* with mapping, your data will be accessible in your template through `{{ .EnvFiles.my_key.my_data }}`.
* otherwise through `{{ .EnvFiles.default.my_data }}`.

##### `-r --raw`

The flag `-r` allows you to specify data directly from the CLI.

```
orbit generate [...] -r=key_1=value_1
orbit generate [...] -r=key_1=value_1;key_2=value_2
```

Your data will be accessible in your template through `{{ .RawData.my_key }}`.

##### `-s --silent`

Disables the notifications.
Expand Down Expand Up @@ -247,6 +258,12 @@ The flag `-e` allows you to specify one or many *.env* files.

It works the same as the `-e` flag from the `generate` command.

##### `-r --raw`

The flag `-r` allows you to specify data directly from the CLI.

It works the same as the `-r` flag from the `generate` command.

##### `-s --silent`

Disables the notifications.
Expand Down
2 changes: 1 addition & 1 deletion commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If no output file is given, prints the result to Stdout.
*/
func generate(cmd *cobra.Command, args []string) error {
// first, let's instantiate our Orbit context.
ctx, err := context.NewOrbitContext(templateFilePath, ValuesFiles, EnvFiles)
ctx, err := context.NewOrbitContext(templateFilePath, ValuesFiles, EnvFiles, RawData)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var (
// EnvFiles is the path or a map of paths of .env files listing values used in a data-driven template.
EnvFiles string

// RawData are a map of values used in a data-driven template.
RawData string

// silent disables the notifications if true.
silent bool

Expand All @@ -32,6 +35,7 @@ var (

func init() {
RootCmd.PersistentFlags().StringVarP(&ValuesFiles, "values", "v", "", "specify a YAML file or a map of YAML files listing values used in the template")
RootCmd.PersistentFlags().StringVarP(&EnvFiles, "env", "e", "", "specify a .env file or a map of .env files listing values used in the the template")
RootCmd.PersistentFlags().StringVarP(&EnvFiles, "env", "e", "", "specify a .env file or a map of .env files listing values used in the template")
RootCmd.PersistentFlags().StringVarP(&RawData, "raw", "r", "", "specify a map of values used in the template")
RootCmd.PersistentFlags().BoolVarP(&silent, "silent", "s", false, "disable the notifications")
}
2 changes: 1 addition & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func run(cmd *cobra.Command, args []string) error {
}

// alright, let's instantiate our Orbit context...
ctx, err := context.NewOrbitContext(configFilePath, ValuesFiles, EnvFiles)
ctx, err := context.NewOrbitContext(configFilePath, ValuesFiles, EnvFiles, RawData)
if err != nil {
return err
}
Expand Down
32 changes: 31 additions & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type (
// EnvFiles map contains pairs from .env files.
EnvFiles map[string]map[string]string

// RawData contains data past directly in the CLI.
RawData map[string]string

// Os is the OS name at runtime.
Os string
}
Expand All @@ -46,7 +49,7 @@ type (
)

// NewOrbitContext instantiates a new OrbitContext.
func NewOrbitContext(templateFilePath string, valuesFiles string, envFiles string) (*OrbitContext, error) {
func NewOrbitContext(templateFilePath string, valuesFiles string, envFiles string, rawData string) (*OrbitContext, error) {
// as the data-driven template is mandatory, we must check its validity.
if templateFilePath == "" || !helpers.FileExists(templateFilePath) {
return nil, fmt.Errorf("template file \"%s\" does not exist", templateFilePath)
Expand Down Expand Up @@ -78,6 +81,16 @@ func NewOrbitContext(templateFilePath string, valuesFiles string, envFiles strin
ctx.EnvFiles = data
}

// checks if raw data have been specified.
if rawData != "" {
data, err := getRawDataMap(rawData)
if err != nil {
return nil, err
}

ctx.RawData = data
}

return ctx, nil
}

Expand Down Expand Up @@ -166,3 +179,20 @@ func getFilesMap(s string) ([]*OrbitFileMap, error) {

return filesMap, nil
}

// getRawDataMap reads a string and populates a map of strings.
func getRawDataMap(s string) (map[string]string, error) {
parts := strings.Split(s, ";")

rawDataMap := make(map[string]string)
for _, part := range parts {
data := strings.Split(part, "=")
if len(data) != 2 {
return rawDataMap, fmt.Errorf("unable to process the raw data \"%s\"", s)
}

rawDataMap[data[0]] = data[1]
}

return rawDataMap, nil
}
5 changes: 3 additions & 2 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ func init() {
manyTmpl := helpers.Abs("../.assets/tests/template_many.yml")
values := helpers.Abs("../.assets/tests/values.yml")
envFile := helpers.Abs("../.assets/tests/.env")
rawData := "author=Julien Neuhart;comment=A simple file for testing purpose"

// last but not least, creates our OrbitGenerator instances.
ctx, err := context.NewOrbitContext(defaultTmpl, values, envFile)
ctx, err := context.NewOrbitContext(defaultTmpl, values, envFile, rawData)
if err != nil {
panic(err)
}

defaultGenerator = NewOrbitGenerator(ctx)

ctx, err = context.NewOrbitContext(manyTmpl, "ru,"+values+";usa,"+values, "ru,"+envFile+";usa,"+envFile)
ctx, err = context.NewOrbitContext(manyTmpl, "ru,"+values+";usa,"+values, "ru,"+envFile+";usa,"+envFile, rawData)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var testRunner *OrbitRunner
func init() {
config := helpers.Abs("../.assets/tests/orbit.yml")

ctx, err := context.NewOrbitContext(config, "", "")
ctx, err := context.NewOrbitContext(config, "", "", "")
if err != nil {
panic(err)
}
Expand Down

0 comments on commit ccdf744

Please sign in to comment.