Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to passing variables as command-line options #35

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions integration-tests/postgres-sql/good/assert.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ DECLARE
expected_average_age CONSTANT integer := 25;
BEGIN
{{/* Update view names to today's date or Travis will error */}}
IF (SELECT average_age <> expected_average_age FROM {{.test_schema}}.view_2015_09_13) THEN
IF (SELECT average_age <> expected_average_age FROM {{.test_schema}}.view_2015_09_14) THEN
RAISE EXCEPTION 'Average_age % does not match expected age %',
(SELECT average_age FROM {{.test_schema}}.view_2015_09_13),
expected_average_age;
(SELECT average_age FROM {{.test_schema}}.view_2015_09_14),
expected_average_age;
END IF;
END;
$$ LANGUAGE plpgsql;
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {

options := processFlags()

pb, err := playbook.ParsePlaybook(options.playbook)
pb, err := playbook.ParsePlaybook(options.playbook, options.variables)
if err != nil {
log.Fatalf("Could not parse playbook (YAML): %s", err.Error())
}
Expand All @@ -51,7 +51,7 @@ func main() {
// required flag
func processFlags() Options {

var options Options
var options Options = NewOptions()
var fs = options.GetFlagSet()
fs.Parse(os.Args[1:])

Expand Down
32 changes: 28 additions & 4 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,36 @@ package main
import (
"flag"
"fmt"
"strings"
)

type CLIVariables map[string]string

// Implement the Value interface
func (i *CLIVariables) String() string {
return fmt.Sprintf("%s", *i)
}

func (i *CLIVariables) Set(value string) error {
var split = strings.SplitN(value, "=", 2)
if len(split) > 1 {
key := split[0]
val := split[1]
(*i)[key] = val
}
return nil
}

type Options struct {
help bool
version bool
playbook string
sqlroot string
help bool
version bool
playbook string
sqlroot string
variables CLIVariables
}

func NewOptions() Options {
return Options{variables: make(map[string]string)}
}

func (o *Options) GetFlagSet() *flag.FlagSet {
Expand All @@ -31,6 +54,7 @@ func (o *Options) GetFlagSet() *flag.FlagSet {
fs.BoolVar(&(o.version), "version", false, "Shows the program version")
fs.StringVar(&(o.playbook), "playbook", "", "Playbook of SQL scripts to execute")
fs.StringVar(&(o.sqlroot), "sqlroot", SQLROOT_PLAYBOOK, fmt.Sprintf("Absolute path to SQL scripts. Use %s and %s for those respective paths", SQLROOT_PLAYBOOK, SQLROOT_BINARY))
fs.Var(&(o.variables), "var", "Variables to be passed to the playbook, in the key=value format")
// TODO: add format flag if/when we support TOML

return fs
Expand Down
15 changes: 13 additions & 2 deletions playbook/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,18 @@ type Query struct {
}

// Dispatch to format-specific parser
func ParsePlaybook(filepath string) (Playbook, error) {
func ParsePlaybook(filepath string, variables map[string]string) (Playbook, error) {
// TODO: Add TOML support?
return parsePlaybookYaml(filepath)
playbook, err := parsePlaybookYaml(filepath)
if err == nil {
playbook = MergeCLIVariables(playbook, variables)
}
return playbook, err
}

func MergeCLIVariables(playbook Playbook, variables map[string]string) Playbook {
for k, v := range variables {
playbook.Variables[k] = v
}
return playbook
}