diff --git a/cmd/gitsemver/gitsemver.go b/cmd/gitsemver/gitsemver.go index 52406e3..93b86d9 100644 --- a/cmd/gitsemver/gitsemver.go +++ b/cmd/gitsemver/gitsemver.go @@ -21,10 +21,21 @@ var bumpCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { project := newProjectOrPanic(cmd) versionFilenamesAndKeys := getVersionFilenamesAndKeysOrFail(cmd) + username := getUsernameOrFail(cmd) + password := getPasswordOrFail(cmd) latest := getLatestVersionOrFail(project) next := getNextVersionOrFail(project) - if err := project.Bump(versionFilenamesAndKeys); err != nil { + var auth gitsemver.AuthMethod = nil + + if username != "" && password != "" { + auth = &gitsemver.BasicAuth{ + Username: username, + Password: password, + } + } + + if err := project.Bump(versionFilenamesAndKeys, auth); err != nil { log.Fatalln(err) } @@ -65,6 +76,8 @@ func init() { rootCmd.PersistentFlags().StringP("project", "p", "", "Project") bumpCmd.Flags().StringArrayP("version-file", "f", make([]string, 0), "Specify version files to be updated with the new version in the format `filename:key` (i.e. `package.json:\"version\"`)") + bumpCmd.Flags().StringP("username", "u", "", "Username to use in HTTP basic authentication") + bumpCmd.Flags().StringP("password", "P", "", "Password to use in HTTP basic authentication") } func newProjectOrPanic(cmd *cobra.Command) *gitsemver.Project { @@ -112,3 +125,21 @@ func getVersionFilenamesAndKeysOrFail(cmd *cobra.Command) []string { return versionFilenamesAndKeys } + +func getUsernameOrFail(cmd *cobra.Command) string { + username, err := cmd.Flags().GetString("username") + if err != nil { + log.Fatalln(err) + } + + return username +} + +func getPasswordOrFail(cmd *cobra.Command) string { + password, err := cmd.Flags().GetString("password") + if err != nil { + log.Fatalln(err) + } + + return password +} diff --git a/internal/git/mod.go b/internal/git/mod.go index 9060790..d60a310 100644 --- a/internal/git/mod.go +++ b/internal/git/mod.go @@ -10,12 +10,15 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/storer" + "github.com/go-git/go-git/v5/plumbing/transport" "github.com/go-git/go-git/v5/plumbing/transport/http" ) type Commit = object.Commit type Hash = plumbing.Hash type Repository = git.Repository +type AuthMethod = transport.AuthMethod +type BasicAuth = http.BasicAuth func OpenRepository(path string) (*Repository, error) { return git.PlainOpen(path) @@ -125,7 +128,7 @@ func CreateTag(repo *Repository, name, message string) error { return err } -func PushTagsToOrigin(repo *Repository) error { +func PushTagsToOrigin(repo *Repository, auth AuthMethod) error { // skip pushing if remote doesn't exist if _, err := repo.Remote("origin"); err != nil { return nil @@ -135,10 +138,7 @@ func PushTagsToOrigin(repo *Repository) error { RemoteName: "origin", Progress: os.Stdout, RefSpecs: []config.RefSpec{config.RefSpec("refs/tags/*:refs/tags/*")}, - Auth: &http.BasicAuth{ - Username: "git", - Password: os.Getenv("GITHUB_TOKEN"), - }, + Auth: auth, } if err := repo.Push(pushOpts); err != nil { diff --git a/pkg/gitsemver/project.go b/pkg/gitsemver/project.go index f10ea6c..f97f8ab 100644 --- a/pkg/gitsemver/project.go +++ b/pkg/gitsemver/project.go @@ -13,6 +13,8 @@ import ( type Increment int64 type Change string +type AuthMethod = git.AuthMethod +type BasicAuth = git.BasicAuth const ( Major Increment = 4 @@ -215,7 +217,7 @@ func (p *Project) NextVersionIncrement() (Increment, error) { return increment, nil } -func (p *Project) Bump(versionFilenamesAndKeys []string) error { +func (p *Project) Bump(versionFilenamesAndKeys []string, auth AuthMethod) error { latest, err := p.LatestVersion() if err != nil { return err @@ -251,7 +253,7 @@ func (p *Project) Bump(versionFilenamesAndKeys []string) error { return err } - if err := git.PushTagsToOrigin(p.Repo()); err != nil { + if err := git.PushTagsToOrigin(p.Repo(), auth); err != nil { return err }