git-bug/git-bug.go

84 lines
1.6 KiB
Go
Raw Normal View History

2018-07-10 15:18:50 +03:00
package main
2018-07-12 10:55:13 +03:00
import (
"fmt"
2018-07-12 13:45:09 +03:00
"github.com/MichaelMure/git-bug/commands"
"github.com/MichaelMure/git-bug/repository"
2018-07-12 10:55:13 +03:00
"os"
"sort"
"strings"
)
2018-07-12 13:45:09 +03:00
const rootCommandName = "git bug"
2018-07-12 10:55:13 +03:00
const usageMessageTemplate = `Usage: %s <command>
Where <command> is one of:
%s
For individual command usage, run:
%s help <command>
`
2018-07-12 13:45:09 +03:00
func rootUsage() {
2018-07-12 10:55:13 +03:00
var subcommands []string
for subcommand := range commands.CommandMap {
subcommands = append(subcommands, subcommand)
}
sort.Strings(subcommands)
2018-07-12 13:45:09 +03:00
fmt.Printf(usageMessageTemplate, rootCommandName, strings.Join(subcommands, "\n "), rootCommandName)
2018-07-12 10:55:13 +03:00
}
2018-07-12 13:45:09 +03:00
func help(command string) {
subcommand, ok := commands.CommandMap[command]
2018-07-12 10:55:13 +03:00
if !ok {
2018-07-12 13:45:09 +03:00
fmt.Printf("Unknown command %q\n", command)
rootUsage()
2018-07-12 10:55:13 +03:00
return
}
2018-07-16 16:24:34 +03:00
subcommand.PrintUsage(rootCommandName, command)
2018-07-12 10:55:13 +03:00
}
2018-07-10 15:18:50 +03:00
func main() {
2018-07-12 13:45:09 +03:00
args := os.Args
// git bug
if len(args) == 1 {
fmt.Println("Will list bugs, not implemented yet")
2018-07-12 13:45:09 +03:00
//TODO: list bugs
2018-07-12 10:55:13 +03:00
return
}
2018-07-12 13:45:09 +03:00
if args[1] == "help" {
if len(args) == 2 {
// git bug help
rootUsage()
} else {
// git bug help <command>
help(args[2])
}
return
}
2018-07-12 10:55:13 +03:00
cwd, err := os.Getwd()
if err != nil {
fmt.Printf("Unable to get the current working directory: %q\n", err)
return
}
repo, err := repository.NewGitRepo(cwd)
if err != nil {
2018-07-12 13:45:09 +03:00
fmt.Printf("%s must be run from within a git repo.\n", rootCommandName)
2018-07-12 10:55:13 +03:00
return
}
2018-07-12 13:45:09 +03:00
subcommand, ok := commands.CommandMap[args[1]]
2018-07-12 10:55:13 +03:00
if !ok {
2018-07-12 13:45:09 +03:00
fmt.Printf("Unknown command: %q\n", args[1])
rootUsage()
2018-07-12 10:55:13 +03:00
return
}
2018-07-12 13:45:09 +03:00
if err := subcommand.Run(repo, args[2:]); err != nil {
2018-07-12 10:55:13 +03:00
fmt.Println(err.Error())
os.Exit(1)
}
2018-07-10 15:18:50 +03:00
}