Skip to content

Commit

Permalink
change the way, build context is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
sarumaj committed Oct 3, 2023
1 parent 492ce18 commit 566aa2e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 37 deletions.
8 changes: 3 additions & 5 deletions cmd/gr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import (

// Version holds the application version.
// It gets filled automatically at build time.
var Version string
var Version = "v0.0.0"

// BuildDate holds the date and time at which the application was build.
// It gets filled automatically at build time.
var BuildDate string
var BuildDate = "0000-00-00 00:00:00 UTC"

func main() {
commands.Version = Version
commands.BuildDate = BuildDate
commands.Execute()
commands.Execute(Version, BuildDate)
}
11 changes: 8 additions & 3 deletions pkg/commands/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@ func runPull(conf *configfile.Configuration, repo configfile.Repository, status
return
}

updateRepoConfig(conf, repository)
_, err = repository.Remote("upstream")
if err := updateRepoConfig(conf, repository); err != nil {
status.appendError(repo.Directory, err)
return
}

if repo.ParentURL != "" && errors.Is(err, git.ErrRemoteNotFound) {
switch _, err := repository.Remote("upstream"); {

case repo.ParentURL != "" && errors.Is(err, git.ErrRemoteNotFound):
_, err := repository.CreateRemote(&gitconfig.RemoteConfig{
Name: "upstream",
URLs: []string{repo.ParentURL},
Expand All @@ -117,6 +121,7 @@ func runPull(conf *configfile.Configuration, repo configfile.Repository, status
status.appendError(repo.Directory, err)
return
}

}

status.append(repo.Directory, color.GreenString("ok"))
Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var rootCmd = func() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
util.FatalIfError(cmd.Help())
},
Version: Version,
Version: version,
}

flags := cmd.PersistentFlags()
Expand Down Expand Up @@ -83,6 +83,7 @@ func repositoryOperationLoop(fn repositoryOperation, msg string) {
}

// Execute executes the root command.
func Execute() {
func Execute(Version, BuildDate string) {
version, buildDate = Version, BuildDate
util.FatalIfError(rootCmd.Execute())
}
2 changes: 1 addition & 1 deletion pkg/commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var statusCmd = &cobra.Command{
Short: "Show status for all repositories",
Run: func(cmd *cobra.Command, args []string) {
repositoryOperationLoop(runStatus, "Checking")
runLocalStatus()
util.FatalIfError(runLocalStatus())
},
}

Expand Down
39 changes: 23 additions & 16 deletions pkg/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sort"
"strings"

semver "github.com/blang/semver"
color "github.com/fatih/color"
git "github.com/go-git/go-git/v5"
gitconfig "github.com/go-git/go-git/v5/config"
Expand Down Expand Up @@ -60,14 +59,6 @@ func (statuslist *statusList) print() {
Render()
}

func currentVersion() semver.Version {
mmp := versionRegex.ReplaceAllString(Version, "$MMP")
current, err := semver.Parse(mmp)
util.FatalIfError(err)

return current
}

func isRepoDir(path string, repos []configfile.Repository) bool {
for _, r := range repos {
if strings.HasPrefix(r.Directory+"/", path+"/") {
Expand Down Expand Up @@ -161,15 +152,20 @@ func pullSubmodule(submodule *git.Submodule) error {
return nil
}

func runLocalStatus() {
func runLocalStatus() error {
conf := configfile.Load()

files, err := filepath.Glob(conf.BaseDirectory + "/*")
util.FatalIfError(err)
if err != nil {
return err
}

if conf.SubDirectories {
parents, err := filepath.Glob(conf.BaseDirectory + "/*/*")
util.FatalIfError(err)
if err != nil {
return err
}

files = append(files, parents...)
}

Expand All @@ -181,16 +177,27 @@ func runLocalStatus() {
}

status.print()

return nil
}

func updateRepoConfig(conf *configfile.Configuration, repository *git.Repository) {
func updateRepoConfig(conf *configfile.Configuration, repository *git.Repository) error {
repoConf, err := repository.Config()
util.FatalIfError(err)
if err != nil {
return err
}

section := repoConf.Raw.Section("user")
section.SetOption("name", conf.Fullname)
section.SetOption("email", conf.Email)

util.FatalIfError(repoConf.Validate())
util.FatalIfError(repository.Storer.SetConfig(repoConf))
if err := repoConf.Validate(); err != nil {
return err
}

if err := repository.Storer.SetConfig(repoConf); err != nil {
return err
}

return nil
}
21 changes: 11 additions & 10 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ package commands

import (
"fmt"
"regexp"

"github.com/blang/semver"
selfupdate "github.com/rhysd/go-github-selfupdate/selfupdate"
util "github.com/sarumaj/gh-gr/pkg/util"
cobra "github.com/spf13/cobra"
)

const remoteRepositoryName = "sarumaj/gh-gr"

var versionRegex = regexp.MustCompile(`^v(?P<MMP>(?:\d+\.){1,2}(?:\d+))(?:.*)$`)

// Version holds the application version.
// It gets filled automatically at build time.
var Version string
var version string

// BuildDate holds the date and time at which the application was build.
// It gets filled automatically at build time.
var BuildDate string
var buildDate string

var versionCmd = func() *cobra.Command {
var update bool
Expand All @@ -43,7 +41,9 @@ var versionCmd = func() *cobra.Command {
}()

func printVersion() {
current := currentVersion()
current, err := semver.ParseTolerant(version)
util.FatalIfError(err)

latest, found, err := selfupdate.DetectLatest(remoteRepositoryName)
util.FatalIfError(err)

Expand All @@ -54,12 +54,13 @@ func printVersion() {
vSuffix = "(newer version available: " + latest.Version.String() + ")"
}

fmt.Println("gr version:", Version, vSuffix)
fmt.Println("Built at:", BuildDate)
fmt.Println("gr version:", version, vSuffix)
fmt.Println("Built at:", buildDate)
}

func selfUpdate() {
current := currentVersion()
current, err := semver.ParseTolerant(version)
util.FatalIfError(err)

updater, err := selfupdate.NewUpdater(selfupdate.Config{
Validator: &selfupdate.SHA2Validator{},
Expand All @@ -70,7 +71,7 @@ func selfUpdate() {
util.FatalIfError(err)

if latest.Version.LTE(current) {
fmt.Println("You are already using the latest version:", Version)
fmt.Println("You are already using the latest version:", version)
} else {
fmt.Println("Successfully updated to version", latest.Version)
}
Expand Down

0 comments on commit 566aa2e

Please sign in to comment.