Skip to content

Commit

Permalink
Add ability to execute a command for each generated file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeewes committed Jan 11, 2020
1 parent 37aafaf commit b55a6f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ An example of `nerc.yml` file:
```yaml
input: test_data/products.csv # Defines the CSV input filepath
templates: test_data/templates/ # Defines the dirpath of the template files
output: output/ # Defines the output dirpath
variables: # Defines template variables
- key: ProductName # Defines the variable key or "name"
csvSourceCol: 6 # CSV column no for product name
Expand All @@ -31,16 +30,29 @@ variables: # Defines template variables
type: price # Type of the variable. Price is rendered with two decimals.
- key: ProductImage
csvSourceCol: 21
output: output/ # Defines the output dirpath
runCommand: head -1 %s # Command to be executed if -r flag is given.
```
**input:** Path to the input CSV file
`input:` Path to the input CSV file

**templates:** Path to the directory that contains the template files. Each template is used to generate
`templates:` Path to the directory that contains the template files. Each template is used to generate
an output file per each line in the input CSV file. Templates can use variables defined in `nerc.yml` file
with `{{.<VariableKey>}}` syntax.

**output:** Output directory path for the generated config files.
`output:` Output directory path for the generated config files.

**variables:** List of variables that can be used in the templates. Each variable must have `key` and `csvSourceCol`
`variables:` List of variables that can be used in the templates. Each variable must have `key` and `csvSourceCol`
which defines the CSV column number where the tools fetches the variable value. Optionally variable can also have
`type`. Currently only `price` type is supported, which tries to ensure two decimal price.

## Command line flags

`-r`: Use `nerc -r` to execute command using each generated config file as an input for the command. Define the
executed command in `nerc.yaml` with `runCommand` key e.g. `runCommand: head -1 %s` would call `head -1` command
for each generated config file and hence output the first line of each file. Note that the `%s` is substituted with
the template filename.

`-purge`: Cleans the output directory from all files. Everything inside the output directory specified in `nerc.yaml`
will be removed.

`-v`: Gives more verbose output.
37 changes: 34 additions & 3 deletions nerc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"text/template"
)

Expand All @@ -24,11 +26,12 @@ const PRICE_COL = 14

var purgeOnly bool
var verbose bool
var run bool

type TemplateVariable struct {
Key string `yaml:"key"`
CSVSourceCol int `yaml:"csvSourceCol"`
Type string `yaml:"type"`
Key string `yaml:"key"`
CSVSourceCol int `yaml:"csvSourceCol"`
Type string `yaml:"type"`
}

type NercConf struct {
Expand All @@ -38,11 +41,13 @@ type NercConf struct {
Variables []TemplateVariable `yaml:"variables"`
StaticVariables map[string]interface{} `yaml:"staticVariables"`
CSVMapping map[string]int `yaml:"csvMapping"`
RunCommand string `yaml:"runCommand"`
}

func main() {
flag.BoolVar(&purgeOnly, "purge", false, "Purge all existing files from output directory and stop.")
flag.BoolVar(&verbose, "v", false, "Use verbose output.")
flag.BoolVar(&run, "run", false, "Execute runCommand (from nerc.yml) for each generated file.")
flag.Parse()

nercConf := NercConf{}
Expand Down Expand Up @@ -72,12 +77,38 @@ func main() {
r := csv.NewReader(bufio.NewReader(csvFile))
os.Mkdir(nercConf.Output, os.ModePerm)
csvToConfigs(r, nercConf)
if run && nercConf.RunCommand != "" {
runCommandForTemplates(nercConf)
}
}
}

fmt.Println("Done")
}

func runCommandForTemplates(conf NercConf) {
var files []string

if _, err := os.Stat(conf.Output); !os.IsNotExist(err) {
err := filepath.Walk(conf.Output, visitPath(&files))
if err != nil {
panic(err)
}
for _, file := range files {
cmdArgs := strings.Fields(fmt.Sprintf(conf.RunCommand, file))
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
stdout, err := cmd.Output()

if err != nil {
fmt.Println(err.Error())
return
}

fmt.Print(string(stdout))
}
}
}

func purgeOutput(nercConf NercConf, silent bool) {
err := os.RemoveAll(nercConf.Output)
if err != nil && !silent {
Expand Down

0 comments on commit b55a6f4

Please sign in to comment.