diff options
author | Martin Polden <mpolden@mpolden.no> | 2020-05-27 00:05:43 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2020-05-27 00:07:30 +0200 |
commit | dbe32e072cd29689aa3b2b56a459ec8c84da43b4 (patch) | |
tree | 26aebed1abe7f48c3af3e8236732e8f4a9c7dea7 | |
parent | a943e0260fdb1753180c79ef88a6a4c893fd2501 (diff) |
Remove go-flags dependency
-rw-r--r-- | README.md | 26 | ||||
-rw-r--r-- | go.mod | 5 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | main.go | 90 |
4 files changed, 67 insertions, 56 deletions
@@ -39,21 +39,17 @@ Speed up mirroring by running git commands concurrently: ## Usage ``` $ ghm -h -Usage: - ghm [OPTIONS] github-user path - -Application Options: - -g, --git=PATH Path to git executable (default: git) - -q, --quiet Only print errors - -n, --dryrun Print commands that would be run and exit - -p, --protocol=[ssh|https|git] Use the given protocol when mirroring (default: ssh) - -s, --skip-fork Skip forked repositories - -c, --concurrency=COUNT Mirror COUNT repositories concurrently (default: 1) - -Help Options: - -h, --help Show this help message +Usage of ghm: + -a Skip archived repositories + -c int + Number of repositories to mirror concurrently (default 1) + -n Print commands that would be run and exit + -p string + Protocol to use for mirroring [ssh|https|git] (default "ssh") + -q Only print errors + -s Skip forked repositories Arguments: - github-user: GitHub username - path: Path where repositories should be mirrored + <github-user> GitHub username + <path> Path where repositories should be mirrored ``` @@ -2,7 +2,4 @@ module github.com/mpolden/ghm go 1.13 -require ( - github.com/google/go-github/v28 v28.1.1 - github.com/jessevdk/go-flags v1.4.0 -) +require github.com/google/go-github/v28 v28.1.1 @@ -3,8 +3,6 @@ github.com/google/go-github/v28 v28.1.1 h1:kORf5ekX5qwXO2mGzXXOjMe/g6ap8ahVe0sBE github.com/google/go-github/v28 v28.1.1/go.mod h1:bsqJWQX05omyWVmc00nEUql9mhQyv38lDZ8kPZcQVoM= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= -github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "log" "os" @@ -10,30 +11,26 @@ import ( "github.com/google/go-github/v28/github" - flags "github.com/jessevdk/go-flags" "github.com/mpolden/ghm/git" gh "github.com/mpolden/ghm/github" ) -type CLI struct { - Quiet bool `short:"q" long:"quiet" description:"Only print errors"` - Dryrun bool `short:"n" long:"dryrun" description:"Print commands that would be run and exit"` - Protocol string `short:"p" long:"protocol" description:"Use the given protocol when mirroring" choice:"ssh" choice:"https" choice:"git" default:"ssh"` - SkipFork bool `short:"s" long:"skip-fork" description:"Skip forked repositories"` - SkipArchived bool `short:"a" long:"skip-archived" description:"Skip archived repositories"` - Concurrency int `short:"c" long:"concurrency" description:"Mirror COUNT repositories concurrently" value-name:"COUNT" default:"1"` - Args struct { - Username string `description:"GitHub username" positional-arg-name:"github-user"` - Path string `description:"Path where repositories should be mirrored" positional-arg-name:"path"` - } `positional-args:"yes" required:"yes"` - mu sync.Mutex +type syncer struct { + quiet bool + protocol string + dryrun bool + concurrency int + skipFork bool + skipArchived bool + localPath string + mu sync.Mutex } -func (c *CLI) run(cmd *exec.Cmd) error { - if c.Dryrun { +func (s *syncer) run(cmd *exec.Cmd) error { + if s.dryrun { // Prevent overlapping output - c.mu.Lock() - defer c.mu.Unlock() + s.mu.Lock() + defer s.mu.Unlock() fmt.Println(strings.Join(cmd.Args, " ")) return nil } @@ -43,29 +40,29 @@ func (c *CLI) run(cmd *exec.Cmd) error { return nil } -func (c *CLI) sync(g *git.Git, r *github.Repository) error { - repoURL, err := gh.CloneURL(c.Protocol, r) +func (s *syncer) sync(g *git.Git, r *github.Repository) error { + repoURL, err := gh.CloneURL(s.protocol, r) if err != nil { return err } - localDir := git.LocalDir(c.Args.Path, *r.Name) + localDir := git.LocalDir(s.localPath, *r.Name) syncCmd := g.Sync(repoURL, localDir) - return c.run(syncCmd) + return s.run(syncCmd) } -func (c *CLI) syncAll(g *git.Git, repos []*github.Repository) { - sem := make(chan bool, c.Concurrency) +func (s *syncer) syncAll(g *git.Git, repos []*github.Repository) { + sem := make(chan bool, s.concurrency) for _, r := range repos { - if c.SkipFork && *r.Fork { + if s.skipFork && *r.Fork { continue } - if c.SkipArchived && *r.Archived { + if s.skipArchived && *r.Archived { continue } sem <- true go func(r *github.Repository) { defer func() { <-sem }() - if err := c.sync(g, r); err != nil { + if err := s.sync(g, r); err != nil { log.Fatal(err) } }(r) @@ -80,27 +77,50 @@ func main() { log.SetPrefix("ghm: ") log.SetFlags(log.Lshortfile) - var cli CLI - p := flags.NewParser(&cli, flags.HelpFlag|flags.PassDoubleDash) - if _, err := p.Parse(); err != nil { - fmt.Println(err) - os.Exit(1) + flag.Usage = func() { + out := flag.CommandLine.Output() + fmt.Fprintf(out, "Usage of %s:\n", os.Args[0]) + flag.PrintDefaults() + fmt.Fprintln(out, "\nArguments:\n <github-user>\tGitHub username") + fmt.Fprintln(out, " <path>\tPath where repositories should be mirrored") } + quiet := flag.Bool("q", false, "Only print errors") + dryrun := flag.Bool("n", false, "Print commands that would be run and exit") + protocol := flag.String("p", "ssh", "Protocol to use for mirroring [ssh|https|git]") + skipFork := flag.Bool("s", false, "Skip forked repositories") + skipArchived := flag.Bool("a", false, "Skip archived repositories") + concurrency := flag.Int("c", 1, "Number of repositories to mirror concurrently") + flag.Parse() - if cli.Concurrency < 1 { + if *concurrency < 1 { log.Fatal("concurrency level must be positive") } + args := flag.Args() + if len(args) < 2 { + flag.Usage() + } + username := args[0] + path := args[1] + gh := gh.New() - repos, err := gh.ListAllRepositories(cli.Args.Username) + repos, err := gh.ListAllRepositories(username) if err != nil { log.Fatal(err) } - g, err := git.New(!cli.Quiet) + g, err := git.New(!*quiet) if err != nil { log.Fatal(err) } - cli.syncAll(g, repos) + syncer := syncer{ + dryrun: *dryrun, + protocol: *protocol, + skipFork: *skipFork, + skipArchived: *skipArchived, + concurrency: *concurrency, + localPath: path, + } + syncer.syncAll(g, repos) } |