-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathoptions.go
94 lines (82 loc) · 3.81 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Copyright (c) 2015-2022 Snowplow Analytics Ltd. All rights reserved.
//
// This program is licensed to you under the Apache License Version 2.0,
// and you may not use this file except in compliance with the Apache License Version 2.0.
// You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the Apache License Version 2.0 is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
//
package main
import (
"errors"
"flag"
"fmt"
"strings"
)
// CLIVariables represents the cli variables map.
type CLIVariables map[string]string
// Implement the Value interface
func (i *CLIVariables) String() string {
return fmt.Sprintf("%s", *i)
}
// Set adds a kv pair given as string to CLIVariables.
func (i *CLIVariables) Set(value string) error {
var split = strings.Split(value, ",")
for value := range split {
kv := strings.SplitN(split[value], "=", 2)
if len(kv) != 2 {
return errors.New("invalid size for key, value, key value should be in the key=value format")
}
(*i)[kv[0]] = kv[1]
}
return nil
}
// Options represents the SQL-Runner options.
type Options struct {
help bool
version bool
playbook string
sqlroot string
fromStep string
dryRun bool
consul string
lock string
softLock string
checkLock string
deleteLock string
runQuery string
variables CLIVariables
fillTemplates bool
consulOnlyForLock bool
showQueryOutput bool
}
// NewOptions returns Options.
func NewOptions() Options {
return Options{variables: make(map[string]string)}
}
// GetFlagSet returns a ptr to the FlagSet.
func (o *Options) GetFlagSet() *flag.FlagSet {
var fs = flag.NewFlagSet("Options", flag.ExitOnError)
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", sqlrootPlaybook, fmt.Sprintf("Absolute path to SQL scripts. Use %s, %s and %s for those respective paths", sqlrootPlaybook, sqlrootBinary, sqlrootPlaybookChild))
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")
fs.StringVar(&(o.consul), "consul", "", "The address of a consul server with playbooks and SQL files stored in KV pairs")
fs.StringVar(&(o.lock), "lock", "", "Optional argument which checks and sets a lockfile to ensure this run is a singleton. Deletes lock on run completing successfully")
fs.StringVar(&(o.softLock), "softLock", "", "Optional argument, like '-lock' but the lockfile will be deleted even if the run fails")
fs.StringVar(&(o.checkLock), "checkLock", "", "Checks whether the lockfile already exists")
fs.StringVar(&(o.deleteLock), "deleteLock", "", "Will attempt to delete a lockfile if it exists")
fs.StringVar(&(o.runQuery), "runQuery", "", "Will run a single query in the playbook")
fs.BoolVar(&(o.fillTemplates), "fillTemplates", false, "Will print all queries after templates are filled")
fs.BoolVar(&(o.consulOnlyForLock), "consulOnlyForLock", false, "Will read playbooks locally, but use Consul for locking.")
fs.BoolVar(&(o.showQueryOutput), "showQueryOutput", false, "Will print all output from queries")
// TODO: add format flag if/when we support TOML
return fs
}