hub-pr ãä½ãã¨ããï¼ghq ãªã©ã§ä½¿ã£ã¦ããï¼codegangsta/cli ã§ã¯ãªãæ°ããã©ã¤ãã©ãªã試ãã¦ã¿ããã¨æã£ã¦ mitchellh/cli ã使ã£ã¦ã¿ããã©ãä½ããã£ããããªããã®ããã£ãã®ã§ããã£ããã ããã¨èªä½ãã¦ã¿ããä»åã®è¦ä»¶ã¯ä»¥ä¸ã®ã¨ããã
- ï¼
go
ãgit
ã®ããã«ï¼ãµãã³ãã³ãããã - ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã®è§£æã«ã¯æ¨æºã®
flag
ããã±ã¼ã¸ãã¤ãã - ã³ãã³ãã追å ããã®ãé¢åã§ãªã
ã§ãgo-cliï¼ããã¥ã¡ã³ãï¼ã
ã³ãã³ãã®å®è£
hub-pr ã®ã½ã¼ã¹ã³ã¼ããä¸çªã®ãµã³ãã«ã ãã©ãã³ãã³ãã¯ä»¥ä¸ã®ãããªé¢æ°ã§è¡¨ç¾ãããã
func doCheckout(flags *flag.FlagSet, args []string) error {
åã³ãã³ãã®å®è£
ã¯åæåããã *flag.FlagSet
ã¨ã³ãã³ãã®å¼æ°ï¼ããã°ã©ã ã®ç¬¬1å¼æ°ãã³ãã³ãåã«ãªãã®ã§ os.Args[2:]
ï¼ãåãã¨ãããã©ã°ã®ç®¡çã¨å¼æ°ã®å¦çããããªãã®ã¯ããããã®é¢æ°ã®è²¬ä»»ãã¾ã£ãããããªããªãã¦ãããã
flags.StringVar(&tmpl, "f", tmpl, "branch name format") flags.Parse(args)
ã¾ããåé¢æ°ã¯ error ãè¿ããã¨ã§ç°å¸¸çµäºãããã cli ã«æ示ã§ãããã¨ãã« cli.ErrUsage
ã¯ä½¿ç¨æ¹æ³ããããããã¨ã示ããflags
ãå©ç¨ãã¤ã¤å¼ã³åºãå´ã§ããæãã«ã¨ã©ã¼ã表示ãã¦ãããã
% hub-pr checkout Usage: checkout [-f TEMPLATE] PULL_REQUEST_NUMBER Checks out a branch corresponding to given pull request number. The branch name is based on the template, which defaults to '#{{.Number}}-{{.Head.Repo.Owner.Login}}/{{.Head.Ref}}'. Options: -f="#{{.Number}}-{{.Head.Repo.Owner.Login}}/{{.Head.Ref}}": branch name format
ã³ãã³ãã®ç»é²ã»å®è¡
ã³ãã³ããç»é²ããå®è¡ããã«ã¯ä»¥ä¸ã®ããã«ããã
cli.Use( &cli.Command{ Name: "checkout", Action: doCheckout, Short: "Checkout a branch for a pull request", Long: "checkout [-f TEMPLATE] ...", }, ) cli.Run(os.Args[1:])
ã§ããã® Name ãã Long ããããã«ããæ¸ãã®ãé¢åãªãã§ããâ¦â¦ãã¨ãã«æååãªãã©ã«ã¨ãã¦æ¸ããªããã°ãªããªããããã«ãªãã¨ãªãã¹ãã¬ã¹ãæãããããã¥ã¡ã³ããªã®ã ããæ®éã«æ¸ããããããã§ã³ã¡ã³ãã使ãã
// +command checkout - Checkout a branch for a pull request // // checkout [-f TEMPLATE] PULL_REQUEST_NUMBER // // Checks out a branch corresponding to given pull request // ... func doCheckout(flags *flag.FlagSet, args []string) error {
以ä¸ã®ããã«ãã³ãã³ãã«å¯¾å¿ããé¢æ°ã®ããã¥ã¡ã³ãã« "+command" ã§å§ã¾ããã«ããå«ãã¦ããã¨ããã«ã¼ã«ã«ãããæ®éã®æååãªãã©ã«ã«ããã¹ã¦ãã³ã¡ã³ãã§ããã¥ã¡ã³ããæ¸ãã»ããã¡ã³ããããããã³ã¼ããæ¸ãã¨ãã«ãåç §ãããããã¯ãã
ã³ã¡ã³ãããã³ãã³ãã®ç»é²ã³ã¼ããçæãã
ãã®ã³ã¡ã³ãã©ã使ãã®ã£ã¦è©±ã ãã©ãããããå
ã«æãããã㪠cli.Use()
ã®å½¢ã®ã³ã¼ããçæããããã®ï¼éãªï¼ API ã go-cli/gen ã¨ãã¦æä¾ãã¦ããã
hub-pr ã§ã¯ _tools/gen_commands.go ããå©ç¨ãã¦ãããããã¥ã¡ã³ããæ´æ°ããã go run _tools/gen_commands.go
ããã° commands.go ãèªåçæããããå®éã«ã¯ go generatable ã«ãã¦ãããããã¸ã§ã¯ãã«ãããã¦èªç±ã«ä½¿ããããã«ããã¼ã«ã®å½¢ã§ã¯æä¾ãã¦ããªãã