diff --git a/cmd/gr/main.go b/cmd/gr/main.go index bca7be3..b1bbb62 100644 --- a/cmd/gr/main.go +++ b/cmd/gr/main.go @@ -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) } diff --git a/pkg/commands/pull.go b/pkg/commands/pull.go index 32bdc1e..6c5ebf5 100644 --- a/pkg/commands/pull.go +++ b/pkg/commands/pull.go @@ -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}, @@ -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")) diff --git a/pkg/commands/root.go b/pkg/commands/root.go index 81bcf14..97afefd 100644 --- a/pkg/commands/root.go +++ b/pkg/commands/root.go @@ -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() @@ -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()) } diff --git a/pkg/commands/status.go b/pkg/commands/status.go index 83805cb..bb12b0a 100644 --- a/pkg/commands/status.go +++ b/pkg/commands/status.go @@ -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()) }, } diff --git a/pkg/commands/util.go b/pkg/commands/util.go index 8cba638..f873ffe 100644 --- a/pkg/commands/util.go +++ b/pkg/commands/util.go @@ -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" @@ -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+"/") { @@ -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...) } @@ -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 } diff --git a/pkg/commands/version.go b/pkg/commands/version.go index f2108dc..37fc7b9 100644 --- a/pkg/commands/version.go +++ b/pkg/commands/version.go @@ -2,8 +2,8 @@ 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" @@ -11,15 +11,13 @@ import ( const remoteRepositoryName = "sarumaj/gh-gr" -var versionRegex = regexp.MustCompile(`^v(?P(?:\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 @@ -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) @@ -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{}, @@ -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) }