@@ -3,57 +3,111 @@ package main
3
3
import (
4
4
"os"
5
5
6
+ "github.com/urfave/cli/v2"
7
+
6
8
"github.com/yeqown/gitlab-flow/internal"
7
9
"github.com/yeqown/gitlab-flow/internal/conf"
8
10
"github.com/yeqown/gitlab-flow/internal/types"
9
11
10
12
"github.com/yeqown/log"
11
13
)
12
14
13
- func getFlow (confPath string , debug bool ) internal.IFlow {
14
- cfg := setEnviron (confPath , debug )
15
- // DONE(@yeqown) get cwd correctly.
16
- cwd , _ := os .Getwd ()
17
- ctx := types .NewContext (cwd , confPath , cfg )
15
+ // _cliGlobalFlags should be used like this:
16
+ // flow --debug -c path/to/config SUB-COMMAND [...options]
17
+ var _cliGlobalFlags = []cli.Flag {
18
+ & cli.StringFlag {
19
+ Name : "conf_path" ,
20
+ Aliases : []string {"c" },
21
+ Value : conf .DefaultConfPath (),
22
+ DefaultText : "~/.gitlab-flow" ,
23
+ Usage : "choose which `path/to/file` to load" ,
24
+ Required : false ,
25
+ },
26
+ & cli.BoolFlag {
27
+ Name : "debug" ,
28
+ Value : false ,
29
+ Usage : "verbose mode" ,
30
+ DefaultText : "false" ,
31
+ Required : false ,
32
+ },
33
+ & cli.StringFlag {
34
+ Name : "project" ,
35
+ Aliases : []string {"p" },
36
+ Value : "" ,
37
+ DefaultText : "extract working directory" ,
38
+ Usage : "input `projectName` to locate which project should be operate." ,
39
+ Required : false ,
40
+ },
41
+ & cli.BoolFlag {
42
+ Name : "web" ,
43
+ Value : false ,
44
+ Usage : "open web browser automatically or not" ,
45
+ DefaultText : "false" ,
46
+ Required : false ,
47
+ },
48
+ }
49
+
50
+ type globalFlags struct {
51
+ ConfPath string
52
+ DebugMode bool
53
+ ProjectName string
54
+ OpenBrowser bool
55
+ }
56
+
57
+ func parseGlobalFlags (c * cli.Context ) globalFlags {
58
+ return globalFlags {
59
+ ConfPath : c .String ("conf_path" ),
60
+ DebugMode : c .Bool ("debug" ),
61
+ ProjectName : c .String ("project" ),
62
+ OpenBrowser : c .Bool ("web" ),
63
+ }
64
+ }
65
+
66
+ func getFlow (c * cli.Context ) internal.IFlow {
67
+ flags := parseGlobalFlags (c )
68
+ ctx := setEnviron (flags )
18
69
return internal .NewFlow (ctx )
19
70
}
20
71
21
- func getDash (confPath string , debug bool ) internal.IDash {
22
- cfg := setEnviron (confPath , debug )
23
- cwd , _ := os .Getwd ()
24
- ctx := types .NewContext (cwd , confPath , cfg )
72
+ func getDash (c * cli.Context ) internal.IDash {
73
+ flags := parseGlobalFlags (c )
74
+ ctx := setEnviron (flags )
25
75
return internal .NewDash (ctx )
26
76
}
27
77
28
78
// setEnviron set global environment of debug mode.
29
- func setEnviron (confPath string , debug bool ) * types.Config {
30
- if ! debug {
79
+ // DONE(@yeqown): apply project name from CLI and CWD.
80
+ // TODO(@yeqown): CWD could be configured from CLI.
81
+ func setEnviron (flags globalFlags ) * types.FlowContext {
82
+ if ! flags .DebugMode {
31
83
log .SetLogLevel (log .LevelInfo )
32
84
} else {
33
85
// open caller report
34
86
log .SetCallerReporter (true )
35
87
log .SetLogLevel (log .LevelDebug )
36
88
}
37
-
38
89
log .
39
- WithFields (log.Fields {
40
- "confPath" : confPath ,
41
- "debug" : debug ,
42
- }).
90
+ WithField ("flags" , flags ).
43
91
Debugf ("setEnviron called" )
44
92
45
- cfg , err := conf .Load (confPath , nil )
93
+ // prepare configuration
94
+ cfg , err := conf .Load (flags .ConfPath , nil )
46
95
if err != nil {
47
96
log .
48
- Fatalf ("could not load config file from %s" , confPath )
97
+ WithField ("path" , flags .ConfPath ).
98
+ Fatalf ("could not load config file" )
49
99
panic ("could not reach" )
50
100
}
51
- if err = cfg .Debug (debug ).Valid (); err != nil {
101
+ if err = cfg .
102
+ Apply (flags .DebugMode , flags .OpenBrowser ).
103
+ Valid (); err != nil {
52
104
log .
53
- WithField ("config " , cfg ).
54
- Fatalf ("config is invalid: %s" , confPath )
105
+ WithField ("cfg " , cfg ).
106
+ Fatalf ("config is invalid" )
55
107
panic ("could not reach" )
56
108
}
57
109
58
- return cfg
110
+ // generate a FlowContext
111
+ cwd , _ := os .Getwd ()
112
+ return types .NewContext (cwd , flags .ConfPath , flags .ProjectName , cfg )
59
113
}
0 commit comments