-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
105 lines (90 loc) · 2.55 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"os"
"log"
"fmt"
"runtime/debug"
flags "github.com/jessevdk/go-flags"
"github.com/webdevops/go-shell"
"./logger"
)
const (
// application informations
Name = "gosync"
Author = "webdevops.io"
Version = "0.3.0"
// self update informations
GithubOrganization = "webdevops"
GithubRepository = "go-sync"
GithubAssetTemplate = "gosync-%OS%-%ARCH%"
)
var (
Logger *logger.SyncLogger
argparser *flags.Parser
args []string
)
var opts struct {
Verbose []bool `short:"v" long:"verbose" description:"verbose mode"`
}
var validConfigFiles = []string{
"gosync.yml",
"gosync.yaml",
".gosync.yml",
".gosync.yaml",
}
func handleArgParser() {
var err error
argparser = flags.NewParser(&opts, flags.Default)
argparser.CommandHandler = func(command flags.Commander, args []string) error {
switch {
case len(opts.Verbose) >= 2:
shell.Trace = true
shell.TracePrefix = "[CMD] "
Logger = logger.GetInstance(argparser.Command.Name, log.Ldate|log.Ltime|log.Lshortfile)
fallthrough
case len(opts.Verbose) >= 1:
logger.Verbose = true
shell.VerboseFunc = func(c *shell.Command) {
Logger.Command(c.ToString())
}
fallthrough
default:
if Logger == nil {
Logger = logger.GetInstance(argparser.Command.Name, 0)
}
}
return command.Execute(args)
}
argparser.AddCommand("version", "Show version", fmt.Sprintf("Show %s version", Name), &VersionCommand{Name:Name, Version:Version, Author:Author})
argparser.AddCommand("self-update", "Self update", "Run self update of this application", &SelfUpdateCommand{GithubOrganization:GithubOrganization, GithubRepository:GithubRepository, GithubAssetTemplate:GithubAssetTemplate, CurrentVersion:Version})
argparser.AddCommand("list", "List server configurations", "List server configurations", &ListCommand{})
argparser.AddCommand("sync", "Sync from server", "Sync filesystem and databases from server", &SyncCommand{})
argparser.AddCommand("deploy", "Deploy to server", "Deploy filesystem and databases to server", &DeployCommand{})
args, err = argparser.Parse()
// check if there is an parse error
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println()
if len(opts.Verbose) >= 2 {
fmt.Println(r)
debug.PrintStack()
} else {
fmt.Println(r)
}
os.Exit(255)
}
}()
handleArgParser()
os.Exit(0)
}