-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
and fix some newly appearing linter errors Signed-off-by: Knut Ahlers <knut@ahlers.me>
- Loading branch information
Showing
24 changed files
with
47 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/Luzifer/go_helpers/v2/cli" | ||
) | ||
|
||
type ( | ||
cliRegistry struct { | ||
cmds map[string]cliRegistryEntry | ||
sync.Mutex | ||
} | ||
|
||
cliRegistryEntry struct { | ||
Description string | ||
Name string | ||
Params []string | ||
Run func([]string) error | ||
} | ||
) | ||
|
||
var ( | ||
cli = newCLIRegistry() | ||
errHelpCalled = errors.New("help called") | ||
) | ||
|
||
func newCLIRegistry() *cliRegistry { | ||
return &cliRegistry{ | ||
cmds: make(map[string]cliRegistryEntry), | ||
} | ||
} | ||
|
||
func (c *cliRegistry) Add(e cliRegistryEntry) { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
c.cmds[e.Name] = e | ||
} | ||
|
||
func (c *cliRegistry) Call(args []string) error { | ||
c.Lock() | ||
defer c.Unlock() | ||
|
||
cmdEntry := c.cmds[args[0]] | ||
if cmdEntry.Name != args[0] { | ||
c.help() | ||
return errHelpCalled | ||
} | ||
|
||
return cmdEntry.Run(args) | ||
} | ||
|
||
func (c *cliRegistry) help() { | ||
// Called from Call, does not need lock | ||
|
||
var ( | ||
maxCmdLen int | ||
cmds []cliRegistryEntry | ||
) | ||
|
||
for name := range c.cmds { | ||
entry := c.cmds[name] | ||
if l := len(entry.CommandDisplay()); l > maxCmdLen { | ||
maxCmdLen = l | ||
} | ||
cmds = append(cmds, entry) | ||
} | ||
|
||
sort.Slice(cmds, func(i, j int) bool { return cmds[i].Name < cmds[j].Name }) | ||
|
||
tpl := fmt.Sprintf(" %%-%ds %%s\n", maxCmdLen) | ||
fmt.Fprintln(os.Stdout, "Supported sub-commands are:") | ||
for _, cmd := range cmds { | ||
fmt.Fprintf(os.Stdout, tpl, cmd.CommandDisplay(), cmd.Description) | ||
} | ||
} | ||
|
||
func (c cliRegistryEntry) CommandDisplay() string { | ||
return strings.Join(append([]string{c.Name}, c.Params...), " ") | ||
} | ||
var cliTool = cli.New() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.