diff --git a/bug/bug.go b/bug/bug.go index f536716e53fd6baeb4c844f3c3d9e823db845772..a82ee37129ee28b99564d089b1a742143787acaf 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -34,7 +34,7 @@ type Bug struct { // Create a new Bug func NewBug() (*Bug, error) { - // TODO: replace with simple random bytes + hash + // TODO: replace with commit hash of (first commit + some random) // Creating UUID Version 4 unique, err := uuid.ID4() diff --git a/commands/command.go b/commands/command.go new file mode 100644 index 0000000000000000000000000000000000000000..30298d3fa7ce6b09b243a2ba4fbfba2bacc3b9d2 --- /dev/null +++ b/commands/command.go @@ -0,0 +1,54 @@ +// Package commands contains the assorted sub commands supported by the git-bug tool. +package commands + +import ( + "flag" + "fmt" + "github.com/MichaelMure/git-bug/repository" +) + +const messageFilename = "BUG_MESSAGE_EDITMSG" + +// Command represents the definition of a single command. +type Command struct { + // Short description of the command + Description string + // Command line usage + Usage string + // Flag set of the command + flagSet *flag.FlagSet + // Execute the command + RunMethod func(repository.Repo, []string) error +} + +// Run executes a command, given its arguments. +// +// The args parameter is all of the command line args that followed the +// subcommand. +func (cmd *Command) Run(repo repository.Repo, args []string) error { + return cmd.RunMethod(repo, args) +} + +func (cmd *Command) PrintUsage(rootCommand string, cmdName string) { + fmt.Printf("Usage: %s %s %s\n", rootCommand, cmdName, cmd.Usage) + + if cmd.flagSet != nil { + fmt.Printf("\nOptions:\n") + cmd.flagSet.PrintDefaults() + } +} + +// CommandMap defines all of the available (sub)commands. +var CommandMap map[string]*Command + +// We use init() to avoid a cycle in the data initialization because of the "commands" command +func init() { + CommandMap = map[string]*Command{ + "commands": commandsCmd, + "comment": commentCmd, + "ls": lsCmd, + "new": newCmd, + "pull": pullCmd, + "push": pushCmd, + } +} diff --git a/commands/commands.go b/commands/commands.go index faca02ab5a55d3ef6e0ee22f924064acacdeaf4a..0a3c33ad968bc60d08bb0922a54c1deae5310372 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,31 +1,44 @@ -// Package commands contains the assorted sub commands supported by the git-bug tool. package commands import ( + "flag" + "fmt" "github.com/MichaelMure/git-bug/repository" ) -const messageFilename = "BUG_MESSAGE_EDITMSG" +var commandsFlagSet = flag.NewFlagSet("commands", flag.ExitOnError) -// Command represents the definition of a single command. -type Command struct { - Usage func(string) - RunMethod func(repository.Repo, []string) error -} +var ( + commandsDesc = commandsFlagSet.Bool("pretty", false, "Output the command description as well as Markdown compatible comment") +) + +func runCommands(repo repository.Repo, args []string) error { + commandsFlagSet.Parse(args) + args = commandsFlagSet.Args() + + first := true + + for name, cmd := range CommandMap { + if !first { + fmt.Println() + } + + first = false + + if *commandsDesc { + fmt.Printf("# %s\n", cmd.Description) + } + + // TODO: the root name command ("git bug") should be passed from git-bug.go but well ... + fmt.Printf("%s %s %s\n", "git bug", name, cmd.Usage) + } -// Run executes a command, given its arguments. -// -// The args parameter is all of the command line args that followed the -// subcommand. -func (cmd *Command) Run(repo repository.Repo, args []string) error { - return cmd.RunMethod(repo, args) + return nil } -// CommandMap defines all of the available (sub)commands. -var CommandMap = map[string]*Command{ - "comment": commentCmd, - "ls": lsCmd, - "new": newCmd, - "pull": pullCmd, - "push": pushCmd, +var commandsCmd = &Command{ + Description: "Display available commands", + Usage: "[