Skip to content

Commit fc2af51

Browse files
committed
Switch to command style argparser
1 parent fc91307 commit fc2af51

7 files changed

+172
-117
lines changed

command.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"./sync"
6+
)
7+
8+
type AbstractCommand struct {
9+
10+
}
11+
12+
func (command *AbstractCommand) GetConfig() *sync.SyncConfig {
13+
Logger.Main("Initialisation")
14+
configFile := findConfigFile()
15+
if configFile == "" {
16+
Logger.FatalExit(2, "Unable to find configuration file (searched %s)", strings.Join(validConfigFiles, " "))
17+
}
18+
Logger.Step("found configuration file %s", configFile)
19+
20+
sync.Logger = Logger
21+
config := sync.NewConfigParser(configFile)
22+
23+
return config
24+
}

command_deploy.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type DeployCommand struct {
6+
AbstractCommand
7+
Positional struct {
8+
Server string `description:"server configuration key"`
9+
} `positional-args:"true"`
10+
Dump bool `long:"dump" description:"dump configuration as yaml"`
11+
}
12+
13+
// Run deployment command
14+
func (command *DeployCommand) Execute(args []string) error {
15+
config := command.GetConfig()
16+
server := getArgServer(config, "deploy", command.Positional.Server)
17+
confServer, err := config.GetDeployServer(server)
18+
if err != nil {
19+
Logger.FatalErrorExit(3, err)
20+
}
21+
Logger.Step("using Server[%s]", server)
22+
Logger.Step("using %s", confServer.Connection.String())
23+
24+
// --dump
25+
if command.Dump {
26+
fmt.Println()
27+
fmt.Println(confServer.AsYaml())
28+
} else {
29+
confServer.Deploy()
30+
Logger.Println("-> finished")
31+
}
32+
33+
return nil
34+
}

command_list.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
type ListCommand struct {
4+
AbstractCommand
5+
}
6+
7+
// List all possible server configurations (for sync and deploy)
8+
func (command *ListCommand) Execute(args []string) error {
9+
config := command.GetConfig()
10+
config.ShowConfiguration()
11+
return nil
12+
}

command_sync.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type SyncCommand struct {
6+
AbstractCommand
7+
Positional struct {
8+
Server string `description:"server configuration key"`
9+
} `positional-args:"true"`
10+
Dump bool `long:"dump" description:"dump configuration as yaml"`
11+
}
12+
13+
// Run sync command
14+
func (command *SyncCommand) Execute(args []string) error {
15+
config := command.GetConfig()
16+
server := getArgServer(config, "sync", command.Positional.Server)
17+
confServer, err := config.GetSyncServer(server)
18+
if err != nil {
19+
Logger.FatalErrorExit(3, err)
20+
}
21+
Logger.Step("using Server[%s]", server)
22+
Logger.Step("using %s", confServer.Connection.String())
23+
24+
// --dump
25+
if command.Dump {
26+
fmt.Println()
27+
fmt.Println(confServer.AsYaml())
28+
} else {
29+
confServer.Sync()
30+
Logger.Println("-> finished")
31+
}
32+
33+
return nil
34+
}

command_version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type VersionCommand struct {
8+
ShowOnlyVersion bool `long:"dump" description:"show only version number and exit"`
9+
Name string
10+
Version string
11+
Author string
12+
}
13+
14+
// Show app version
15+
func (conf *VersionCommand) Execute(args []string) error {
16+
if conf.ShowOnlyVersion {
17+
fmt.Println(conf.Version)
18+
} else {
19+
fmt.Println(fmt.Sprintf("%s version %s", conf.Name, conf.Version))
20+
fmt.Println(fmt.Sprintf("Copyright (C) 2017 %s", conf.Author))
21+
}
22+
23+
return nil
24+
}

main.go

Lines changed: 31 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"fmt"
77
"path"
8-
"strings"
98
"runtime/debug"
109
flags "github.com/jessevdk/go-flags"
1110
"github.com/webdevops/go-shell"
@@ -27,17 +26,7 @@ var (
2726
)
2827

2928
var opts struct {
30-
Positional struct {
31-
Command string `description:"What to do [help, sync, deploy or show]" choice:"show" choice:"sync" choice:"deploy" choice:"help" required:"1"`
32-
Server string `description:"server configuration key"`
33-
} `positional-args:"true"`
34-
35-
Dump bool ` long:"dump" description:"dump configuration as yaml"`
3629
Verbose []bool `short:"v" long:"verbose" description:"verbose mode"`
37-
DryRun bool ` long:"dry-run" description:"dry run mode"`
38-
ShowVersion bool `short:"V" long:"version" description:"show version and exit"`
39-
ShowOnlyVersion bool ` long:"dumpversion" description:"show only version number and exit"`
40-
ShowHelp bool `short:"h" long:"help" description:"show this help message"`
4130
}
4231

4332
var validConfigFiles = []string{
@@ -50,6 +39,33 @@ var validConfigFiles = []string{
5039
func createArgparser() {
5140
var err error
5241
argparser = flags.NewParser(&opts, flags.Default)
42+
argparser.CommandHandler = func(command flags.Commander, args []string) error {
43+
switch {
44+
case len(opts.Verbose) >= 2:
45+
shell.Trace = true
46+
shell.TracePrefix = "[CMD] "
47+
Logger = logger.GetInstance(argparser.Command.Name, log.Ldate|log.Ltime|log.Lshortfile)
48+
fallthrough
49+
case len(opts.Verbose) >= 1:
50+
logger.Verbose = true
51+
shell.VerboseFunc = func(c *shell.Command) {
52+
Logger.Command(c.ToString())
53+
}
54+
fallthrough
55+
default:
56+
if Logger == nil {
57+
Logger = logger.GetInstance(argparser.Command.Name, 0)
58+
}
59+
}
60+
61+
return command.Execute(args)
62+
}
63+
64+
argparser.AddCommand("version", "Show version", fmt.Sprintf("Show %s version", Name), &VersionCommand{Name:Name, Version:Version, Author:Author})
65+
argparser.AddCommand("list", "List server configurations", "List server configurations", &ListCommand{})
66+
argparser.AddCommand("sync", "Sync from server", "Sync filesystem and databases from server", &SyncCommand{})
67+
argparser.AddCommand("deploy", "Deploy to server", "Deploy filesystem and databases to server", &DeployCommand{})
68+
5369
args, err = argparser.Parse()
5470

5571
// check if there is an parse error
@@ -62,19 +78,6 @@ func createArgparser() {
6278
os.Exit(1)
6379
}
6480
}
65-
66-
// --dumpversion
67-
if opts.ShowOnlyVersion {
68-
fmt.Println(Version)
69-
os.Exit(0)
70-
}
71-
72-
// --version
73-
if opts.ShowVersion {
74-
fmt.Println(fmt.Sprintf("%s version %s", Name, Version))
75-
fmt.Println(fmt.Sprintf("Copyright (C) 2017 %s", Author))
76-
os.Exit(0)
77-
}
7881
}
7982

8083
func findConfigFile() string {
@@ -109,17 +112,16 @@ func findConfigFile() string {
109112
return ""
110113
}
111114

112-
func getArgServer(config *sync.SyncConfig, confType string) string {
113-
server := opts.Positional.Server
114-
if server == "" {
115+
func getArgServer(config *sync.SyncConfig, confType string, userSelection string) string {
116+
if userSelection == "" {
115117
prompt := &survey.Select{
116118
Message: "Choose configuration:",
117119
Options: config.GetServerList(confType),
118120
}
119-
survey.AskOne(prompt, &server, nil)
121+
survey.AskOne(prompt, &userSelection, nil)
120122
}
121123

122-
return server
124+
return userSelection
123125
}
124126

125127
func main() {
@@ -138,87 +140,5 @@ func main() {
138140

139141
createArgparser()
140142

141-
argCommand := strings.ToLower(opts.Positional.Command)
142-
143-
switch {
144-
case len(opts.Verbose) >= 2:
145-
shell.Trace = true
146-
shell.TracePrefix = "[CMD] "
147-
Logger = logger.GetInstance(argparser.Command.Name, log.Ldate|log.Ltime|log.Lshortfile)
148-
fallthrough
149-
case len(opts.Verbose) >= 1:
150-
logger.Verbose = true
151-
shell.VerboseFunc = func(c *shell.Command) {
152-
Logger.Command(c.ToString())
153-
}
154-
fallthrough
155-
default:
156-
if Logger == nil {
157-
Logger = logger.GetInstance(argparser.Command.Name, 0)
158-
}
159-
}
160-
161-
if argCommand == "help" {
162-
argparser.WriteHelp(os.Stdout)
163-
os.Exit(0)
164-
}
165-
166-
Logger.Main("Initialisation")
167-
configFile := findConfigFile()
168-
if configFile == "" {
169-
Logger.FatalExit(2, "Unable to find configuration file (searched %s)", strings.Join(validConfigFiles, " "))
170-
}
171-
Logger.Step("found configuration file %s", configFile)
172-
173-
sync.Logger = Logger
174-
config := sync.NewConfigParser(configFile)
175-
176-
switch argCommand {
177-
case "show":
178-
//----------------------
179-
// Show
180-
//----------------------
181-
config.ShowConfiguration()
182-
case "sync":
183-
//----------------------
184-
// Sync
185-
//----------------------
186-
server := getArgServer(config, "sync")
187-
confServer, err := config.GetSyncServer(server)
188-
if err != nil {
189-
Logger.FatalErrorExit(3, err)
190-
}
191-
Logger.Step("using Server[%s]", server)
192-
Logger.Step("using %s", confServer.Connection.String())
193-
194-
// --dump
195-
if opts.Dump {
196-
fmt.Println()
197-
fmt.Println(confServer.AsYaml())
198-
} else {
199-
confServer.Sync()
200-
Logger.Println("-> finished")
201-
}
202-
case "deploy":
203-
//----------------------
204-
// Deploy
205-
//----------------------
206-
server := getArgServer(config, "deploy")
207-
confServer, err := config.GetDeployServer(server)
208-
if err != nil {
209-
Logger.FatalErrorExit(3, err)
210-
}
211-
Logger.Step("using %s server", server)
212-
Logger.Step("using connection %s", confServer.Connection.String())
213-
// --dump
214-
if opts.Dump {
215-
fmt.Println()
216-
fmt.Println(confServer.AsYaml())
217-
} else {
218-
confServer.Deploy()
219-
Logger.Println("-> finished")
220-
}
221-
}
222-
223143
os.Exit(0)
224144
}

sync/configparser.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,29 @@ func (config *SyncConfig) GetServerList(confType string) []string {
7777
return ret
7878
}
7979

80+
// List all possible server configurations
8081
func (config *SyncConfig) ListServer() map[string][]string {
8182
ret := map[string][]string{}
8283

83-
ret["Sync"] = make([]string, len(config.Sync)-1)
84-
for key := range config.Sync {
85-
ret["Sync"] = append(ret["Sync"], key)
84+
if len(config.Sync) > 0 {
85+
ret["Sync"] = make([]string, len(config.Sync)-1)
86+
for key := range config.Sync {
87+
ret["Sync"] = append(ret["Sync"], key)
88+
}
8689
}
8790

88-
ret["Deploy"] = make([]string, len(config.Deploy)-1)
89-
for key := range config.Deploy {
90-
ret["Deploy"] = append(ret["Deploy"], key)
91+
if len(config.Deploy) > 0 {
92+
ret["Deploy"] = make([]string, len(config.Deploy)-1)
93+
for key := range config.Deploy {
94+
ret["Deploy"] = append(ret["Deploy"], key)
95+
}
9196
}
9297

9398
return ret
9499
}
95100

101+
// Show all possible server configurations
102+
// in an human readable style
96103
func (config *SyncConfig) ShowConfiguration() {
97104
serverList := config.ListServer()
98105

0 commit comments

Comments
 (0)