Skip to content

Commit

Permalink
Merge pull request #3 from kazhuravlev/cleanup-code
Browse files Browse the repository at this point in the history
Cleanup code
  • Loading branch information
kazhuravlev authored Nov 29, 2023
2 parents 18c92bd + 21ff38e commit 1d6ca0e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 209 deletions.
25 changes: 25 additions & 0 deletions cmd/gt/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"errors"
"fmt"

repomanager "github.com/kazhuravlev/git-tools/internal/repo-manager"
"github.com/urfave/cli/v3"
)

func withManager(action func(c *cli.Context, manager *repomanager.Manager) error) cli.ActionFunc {
return func(c *cli.Context) error {
repoPath := c.String(flagRepoPath)
if repoPath == "" {
return errors.New("path to repo must be set by flag " + flagRepoPath)
}

manager, err := repomanager.New(repoPath)
if err != nil {
return fmt.Errorf("cannot build repo manager: %w", err)
}

return action(c, manager)
}
}
51 changes: 16 additions & 35 deletions cmd/gt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"context"
"errors"
"fmt"
"os"

repomanager "github.com/kazhuravlev/git-tools/internal/repo-manager"
"github.com/pkg/errors"
"github.com/urfave/cli/v3"
"os"
)

const (
Expand Down Expand Up @@ -44,27 +45,27 @@ func main() {
{
Name: "major",
Aliases: []string{"maj"},
Action: buildTagIncrementor(repomanager.ComponentMajor),
Action: withManager(buildTagIncrementor(repomanager.ComponentMajor)),
Usage: "increment major part of semver",
},
{
Name: "minor",
Aliases: []string{"min"},
Action: buildTagIncrementor(repomanager.ComponentMinor),
Action: withManager(buildTagIncrementor(repomanager.ComponentMinor)),
Usage: "increment minor part of semver",
},
{
Name: "patch",
Aliases: []string{"pat"},
Action: buildTagIncrementor(repomanager.ComponentPatch),
Action: withManager(buildTagIncrementor(repomanager.ComponentPatch)),
Usage: "increment patch part of semver",
},
},
},
{
Name: "last",
Aliases: []string{"l"},
Action: cmdTagGetSemverLast,
Action: withManager(cmdTagGetSemverLast),
Usage: "show last semver tag",
},
},
Expand All @@ -73,7 +74,7 @@ func main() {
Name: "lint",
Aliases: []string{"l"},
Usage: "run linter",
Action: cmdLint,
Action: withManager(cmdLint),
},
},
}
Expand All @@ -83,21 +84,21 @@ func main() {
}
}

func buildTagIncrementor(component repomanager.Component) func(ctx *cli.Context) error {
return func(c *cli.Context) error {
func buildTagIncrementor(component repomanager.Component) func(*cli.Context, *repomanager.Manager) error {
return func(c *cli.Context, m *repomanager.Manager) error {
repoPath := c.String(flagRepoPath)
if repoPath == "" {
return errors.New("path to repo must be set by flag " + flagRepoPath)
}

m, err := repomanager.New(repoPath)
if err != nil {
return errors.Wrap(err, "cannot build repo manager")
return fmt.Errorf("cannot build repo manager: %w", err)
}

oldTag, newTag, err := m.IncrementSemverTag(component)
if err != nil {
return errors.Wrap(err, "cannot increment minor")
return fmt.Errorf("cannot increment minor: %w", err)
}

fmt.Printf(
Expand All @@ -111,40 +112,20 @@ func buildTagIncrementor(component repomanager.Component) func(ctx *cli.Context)
}
}

func cmdTagGetSemverLast(c *cli.Context) error {
repoPath := c.String(flagRepoPath)
if repoPath == "" {
return errors.New("path to repo must be set by flag " + flagRepoPath)
}

m, err := repomanager.New(repoPath)
if err != nil {
return errors.Wrap(err, "cannot build repo manager")
}

func cmdTagGetSemverLast(c *cli.Context, m *repomanager.Manager) error {
maxTag, err := m.GetTagsSemverMax()
if err != nil {
return errors.Wrap(err, "cannot get max tag")
return fmt.Errorf("cannot get max tag: %w", err)
}

fmt.Printf("%s (%s)\n", maxTag.TagName(), maxTag.Ref.Hash())
return nil
}

func cmdLint(c *cli.Context) error {
repoPath := c.String(flagRepoPath)
if repoPath == "" {
return errors.New("path to repo must be set by flag " + flagRepoPath)
}

m, err := repomanager.New(repoPath)
if err != nil {
return errors.Wrap(err, "cannot build repo manager")
}

func cmdLint(c *cli.Context, m *repomanager.Manager) error {
tags, err := m.GetTagsSemverTopN(100)
if err != nil {
return errors.Wrap(err, "cannot last semver tags")
return fmt.Errorf("cannot last semver tags: %w", err)
}

if len(tags) == 0 {
Expand Down
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ go 1.21
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/go-git/go-git/v5 v5.10.1
github.com/pkg/errors v0.9.1
github.com/urfave/cli v1.22.12
github.com/urfave/cli/v2 v2.24.4
github.com/urfave/cli/v3 v3.0.0-alpha4
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230923063757-afb1ddc0824c // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.6 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
Expand All @@ -39,5 +33,4 @@ require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.16.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 1d6ca0e

Please sign in to comment.