Skip to content

Commit 8c755cb

Browse files
committed
Refactor functions
1 parent fc2af51 commit 8c755cb

File tree

4 files changed

+55
-52
lines changed

4 files changed

+55
-52
lines changed

command.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package main
22

33
import (
44
"strings"
5+
"os"
6+
"fmt"
7+
"path"
58
"./sync"
9+
"gopkg.in/AlecAivazis/survey.v1"
10+
611
)
712

813
type AbstractCommand struct {
@@ -11,7 +16,7 @@ type AbstractCommand struct {
1116

1217
func (command *AbstractCommand) GetConfig() *sync.SyncConfig {
1318
Logger.Main("Initialisation")
14-
configFile := findConfigFile()
19+
configFile := command.findConfigFile()
1520
if configFile == "" {
1621
Logger.FatalExit(2, "Unable to find configuration file (searched %s)", strings.Join(validConfigFiles, " "))
1722
}
@@ -22,3 +27,48 @@ func (command *AbstractCommand) GetConfig() *sync.SyncConfig {
2227

2328
return config
2429
}
30+
31+
func (command *AbstractCommand) findConfigFile() string {
32+
pwd, err := os.Getwd()
33+
if err != nil {
34+
Logger.FatalErrorExit(1, err)
35+
fmt.Println(err)
36+
}
37+
38+
for true {
39+
for _, filename := range validConfigFiles {
40+
filepath := path.Join(pwd, filename)
41+
if sync.FileExists(filepath) {
42+
return filepath
43+
}
44+
}
45+
46+
47+
// already found root, we finished here
48+
if pwd == "/" {
49+
break
50+
}
51+
52+
pwd = path.Dir(pwd)
53+
54+
// oh, path seems to be empty.. stopping here now
55+
if pwd == "." || pwd == "" {
56+
break
57+
}
58+
}
59+
60+
return ""
61+
}
62+
63+
64+
func (command *AbstractCommand) getServerSelectionFromUser(config *sync.SyncConfig, confType string, userSelection string) string {
65+
if userSelection == "" {
66+
prompt := &survey.Select{
67+
Message: "Choose configuration:",
68+
Options: config.GetServerList(confType),
69+
}
70+
survey.AskOne(prompt, &userSelection, nil)
71+
}
72+
73+
return userSelection
74+
}

command_deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type DeployCommand struct {
1313
// Run deployment command
1414
func (command *DeployCommand) Execute(args []string) error {
1515
config := command.GetConfig()
16-
server := getArgServer(config, "deploy", command.Positional.Server)
16+
server := command.getServerSelectionFromUser(config, "deploy", command.Positional.Server)
1717
confServer, err := config.GetDeployServer(server)
1818
if err != nil {
1919
Logger.FatalErrorExit(3, err)

command_sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type SyncCommand struct {
1313
// Run sync command
1414
func (command *SyncCommand) Execute(args []string) error {
1515
config := command.GetConfig()
16-
server := getArgServer(config, "sync", command.Positional.Server)
16+
server := command.getServerSelectionFromUser(config, "sync", command.Positional.Server)
1717
confServer, err := config.GetSyncServer(server)
1818
if err != nil {
1919
Logger.FatalErrorExit(3, err)

main.go

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import (
44
"os"
55
"log"
66
"fmt"
7-
"path"
87
"runtime/debug"
98
flags "github.com/jessevdk/go-flags"
109
"github.com/webdevops/go-shell"
11-
"gopkg.in/AlecAivazis/survey.v1"
12-
"./sync"
1310
"./logger"
1411
)
1512

@@ -36,7 +33,7 @@ var validConfigFiles = []string{
3633
".gosync.yaml",
3734
}
3835

39-
func createArgparser() {
36+
func handleArgParser() {
4037
var err error
4138
argparser = flags.NewParser(&opts, flags.Default)
4239
argparser.CommandHandler = func(command flags.Commander, args []string) error {
@@ -80,50 +77,6 @@ func createArgparser() {
8077
}
8178
}
8279

83-
func findConfigFile() string {
84-
pwd, err := os.Getwd()
85-
if err != nil {
86-
Logger.FatalErrorExit(1, err)
87-
fmt.Println(err)
88-
}
89-
90-
for true {
91-
for _, filename := range validConfigFiles {
92-
filepath := path.Join(pwd, filename)
93-
if sync.FileExists(filepath) {
94-
return filepath
95-
}
96-
}
97-
98-
99-
// already found root, we finished here
100-
if pwd == "/" {
101-
break
102-
}
103-
104-
pwd = path.Dir(pwd)
105-
106-
// oh, path seems to be empty.. stopping here now
107-
if pwd == "." || pwd == "" {
108-
break
109-
}
110-
}
111-
112-
return ""
113-
}
114-
115-
func getArgServer(config *sync.SyncConfig, confType string, userSelection string) string {
116-
if userSelection == "" {
117-
prompt := &survey.Select{
118-
Message: "Choose configuration:",
119-
Options: config.GetServerList(confType),
120-
}
121-
survey.AskOne(prompt, &userSelection, nil)
122-
}
123-
124-
return userSelection
125-
}
126-
12780
func main() {
12881
defer func() {
12982
if r := recover(); r != nil {
@@ -138,7 +91,7 @@ func main() {
13891
}
13992
}()
14093

141-
createArgparser()
94+
handleArgParser()
14295

14396
os.Exit(0)
14497
}

0 commit comments

Comments
 (0)