forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
127 lines (104 loc) · 3.5 KB
/
cmd.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2015 ThoughtWorks, Inc.
// This file is part of Gauge.
// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Gauge. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"errors"
"fmt"
"github.com/getgauge/common"
"github.com/getgauge/gauge/config"
"github.com/getgauge/gauge/logger"
"github.com/getgauge/gauge/util"
"github.com/spf13/cobra"
)
var (
GaugeCmd = &cobra.Command{
Use: "gauge <command> [flags] [args]",
Example: ` gauge run specs/
gauge run --parallel specs/`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Help()
}
},
DisableAutoGenTag: true,
}
logLevel string
dir string
)
func init() {
GaugeCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}
Aliases:
{{.NameAndAliases}}{{end}}{{if .HasExample}}
Examples:
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Use "{{.CommandPath}} [command] --help" for more information about a command.
Complete documentation is available at https://docs.getgauge.io/.{{end}}
`)
GaugeCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "Set level of logging to debug, info, warning, error or critical")
GaugeCmd.PersistentFlags().StringVarP(&dir, "dir", "d", ".", "Set the working directory for the current command, accepts a path relative to current directory")
}
type commandWriter struct {
bytes []byte
}
func (w *commandWriter) Write(p []byte) (int, error) {
w.bytes = append(w.bytes, p...)
return len(p), nil
}
func (w *commandWriter) display() {
fmt.Print(string(w.bytes))
}
func Parse() (int, error) {
writer := &commandWriter{}
GaugeCmd.SetOutput(writer)
InitHelp(GaugeCmd)
if cmd, err := GaugeCmd.ExecuteC(); err != nil {
if cmd == GaugeCmd {
return 1, errors.New("Failed parsing using the new gauge command structure, falling back to the old usage.")
}
writer.display()
return 1, nil
}
writer.display()
return 0, nil
}
func InitHelp(c *cobra.Command) {
c.Flags().BoolP("help", "h", false, "Help for "+c.Name())
if c.HasSubCommands() {
for _, sc := range c.Commands() {
InitHelp(sc)
}
}
}
func getSpecsDir(args []string) []string {
if len(args) > 0 {
return args
}
return []string{common.SpecsDirectoryName}
}
func isValidGaugeProject(args []string) error {
return config.SetProjectRoot(args)
}
func setGlobalFlags() {
logger.Initialize(logLevel)
logger.Debugf("Gauge Install ID: %s", config.UniqueID())
util.SetWorkingDir(dir)
}