diff --git a/internal/app/app.go b/internal/app/app.go deleted file mode 100644 index d0c14cb..0000000 --- a/internal/app/app.go +++ /dev/null @@ -1,147 +0,0 @@ -package app - -import ( - "flag" - "fmt" - "os" - "runtime/debug" - "strings" -) - -type App struct { - categories []category - - printVersion bool -} - -type category struct { - name string - commands []Command -} - -type Command struct { - Run func(...string) error - Name string - Usage string -} - -func printAppVersion() { - bi, _ := debug.ReadBuildInfo() - valOf := func(k string) string { - for _, v := range bi.Settings { - if v.Key == k { - return v.Value - } - } - return "" - } - fmt.Println( - bi.Main.Version, - bi.GoVersion, - valOf("GOOS"), - valOf("GOARCH"), - valOf("vcs.revision"), - valOf("vcs.time"), - ) -} - -func (a *App) Run(args ...string) error { - fset := flag.NewFlagSet("pocryp", flag.ExitOnError) - fset.Usage = a.Usage - fset.BoolVar(&a.printVersion, "version", false, "") - if err := fset.Parse(args); err != nil { - return err - } - - args = fset.Args() - - if a.printVersion { - printAppVersion() - return nil - } - - if len(args) == 0 { - a.Usage() - return nil - } - - name := args[0] - args = args[1:] - - for _, category := range a.categories { - for _, cmd := range category.commands { - if cmd.Name == name { - return cmd.Run(args...) - } - } - } - - return fmt.Errorf("unknown command '%s'", name) -} - -func (a *App) Add(categoryName string, cmds ...Command) { - i := -1 - for ii, v := range a.categories { - if v.name == categoryName { - i = ii - } - } - if i == -1 { - a.categories = append(a.categories, category{name: categoryName}) - i = len(a.categories) - 1 - } - cat := &a.categories[i] - for _, cmd := range cmds { - if err := cat.hasCmd(cmd.Name); err != nil { - panic(err.Error()) - } - } - cat.commands = append(cat.commands, cmds...) -} - -func (c category) hasCmd(name string) error { - for _, cmd := range c.commands { - if cmd.Name == name { - return fmt.Errorf("category '%s' already has a command name '%s'", c.name, name) - } - } - return nil -} - -func (a App) maxCommandName(category string) int { - max := 0 - for _, v := range a.categories { - if v.name != category { - continue - } - for _, cmd := range v.commands { - if len(cmd.Name) > max { - max = len(cmd.Name) - } - } - } - return max -} - -func (a App) Usage() { - w := os.Stderr - fmt.Fprint(w, `Usage: pocryp command [ARGS] - -Flags: - -h, --help Print this message - --version Print version information - -Commands(by category): - -`) - for _, v := range a.categories { - fmt.Fprintf(w, "%s:\n", v.name) - mlen := a.maxCommandName(v.name) - for _, cmd := range v.commands { - padding := strings.Repeat(" ", mlen-len(cmd.Name)) - fmt.Fprintf(w, " %s%s %s\n", cmd.Name, padding, cmd.Usage) - } - fmt.Fprint(w, "\n") - } - fmt.Fprint(w, "Run 'pocryp command -h' for more information about a command.\n") -} diff --git a/internal/app/app_test.go b/internal/app/app_test.go deleted file mode 100644 index 5519278..0000000 --- a/internal/app/app_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package app - -import ( - "testing" -) - -func TestApp(t *testing.T) { - var app App - - if err := app.Run(); err != nil { - t.Fatal(err) - } - - if err := app.Run("nothing"); err == nil { - t.Fatal("expected an error") - } - - app.Add("foo", Command{ - Name: "bar", - Usage: "bar", - Run: func(args ...string) error { return nil }, - }) - app.Add("foo", Command{ - Name: "baz", - Usage: "baz", - Run: func(args ...string) error { return nil }, - }) - app.Add("fizz", Command{ - Name: "buzz", - Usage: "barr", - Run: func(args ...string) error { return nil }, - }) - - if err := app.Run("nothing"); err == nil { - t.Fatal("expected an error") - } - - if err := app.Run("bar"); err != nil { - t.Fatal(err) - } - - app.Usage() -} diff --git a/internal/cli/app.go b/internal/cli/app.go index b8fa7a3..7fb0994 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -137,7 +137,7 @@ Commands(by category): mlen := a.maxCommandName(v.name) for _, cmd := range v.commands { padding := strings.Repeat(" ", mlen-len(cmd.Name)) - fmt.Fprintf(w, " %s%s %s\n", cmd.Name, padding, cmd.Usage) + fmt.Fprintf(w, " %s%s %s\n", cmd.Name, padding, cmd.Brief) } fmt.Fprint(w, "\n") }