Skip to content

Commit

Permalink
cmd/all: Add --latest flag to the install and use commands
Browse files Browse the repository at this point in the history
  • Loading branch information
irkode authored May 26, 2024
1 parent 2c6028e commit bb2a65d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
30 changes: 20 additions & 10 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
40 changes: 31 additions & 9 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,27 @@ 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)
},
}

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.
Expand All @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -236,6 +247,8 @@ func (r *repository) fetchTags() error {
}
}

r.latestTag = tagNames[0]

if Config.SortAscending {
semver.Sort(tagNames)
}
Expand All @@ -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.
Expand Down

0 comments on commit bb2a65d

Please sign in to comment.