Skip to content

Commit a39a171

Browse files
committed
init
0 parents  commit a39a171

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+8923
-0
lines changed

Gopkg.lock

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/spf13/cobra"
30+
version = "0.0.3"
31+
32+
[[constraint]]
33+
name = "github.com/spf13/pflag"
34+
version = "1.0.2"
35+
36+
[prune]
37+
go-tests = true
38+
unused-packages = true

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2018 Dmitriy Kalinin
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# cobrautil
2+
3+
util package to help with https://github.com/spf13/cobra.

help_sections.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cobrautil
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func init() {
11+
cobra.AddTemplateFunc("commandsWithAnnotation", func(cmd *cobra.Command, key, value string) []*cobra.Command {
12+
var result []*cobra.Command
13+
for _, c := range cmd.Commands() {
14+
anns := map[string]string{}
15+
if c.Annotations != nil {
16+
anns = c.Annotations
17+
}
18+
if anns[key] == value {
19+
result = append(result, c)
20+
}
21+
}
22+
return result
23+
})
24+
}
25+
26+
type HelpSection struct {
27+
Key string
28+
Value string
29+
Title string
30+
}
31+
32+
func HelpSectionsUsageTemplate(sections []HelpSection) string {
33+
unmodifiedCmd := &cobra.Command{}
34+
usageTemplate := unmodifiedCmd.UsageTemplate()
35+
36+
const defaultTpl = `{{if .HasAvailableSubCommands}}
37+
38+
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
39+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}`
40+
41+
if !strings.Contains(usageTemplate, defaultTpl) {
42+
panic("Expected to find available commands section in spf13/cobra default usage template")
43+
}
44+
45+
newTpl := "{{if .HasAvailableSubCommands}}"
46+
47+
for _, section := range sections {
48+
newTpl += fmt.Sprintf(`{{$cmds := (commandsWithAnnotation . "%s" "%s")}}{{if $cmds}}
49+
50+
%s{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
51+
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}`, section.Key, section.Value, section.Title)
52+
}
53+
54+
newTpl += "{{end}}"
55+
56+
return strings.Replace(usageTemplate, defaultTpl, newTpl, 1)
57+
}

misc.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cobrautil
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
func VisitCommands(cmd *cobra.Command, f func(*cobra.Command)) {
11+
f(cmd)
12+
for _, child := range cmd.Commands() {
13+
VisitCommands(child, f)
14+
}
15+
}
16+
17+
func WrapRunEForCmd(additionalRunE func(*cobra.Command, []string) error) func(cmd *cobra.Command) {
18+
return func(cmd *cobra.Command) {
19+
origRunE := cmd.RunE
20+
cmd.RunE = func(cmd2 *cobra.Command, args []string) error {
21+
err := additionalRunE(cmd2, args)
22+
if err != nil {
23+
return err
24+
}
25+
return origRunE(cmd2, args)
26+
}
27+
}
28+
}
29+
30+
func ReconfigureCmdWithSubcmd(cmd *cobra.Command) {
31+
if len(cmd.Commands()) == 0 {
32+
return
33+
}
34+
35+
if cmd.Args == nil {
36+
cmd.Args = cobra.ArbitraryArgs
37+
}
38+
if cmd.RunE == nil {
39+
cmd.RunE = ShowSubcommands
40+
}
41+
42+
var strs []string
43+
for _, subcmd := range cmd.Commands() {
44+
if !subcmd.Hidden {
45+
strs = append(strs, subcmd.Use)
46+
}
47+
}
48+
49+
cmd.Short += " (" + strings.Join(strs, ", ") + ")"
50+
}
51+
52+
func ReconfigureLeafCmd(cmd *cobra.Command) {
53+
if len(cmd.Commands()) > 0 {
54+
return
55+
}
56+
57+
if cmd.RunE == nil {
58+
panic(fmt.Sprintf("Internal: Command '%s' does not set RunE", cmd.CommandPath()))
59+
}
60+
61+
if cmd.Args == nil {
62+
origRunE := cmd.RunE
63+
cmd.RunE = func(cmd2 *cobra.Command, args []string) error {
64+
if len(args) > 0 {
65+
return fmt.Errorf("command '%s' does not accept extra arguments '%s'", args[0], cmd2.CommandPath())
66+
}
67+
return origRunE(cmd2, args)
68+
}
69+
cmd.Args = cobra.ArbitraryArgs
70+
}
71+
}
72+
73+
func ShowSubcommands(cmd *cobra.Command, args []string) error {
74+
var strs []string
75+
for _, subcmd := range cmd.Commands() {
76+
if !subcmd.Hidden {
77+
strs = append(strs, subcmd.Use)
78+
}
79+
}
80+
return fmt.Errorf("Use one of available subcommands: %s", strings.Join(strs, ", "))
81+
}
82+
83+
func ShowHelp(cmd *cobra.Command, args []string) error {
84+
cmd.Help()
85+
return fmt.Errorf("Invalid command - see available commands/subcommands above")
86+
}

resolvable_flags.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cobrautil
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/spf13/pflag"
6+
)
7+
8+
type ResolvableFlag interface {
9+
Resolve() error
10+
}
11+
12+
func ResolveFlagsForCmd(cmd *cobra.Command, args []string) error {
13+
var lastFlagErr error
14+
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
15+
if flag.Value == nil {
16+
return
17+
}
18+
if resolvableVal, ok := flag.Value.(ResolvableFlag); ok {
19+
err := resolvableVal.Resolve()
20+
if err != nil {
21+
lastFlagErr = err
22+
}
23+
}
24+
})
25+
return lastFlagErr
26+
}

vendor/github.com/inconshreveable/mousetrap/LICENSE

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/inconshreveable/mousetrap/README.md

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/inconshreveable/mousetrap/trap_others.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)