-
Notifications
You must be signed in to change notification settings - Fork 1
/
scm.go
180 lines (144 loc) · 4.27 KB
/
scm.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/garethjevans/scm/pkg/cmd"
"github.com/garethjevans/scm/pkg/version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra/doc"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// Version is dynamically set by the toolchain or overridden by the Makefile.
var Version = version.Version
// Verbose enable verbose logging, set by the --debug flag.
var Verbose bool
// Raw all tables should be displayed in raw format.
var Raw bool
// NoHeaders do not write table headers.
var NoHeaders bool
// Markdown all tables should be written in markdown format.
var Markdown bool
// BuildDate is dynamically set at build time in the Makefile.
var BuildDate = version.BuildDate
var versionOutput = ""
func init() {
if strings.Contains(Version, "dev") {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "(devel)" {
Version = info.Main.Version
}
}
Version = strings.TrimPrefix(Version, "v")
if BuildDate == "" {
RootCmd.Version = Version
} else {
RootCmd.Version = fmt.Sprintf("%s (%s)", Version, BuildDate)
}
versionOutput = fmt.Sprintf("inspect version %s\n", RootCmd.Version)
RootCmd.AddCommand(versionCmd)
RootCmd.SetVersionTemplate(versionOutput)
RootCmd.AddCommand(docsCmd)
RootCmd.PersistentFlags().Bool("help", false, "Show help for command")
RootCmd.PersistentFlags().BoolVarP(&Verbose, "debug", "v", false, "Debug Output")
RootCmd.Flags().Bool("version", false, "Show version")
RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
}
return &FlagError{Err: err}
})
RootCmd.AddCommand(cmd.NewURLCmd())
RootCmd.AddCommand(cmd.NewTokenCmd())
RootCmd.AddCommand(cmd.NewPrCmd())
RootCmd.AddCommand(cmd.NewMonoRepoChangeCmd())
RootCmd.AddCommand(cmd.NewTagCmd())
RootCmd.PersistentPreRun = func(command *cobra.Command, args []string) {
if Verbose {
logrus.SetLevel(logrus.DebugLevel)
}
}
c := completionCmd
c.Flags().StringP("shell", "s", "bash", "Shell type: {bash|zsh|fish|powershell}")
RootCmd.AddCommand(c)
}
// FlagError is the kind of error raised in flag processing.
type FlagError struct {
Err error
}
// Error.
func (fe FlagError) Error() string {
return fe.Err.Error()
}
// Unwrap FlagError.
func (fe FlagError) Unwrap() error {
return fe.Err
}
// RootCmd is the entry point of command-line execution.
var RootCmd = &cobra.Command{
Use: "scm",
Short: "provides commands for interacting with different scm providers",
Long: `a CLI used to interact with different scm providers`,
SilenceErrors: false,
SilenceUsage: true,
}
var versionCmd = &cobra.Command{
Use: "version",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(versionOutput)
},
}
var docsCmd = &cobra.Command{
Use: "docs",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
RootCmd.DisableAutoGenTag = true
err := doc.GenMarkdownTree(RootCmd, "./docs")
if err != nil {
panic(err)
}
},
}
var completionCmd = &cobra.Command{
Use: "completion",
Hidden: true,
Short: "Generate shell completion scripts",
Long: `Generate shell completion scripts for GitHub CLI commands.
The output of this command will be computer code and is meant to be saved to a
file or immediately evaluated by an interactive shell.
For example, for bash you could add this to your '~/.bash_profile':
eval "$(gh completion -s bash)"
When installing GitHub CLI through a package manager, however, it's possible that
no additional shell configuration is necessary to gain completion support. For
Homebrew, see <https://docs.brew.sh/Shell-Completion>
`,
RunE: func(cmd *cobra.Command, args []string) error {
shellType, err := cmd.Flags().GetString("shell")
if err != nil {
return err
}
if shellType == "" {
shellType = "bash"
}
switch shellType {
case "bash":
return RootCmd.GenBashCompletion(cmd.OutOrStdout())
case "zsh":
return RootCmd.GenZshCompletion(cmd.OutOrStdout())
case "fish":
return RootCmd.GenFishCompletion(cmd.OutOrStdout(), true)
case "powershell":
return RootCmd.GenPowerShellCompletion(cmd.OutOrStdout())
default:
return fmt.Errorf("unsupported shell type %q", shellType)
}
},
}
func main() {
if err := RootCmd.Execute(); err != nil {
os.Exit(1)
}
os.Exit(0)
}