/
root.go
97 lines (84 loc) · 2.72 KB
/
root.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
/*
winch - Universal Build and Release Tool
Copyright (C) 2021 Ketch Kloud, Inc.
This program 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.
This program 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 this program. If not,
see <https://www.gnu.org/licenses/>.
*/
package commands
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/winchci/winch/pkg/config"
"github.com/winchci/winch/version"
"log"
"os"
"strings"
)
var rootCmd = &cobra.Command{
Use: version.Name,
Short: version.Description,
Version: version.String(),
TraverseChildren: true,
PersistentPreRunE: config.Setup(),
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
func commandName(c *cobra.Command) string {
parts := make([]string, 0)
for c != nil {
parts = append([]string{c.Name()}, parts...)
c = c.Parent()
}
return strings.Join(parts[1:], " ")
}
func Runner(f func(ctx context.Context) error) func(c *cobra.Command, args []string) {
return func(c *cobra.Command, args []string) {
err := f(config.AddCommandToContext(context.Background(), c))
if err != nil {
name := commandName(c)
fmt.Printf("%s: failed\n%s", name, err)
os.Exit(1)
}
}
}
func RunnerWithArgs(f func(ctx context.Context, args []string) error) func(c *cobra.Command, args []string) {
return func(c *cobra.Command, args []string) {
err := f(config.AddCommandToContext(context.Background(), c), args)
if err != nil {
name := commandName(c)
fmt.Printf("%s: failed\n%s", name, err)
os.Exit(1)
}
}
}
func init() {
rootCmd.SetHelpCommand(&cobra.Command{
Use: "help [command]",
Short: "Help about any command",
Long: `Help provides help for any command in the application.
Simply type ` + rootCmd.Name() + ` help [path to command] for full details.`,
Run: func(c *cobra.Command, args []string) {
cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil {
c.Printf("Unknown help topic %#q\n", args)
c.Root().Usage()
} else {
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.Help()
}
},
})
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "quiet output")
rootCmd.PersistentFlags().StringP("file", "f", "winch.yml", "configuration file")
}