github.com/x-motemen/ghq@v1.6.1/commands.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/urfave/cli/v2" 8 ) 9 10 var commands = []*cli.Command{ 11 commandGet, 12 commandList, 13 commandRm, 14 commandRoot, 15 commandCreate, 16 } 17 18 var commandGet = &cli.Command{ 19 Name: "get", 20 Aliases: []string{"clone"}, 21 Usage: "Clone/sync with a remote repository", 22 Description: ` 23 Clone a repository under ghq root directory. If the repository is 24 already cloned to local, nothing will happen unless '-u' ('--update') 25 flag is supplied, in which case 'git remote update' is executed. 26 When you use '-p' option, the repository is cloned via SSH.`, 27 Action: doGet, 28 Flags: []cli.Flag{ 29 &cli.BoolFlag{Name: "update", Aliases: []string{"u"}, 30 Usage: "Update local repository if cloned already"}, 31 &cli.BoolFlag{Name: "p", Usage: "Clone with SSH"}, 32 &cli.BoolFlag{Name: "shallow", Usage: "Do a shallow clone"}, 33 &cli.BoolFlag{Name: "look", Aliases: []string{"l"}, Usage: "Look after get"}, 34 &cli.StringFlag{Name: "vcs", Usage: "Specify `vcs` backend for cloning"}, 35 &cli.BoolFlag{Name: "silent", Aliases: []string{"s"}, Usage: "clone or update silently"}, 36 &cli.BoolFlag{Name: "no-recursive", Usage: "prevent recursive fetching"}, 37 &cli.StringFlag{Name: "branch", Aliases: []string{"b"}, 38 Usage: "Specify `branch` name. This flag implies --single-branch on Git"}, 39 &cli.BoolFlag{Name: "parallel", Aliases: []string{"P"}, Usage: "Import parallelly"}, 40 &cli.BoolFlag{Name: "bare", Usage: "Do a bare clone"}, 41 }, 42 } 43 44 var commandList = &cli.Command{ 45 Name: "list", 46 Usage: "List local repositories", 47 Description: ` 48 List locally cloned repositories. If a query argument is given, only 49 repositories whose names contain that query text are listed. 50 '-e' ('--exact') forces the match to be an exact one (i.e. the query equals to 51 project or user/project) If '-p' ('--full-path') is given, the full paths 52 to the repository root are printed instead of relative ones.`, 53 Action: doList, 54 Flags: []cli.Flag{ 55 &cli.BoolFlag{Name: "exact", Aliases: []string{"e"}, Usage: "Perform an exact match"}, 56 &cli.StringFlag{Name: "vcs", Usage: "Specify `vcs` backend for matching"}, 57 &cli.BoolFlag{Name: "full-path", Aliases: []string{"p"}, Usage: "Print full paths"}, 58 &cli.BoolFlag{Name: "unique", Usage: "Print unique subpaths"}, 59 &cli.BoolFlag{Name: "bare", Usage: "Query bare repositories"}, 60 }, 61 } 62 63 var commandRm = &cli.Command{ 64 Name: "rm", 65 Usage: "Remove local repository", 66 Action: doRm, 67 Flags: []cli.Flag{ 68 &cli.BoolFlag{Name: "dry-run", Usage: "Do not remove actually"}, 69 }, 70 } 71 72 var commandRoot = &cli.Command{ 73 Name: "root", 74 Usage: "Show repositories' root", 75 Action: doRoot, 76 Flags: []cli.Flag{ 77 &cli.BoolFlag{Name: "all", Usage: "Show all roots"}, 78 }, 79 } 80 81 var commandCreate = &cli.Command{ 82 Name: "create", 83 Usage: "Create a new repository", 84 Action: doCreate, 85 Flags: []cli.Flag{ 86 &cli.StringFlag{Name: "vcs", Usage: "Specify `vcs` backend explicitly"}, 87 &cli.BoolFlag{Name: "bare", Usage: "Create a bare repository"}, 88 }, 89 } 90 91 type commandDoc struct { 92 Parent string 93 Arguments string 94 } 95 96 var commandDocs = map[string]commandDoc{ 97 "get": {"", "[-u] [-p] [--shallow] [--vcs <vcs>] [--look] [--silent] [--branch <branch>] [--no-recursive] [--bare] <repository URL>|<project>|<user>/<project>|<host>/<user>/<project>"}, 98 "list": {"", "[-p] [-e] [<query>]"}, 99 "create": {"", "<project>|<user>/<project>|<host>/<user>/<project>"}, 100 "rm": {"", "<project>|<user>/<project>|<host>/<user>/<project>"}, 101 "root": {"", "[-all]"}, 102 } 103 104 // Makes template conditionals to generate per-command documents. 105 func mkCommandsTemplate(genTemplate func(commandDoc) string) string { 106 template := "{{if false}}" 107 for _, command := range commands { 108 template = template + fmt.Sprintf("{{else if (eq .Name %q)}}%s", command.Name, genTemplate(commandDocs[command.Name])) 109 } 110 return template + "{{end}}" 111 } 112 113 func init() { 114 argsTemplate := mkCommandsTemplate(func(doc commandDoc) string { return doc.Arguments }) 115 parentTemplate := mkCommandsTemplate(func(doc commandDoc) string { return strings.TrimLeft(doc.Parent+" ", " ") }) 116 117 cli.CommandHelpTemplate = `NAME: 118 {{.Name}} - {{.Usage}} 119 120 USAGE: 121 ghq ` + parentTemplate + `{{.Name}} ` + argsTemplate + ` 122 {{if (len .Description)}} 123 DESCRIPTION: {{.Description}} 124 {{end}}{{if (len .Flags)}} 125 OPTIONS: 126 {{range .Flags}}{{.}} 127 {{end}} 128 {{end}}` 129 }