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