From d189d4666703c5181f6208aa7674d6a83c282a9c Mon Sep 17 00:00:00 2001 From: mediocregopher <> Date: Sat, 4 Apr 2020 11:54:31 -0600 Subject: [PATCH] refactor dehub binary's flags a bit --- type: change message: |- refactor dehub binary's flags a bit * Make flag outputting a bit prettier, omitting the flag section in both the usage header and the body if that sub-command has no flags set. * Use a different pattern for opening up the repo, since not all sub-commands will want to do so. change_hash: AK10DRu7LBGpLHRHrQ7TMqgkofz1n1DwK16sg6tOGSq8 credentials: - type: pgp_signature pub_key_id: 95C46FA6A41148AC body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl6IydIACgkQlcRvpqQRSKz6tA/+KmSCy+o2jRb7ZMUz2WSSyERLT9u1hC0+5u5AsJvxhFZSPiEbCrMmM6IvPzmtWpfgTV5ug9C2kbNE5ghp/ANlbmwaTNfAv6bnZjsyLWP3AdrVNNW0w6xkoE3PXUGn06xx1Yst46tnHMKc8GXtFRCLfcgetFl9Fp2w0feIIv1Aw4dP2haCnSXgdHoXloFkVfKoKD1zZo+9uJ+sTOnQhXl0JPaP2eBIgjuhVNGb/RYmhU6pHhh5kObUocCcP8Ywn/xSHOeFV+JXjPSNBnAAHLWuwtwP9bJNUNUoIQKc5WS+oiW9wsGRJZemC4IUnvft73rRrRc6+H3ESFXxP3BRELPbn52uv4BJDMAfDHRuRYbOk1vVP3+KyaUQDJC3g/z6L3xkM1MKbOsjSXAl3XRLR8iwl4GbXqKSFCLHTULzRa4roLF32sef0hQUorQAhDuNpkBBmXjpC3QrCbJgOxZdOZTlqn8IbG7uR/g2uqoBv/P6afe4wOxis4iiMCG8bJd8Zinu6lQ5ULimkf6GWEw96+6bUQnVgEQl0/8nDjzEjWgXNN+OteIS6YVeU16rC/2lrYOobXE+4hP/+RUGG7lH2KBDH1MdtWJf0jSBvtPZ7qeDuMaFZeeFHzWdEkGAkfft1TrWK/1kBhthNAKE6dtazCNzebx+7KDY0fnz3Bs3LCr7PIg= account: mediocregopher --- cmd/dehub/cmd_commit.go | 16 ++++++++++++++-- cmd/dehub/cmd_hook.go | 8 ++++++-- cmd/dehub/cmd_util.go | 28 ++++++++++++++++++++++++++++ cmd/dehub/cmd_verify.go | 7 ++++++- cmd/dehub/dcmd/dcmd.go | 33 +++++++++++++++++++++++++++------ cmd/dehub/main.go | 28 +--------------------------- 6 files changed, 82 insertions(+), 38 deletions(-) create mode 100644 cmd/dehub/cmd_util.go diff --git a/cmd/dehub/cmd_commit.go b/cmd/dehub/cmd_commit.go index 40265b1..53c9aef 100644 --- a/cmd/dehub/cmd_commit.go +++ b/cmd/dehub/cmd_commit.go @@ -15,7 +15,9 @@ func cmdCommit(ctx context.Context, cmd *dcmd.Cmd) { flag := cmd.FlagSet() accountID := flag.String("account-id", "", "Account to accredit commit with") - repo := ctxRepo(ctx) + var repo repo + repo.initFlags(flag) + accreditAndCommit := func(commit dehub.Commit) error { cfg, err := repo.LoadConfig() @@ -61,6 +63,10 @@ func cmdCommit(ctx context.Context, cmd *dcmd.Cmd) { return nil, errors.New("-account-id is required") } + if err := repo.openRepo(); err != nil { + return nil, err + } + var err error if hasStaged, err = repo.HasStagedChanges(); err != nil { return nil, fmt.Errorf("determining if any changes have been staged: %w", err) @@ -177,6 +183,9 @@ func cmdCombine(ctx context.Context, cmd *dcmd.Cmd) { startRev := flag.String("start", "", "Revision of the starting commit to combine") endRev := flag.String("end", "", "Revision of the ending commit to combine") + var repo repo + repo.initFlags(flag) + cmd.Run(func() (context.Context, error) { if *onto == "" || *startRev == "" || @@ -184,7 +193,10 @@ func cmdCombine(ctx context.Context, cmd *dcmd.Cmd) { return nil, errors.New("-onto, -start, and -end are required") } - repo := ctxRepo(ctx) + if err := repo.openRepo(); err != nil { + return nil, err + } + commits, err := repo.GetGitRevisionRange( plumbing.Revision(*startRev), plumbing.Revision(*endRev), diff --git a/cmd/dehub/cmd_hook.go b/cmd/dehub/cmd_hook.go index 1414150..2354eb1 100644 --- a/cmd/dehub/cmd_hook.go +++ b/cmd/dehub/cmd_hook.go @@ -18,13 +18,17 @@ func cmdHook(ctx context.Context, cmd *dcmd.Cmd) { flag := cmd.FlagSet() preRcv := flag.Bool("pre-receive", false, "Use dehub as a server-side pre-receive hook") - cmd.Run(func() (context.Context, error) { + var repo repo + repo.initFlags(flag) + cmd.Run(func() (context.Context, error) { if !*preRcv { return nil, errors.New("must set the hook type") } - repo := ctxRepo(ctx) + if err := repo.openRepo(); err != nil { + return nil, err + } br := bufio.NewReader(os.Stdin) for { diff --git a/cmd/dehub/cmd_util.go b/cmd/dehub/cmd_util.go new file mode 100644 index 0000000..60dc18f --- /dev/null +++ b/cmd/dehub/cmd_util.go @@ -0,0 +1,28 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "dehub.dev/src/dehub.git" +) + +type repo struct { + bare bool + + *dehub.Repo +} + +func (r *repo) initFlags(flag *flag.FlagSet) { + flag.BoolVar(&r.bare, "bare", false, "If set then the repo being opened will be expected to be bare") +} + +func (r *repo) openRepo() error { + var err error + if r.Repo, err = dehub.OpenRepo(".", dehub.OpenBare(r.bare)); err != nil { + wd, _ := os.Getwd() + return fmt.Errorf("opening repo at %q: %w", wd, err) + } + return nil +} diff --git a/cmd/dehub/cmd_verify.go b/cmd/dehub/cmd_verify.go index 702a44e..54e25f1 100644 --- a/cmd/dehub/cmd_verify.go +++ b/cmd/dehub/cmd_verify.go @@ -15,8 +15,13 @@ func cmdVerify(ctx context.Context, cmd *dcmd.Cmd) { rev := flag.String("rev", "HEAD", "Revision of commit to verify") branch := flag.String("branch", "", "Branch that the revision is on. If not given then the currently checked out branch is assumed") + var repo repo + repo.initFlags(flag) + cmd.Run(func() (context.Context, error) { - repo := ctxRepo(ctx) + if err := repo.openRepo(); err != nil { + return nil, err + } gitCommit, err := repo.GetGitRevision(plumbing.Revision(*rev)) if err != nil { diff --git a/cmd/dehub/dcmd/dcmd.go b/cmd/dehub/dcmd/dcmd.go index 7727d34..b78ab18 100644 --- a/cmd/dehub/dcmd/dcmd.go +++ b/cmd/dehub/dcmd/dcmd.go @@ -50,6 +50,14 @@ func (cmd *Cmd) getFlagSet() *flag.FlagSet { return cmd.flagSet } +func (cmd *Cmd) numFlags() int { + var n int + cmd.getFlagSet().VisitAll(func(*flag.Flag) { + n++ + }) + return n +} + // FlagSet returns a flag.Cmd instance on which parameter creation methods can // be called, e.g. String(...) or Int(...). func (cmd *Cmd) FlagSet() *flag.FlagSet { @@ -77,11 +85,19 @@ func (cmd *Cmd) SubCmd(name, descr string, run func(context.Context, *Cmd)) { } func (cmd *Cmd) printUsageHead(subCmdTitle string) { + hasFlags := cmd.numFlags() > 0 + var title string if cmd.parent == nil { - title = fmt.Sprintf("USAGE: %s [flags]", cmd.binary) + title = fmt.Sprintf("USAGE: %s", cmd.binary) + if hasFlags { + title += " [flags]" + } } else { - title = fmt.Sprintf("%s [%s flags]", cmd.name, cmd.name) + title = fmt.Sprintf("%s", cmd.name) + if hasFlags { + title += fmt.Sprintf(" [%s flags]", cmd.name) + } } if subCmdTitle != "" { @@ -92,14 +108,19 @@ func (cmd *Cmd) printUsageHead(subCmdTitle string) { if cmd.parent == nil { fmt.Printf("\n%s\n\n", title) - fmt.Print("### FLAGS ###\n\n") } else { cmd.parent.printUsageHead(title) - fmt.Printf("### %s FLAGS ###\n\n", strings.ToUpper(cmd.name)) } - cmd.getFlagSet().PrintDefaults() - fmt.Print("\n") + if hasFlags { + if cmd.parent == nil { + fmt.Print("### FLAGS ###\n\n") + } else { + fmt.Printf("### %s FLAGS ###\n\n", strings.ToUpper(cmd.name)) + } + cmd.getFlagSet().PrintDefaults() + fmt.Print("\n") + } } // Run performs the comand. It starts by parsing all flags in the Cmd's FlagSet, diff --git a/cmd/dehub/main.go b/cmd/dehub/main.go index 2492383..a837ec2 100644 --- a/cmd/dehub/main.go +++ b/cmd/dehub/main.go @@ -2,44 +2,18 @@ package main import ( "context" - "fmt" - "os" - "dehub.dev/src/dehub.git" "dehub.dev/src/dehub.git/cmd/dehub/dcmd" ) -type cmdCtxKey int - -const ( - cmdCtxKeyRepo cmdCtxKey = iota -) - -func ctxRepo(ctx context.Context) *dehub.Repo { - repo, ok := ctx.Value(cmdCtxKeyRepo).(*dehub.Repo) - if !ok { - panic("repo not initialized on the command context") - } - return repo -} - func main() { cmd := dcmd.New() - flag := cmd.FlagSet() - bare := flag.Bool("bare", false, "If set then dehub will expect to be working with a bare repo") - cmd.SubCmd("commit", "commits staged changes to the head of the current branch", cmdCommit) cmd.SubCmd("verify", "verifies one or more commits as having the proper credentials", cmdVerify) cmd.SubCmd("hook", "use dehub as a git hook", cmdHook) cmd.SubCmd("combine", "Combine multiple change and credential commits into a single commit", cmdCombine) cmd.Run(func() (context.Context, error) { - repo, err := dehub.OpenRepo(".", dehub.OpenBare(*bare)) - if err != nil { - wd, _ := os.Getwd() - return nil, fmt.Errorf("failed to OpenRepo at %q: %w", wd, err) - } - - return context.WithValue(context.Background(), cmdCtxKeyRepo, repo), nil + return context.Background(), nil }) }