diff --git a/cmd/install.go b/cmd/install.go index 53e9a91..c442001 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -39,28 +39,39 @@ To use this version when version management is disabled in the current directory, the cache "default" directory must be in your PATH. If it is not, you will be prompted to add it when installation is complete.`, Run: func(cmd *cobra.Command, args []string) { - err := install() + useLatest, err := cmd.Flags().GetBool("latest") + cobra.CheckErr(err) + + err = install(useLatest) cobra.CheckErr(err) }, } func init() { rootCmd.AddCommand(installCmd) + installCmd.Flags().Bool("latest", false, "Install the latest version") } // install sets the version of the Hugo executable to use when version // management is disabled in the current directory. -func install() error { +func install(useLatest bool) error { asset := newAsset() repo := newRepository() - msg := "Select a version to use when version management is disabled" - err := repo.selectTag(asset, msg) - if err != nil { - return err - } - if asset.tag == "" { - return nil // the user did not select a tag; do nothing + if useLatest { + err := repo.getLatestTag(asset) + if err != nil { + return err + } + } else { + msg := "Select a version to use when version management is disabled" + err := repo.selectTag(asset, msg) + if err != nil { + return err + } + if asset.tag == "" { + return nil // the user did not select a tag; do nothing + } } exists, err := helpers.Exists(filepath.Join(App.CacheDirPath, asset.tag)) @@ -74,7 +85,6 @@ func install() error { return err } } - err = helpers.CopyFile(asset.getExecPath(), filepath.Join(App.CacheDirPath, App.DefaultDirName, asset.execName)) if err != nil { return err diff --git a/cmd/status.go b/cmd/status.go index 340ec44..c3a7db5 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -118,7 +118,7 @@ func status(cmd *cobra.Command) error { fmt.Printf("Would you like to get it now? (Y/n): ") fmt.Scanln(&r) if len(r) == 0 || strings.ToLower(string(r[0])) == "y" { - err = use(true) + err = use(true, false) if err != nil { return err } diff --git a/cmd/use.go b/cmd/use.go index f296017..67ea57e 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -51,7 +51,10 @@ tag to an .hvm file.`, useVersionInDotFile, err := cmd.Flags().GetBool("useVersionInDotFile") cobra.CheckErr(err) - err = use(useVersionInDotFile) + useLatest, err := cmd.Flags().GetBool("latest") + cobra.CheckErr(err) + + err = use(useVersionInDotFile, useLatest) cobra.CheckErr(err) }, } @@ -59,14 +62,16 @@ tag to an .hvm file.`, func init() { rootCmd.AddCommand(useCmd) useCmd.Flags().Bool("useVersionInDotFile", false, "Use the version specified by the "+App.DotFileName+" file\nin the current directory") + useCmd.Flags().Bool("latest", false, "Use the latest version") } // A repository is a GitHub repository. type repository struct { - owner string // account owner of the GitHub repository - name string // name of the GitHub repository without the .git extension - tags []string // repository tags in semver ascending order - client *github.Client // a GitHub API client + owner string // account owner of the GitHub repository + name string // name of the GitHub repository without the .git extension + tags []string // repository tags + latestTag string // latest repository tag + client *github.Client // a GitHub API client } // An asset is a GitHub asset for a given release, operating system, and architecture. @@ -80,7 +85,7 @@ type asset struct { } // use sets the version of the Hugo executable to use in the current directory. -func use(useVersionInDotFile bool) error { +func use(useVersionInDotFile bool, useLatest bool) error { asset := newAsset() repo := newRepository() @@ -99,6 +104,11 @@ func use(useVersionInDotFile bool) error { os.Exit(1) } asset.tag = version + } else if useLatest { + err := repo.getLatestTag(asset) + if err != nil { + return err + } } else { msg := "Select a version to use in the current directory" err := repo.selectTag(asset, msg) @@ -158,9 +168,10 @@ func newRepository() *repository { } r := repository{ - client: client, - name: App.RepositoryName, - owner: App.RepositoryOwner, + client: client, + name: App.RepositoryName, + owner: App.RepositoryOwner, + latestTag: "", } err := r.fetchTags() @@ -236,6 +247,8 @@ func (r *repository) fetchTags() error { } } + r.latestTag = tagNames[0] + if Config.SortAscending { semver.Sort(tagNames) } @@ -244,6 +257,15 @@ func (r *repository) fetchTags() error { return nil } +// getLatestTag returns the most recent tag from repository. +func (r *repository) getLatestTag(a *asset) error { + if "" == r.latestTag { + return fmt.Errorf("no latest release found") + } + a.tag = r.latestTag + return nil +} + // selectTag prompts the user to select a tag from a list of recent tags. func (r *repository) selectTag(a *asset, msg string) error { // List tags.