git-bug.go

 1//go:generate go run pack_webui.go
 2
 3package main
 4
 5import (
 6	"fmt"
 7	"github.com/MichaelMure/git-bug/commands"
 8	"github.com/MichaelMure/git-bug/repository"
 9	"os"
10	"sort"
11	"strings"
12)
13
14const rootCommandName = "git bug"
15const usageMessageTemplate = `Usage: %s <command>
16
17Where <command> is one of:
18  %s
19
20For individual command usage, run:
21  %s help <command>
22`
23
24func rootUsage() {
25	var subcommands []string
26	for subcommand := range commands.CommandMap {
27		subcommands = append(subcommands, subcommand)
28	}
29	sort.Strings(subcommands)
30	fmt.Printf(usageMessageTemplate, rootCommandName, strings.Join(subcommands, "\n  "), rootCommandName)
31}
32
33func help(command string) {
34	subcommand, ok := commands.CommandMap[command]
35	if !ok {
36		fmt.Printf("Unknown command %q\n", command)
37		rootUsage()
38		return
39	}
40	subcommand.PrintUsage(rootCommandName, command)
41}
42
43func main() {
44	args := os.Args
45
46	// git bug
47	if len(args) == 1 {
48		fmt.Println("Will list bugs, not implemented yet")
49		//TODO: list bugs
50		return
51	}
52
53	if args[1] == "help" {
54		if len(args) == 2 {
55			// git bug help
56			rootUsage()
57		} else {
58			// git bug help <command>
59			help(args[2])
60		}
61		return
62	}
63
64	cwd, err := os.Getwd()
65	if err != nil {
66		fmt.Printf("Unable to get the current working directory: %q\n", err)
67		return
68	}
69	repo, err := repository.NewGitRepo(cwd)
70	if err != nil {
71		fmt.Printf("%s must be run from within a git repo.\n", rootCommandName)
72		return
73	}
74
75	subcommand, ok := commands.CommandMap[args[1]]
76	if !ok {
77		fmt.Printf("Unknown command: %q\n", args[1])
78		rootUsage()
79		return
80	}
81	if err := subcommand.Run(repo, args[2:]); err != nil {
82		fmt.Println(err.Error())
83		os.Exit(1)
84	}
85}