diff --git a/main.go b/main.go index 425fe2f..b519326 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "log" "os" "path/filepath" + "strings" ) const ( @@ -27,9 +28,9 @@ const ( CLI_DESCRIPTION = `Run playbooks of SQL scripts in series and parallel on Redshift and Postgres` CLI_VERSION = "0.4.0" - SQLROOT_BINARY = "BINARY" - SQLROOT_PLAYBOOK = "PLAYBOOK" - SQLROOT_CONSUL = "CONSUL" + SQLROOT_BINARY = "BINARY" + SQLROOT_PLAYBOOK = "PLAYBOOK" + SQLROOT_PLAYBOOK_CHILD = "PLAYBOOK_CHILD" ) func main() { @@ -86,22 +87,33 @@ func processFlags() Options { // Resolve the path to our SQL scripts func resolveSqlRoot(sqlroot string, playbookPath string, consulAddress string) (string, error) { - consulErr := fmt.Errorf("Cannot use %s option with -consul argument", sqlroot) + consulErr1 := fmt.Errorf("Cannot use %s option with -consul argument", sqlroot) + consulErr2 := fmt.Errorf("Cannot use %s option without -consul argument", sqlroot) switch sqlroot { case SQLROOT_BINARY: if consulAddress != "" { - return "", consulErr + return "", consulErr1 } return osext.ExecutableFolder() case SQLROOT_PLAYBOOK: if consulAddress != "" { - return "", consulErr + return getAbsConsulPath(playbookPath), nil } return filepath.Abs(filepath.Dir(playbookPath)) - case SQLROOT_CONSUL: - return playbookPath, nil + case SQLROOT_PLAYBOOK_CHILD: + if consulAddress != "" { + return playbookPath, nil + } + return "", consulErr2 default: return sqlroot, nil } } + +// Gets an absolute path for Consul one directory up +func getAbsConsulPath(path string) string { + strSpl := strings.Split(path, "/") + trimSpl := strSpl[:len(strSpl)-1] + return strings.Join(trimSpl, "/") +} diff --git a/options.go b/options.go index 5762899..5d2a443 100644 --- a/options.go +++ b/options.go @@ -56,7 +56,7 @@ func (o *Options) GetFlagSet() *flag.FlagSet { fs.BoolVar(&(o.help), "help", false, "Shows this message") 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, %s and %s for those respective paths", SQLROOT_PLAYBOOK, SQLROOT_BINARY, SQLROOT_CONSUL)) + fs.StringVar(&(o.sqlroot), "sqlroot", SQLROOT_PLAYBOOK, fmt.Sprintf("Absolute path to SQL scripts. Use %s, %s and %s for those respective paths", SQLROOT_PLAYBOOK, SQLROOT_BINARY, SQLROOT_PLAYBOOK_CHILD)) fs.Var(&(o.variables), "var", "Variables to be passed to the playbook, in the key=value format") fs.StringVar(&(o.fromStep), "fromStep", "", "Starts from a given step defined in your playbook") fs.BoolVar(&(o.dryRun), "dryRun", false, "Runs through a playbook without executing any of the SQL")