Skip to content

Commit

Permalink
cmd/all: Accept version argument for the install and use commands
Browse files Browse the repository at this point in the history
Closes #106
  • Loading branch information
irkode authored Dec 13, 2024
1 parent 1689fd6 commit afcd4e6
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 53 deletions.
19 changes: 18 additions & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,52 @@ import (
"github.com/rogpeppe/go-internal/testscript"
)

func TestCommands(t *testing.T) {
func TestCommand(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testscripts",
Setup: func(env *testscript.Env) error {
env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN"))
return nil
},
})
}
func TestCommandConfig(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testscripts/config",
Setup: func(env *testscript.Env) error {
env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN"))
return nil
},
})
}
func TestCommandInstall(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testscripts/install",
Setup: func(env *testscript.Env) error {
env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN"))
return nil
},
})
}
func TestCommandRemove(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testscripts/remove",
Setup: func(env *testscript.Env) error {
env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN"))
return nil
},
})
}
func TestCommandStatus(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testscripts/status",
Setup: func(env *testscript.Env) error {
env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN"))
return nil
},
})
}
func TestCommandUse(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testscripts/use",
Setup: func(env *testscript.Env) error {
Expand Down
31 changes: 17 additions & 14 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install",
Use: "install [flags] [version]",
Short: "Install a default version to use when version management is disabled",
Long: `Displays a list of recent Hugo releases, prompting you to select a version
to use when version management is disabled in the current directory. It then
Expand All @@ -37,33 +37,30 @@ architecture, placing a copy in the cache "default" directory.
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) {
useLatest, err := cmd.Flags().GetBool("latest")
cobra.CheckErr(err)
you will be prompted to add it when installation is complete.
err = install(useLatest)
To bypass the selection screen provide a version argument to the command.`,
Run: func(cmd *cobra.Command, args []string) {
version := ""
if len(args) > 0 {
version = args[0]
}
err := install(version)
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(useLatest bool) error {
func install(version string) error {
asset := newAsset()
repo := newRepository()

if useLatest {
err := repo.getLatestTag(asset)
if err != nil {
return err
}
} else {
if version == "" {
msg := "Select a version to use when version management is disabled"
err := repo.selectTag(asset, msg)
if err != nil {
Expand All @@ -72,6 +69,11 @@ func install(useLatest bool) error {
if asset.tag == "" {
return nil // the user did not select a tag; do nothing
}
} else {
err := repo.getTagFromString(asset, version)
if err != nil {
return err
}
}

exists, err := helpers.Exists(filepath.Join(App.CacheDirPath, asset.tag))
Expand All @@ -85,6 +87,7 @@ func install(useLatest bool) error {
return err
}
}

err = helpers.CopyFile(asset.getExecPath(), filepath.Join(App.CacheDirPath, App.DefaultDirName, asset.execName))
if err != nil {
return err
Expand Down
11 changes: 7 additions & 4 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ executable may not exist.`)
statusCmd.Flags().Bool("printExecPathCached", false, `Based on the version specified in the `+App.DotFileName+` file,
print the path to Hugo executable if cached, otherwise
return exit code 1.`)
statusCmd.MarkFlagsMutuallyExclusive("printExecPath", "printExecPathCached")
}

// status displays a list of cached assets, the size of the cache, and the
Expand All @@ -69,7 +70,7 @@ func status(cmd *cobra.Command) error {
return err
}

version, err := getVersionFromDotFile(App.DotFilePath)
version, err := getVersionFromDotFile()
if err != nil {
return err
}
Expand Down Expand Up @@ -118,9 +119,10 @@ 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, false)
err = use(version)
if err != nil {
return err
theFix := fmt.Sprintf("run \"%[1]s use\" to select a version, or \"%[1]s disable\" to remove the file", App.Name)
return fmt.Errorf("the version specified in the %s file (%s) is not available in the repository: %s", App.DotFileName, version, theFix)
}
fmt.Println()
break
Expand Down Expand Up @@ -203,7 +205,8 @@ func getCacheSize() (int64, error) {

// getVersionFromDotFile returns the semver string from the app dot file in the
// current directory, or an empty string if the file does not exist.
func getVersionFromDotFile(path string) (string, error) {
func getVersionFromDotFile() (string, error) {
path := App.DotFilePath
exists, err := helpers.Exists(path)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion cmd/testscripts/install/install_latest.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
[windows] env AppData=config

# Test
exec hvm install --latest
exec hvm install latest
stdout 'Downloading v.* done.\n'
stdout 'Installation of v.* complete.\n'
12 changes: 12 additions & 0 deletions cmd/testscripts/install/install_tag_fail_invalid.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
! exec hvm install x1
stderr 'Error: invalid tag: x1\n'
12 changes: 12 additions & 0 deletions cmd/testscripts/install/install_tag_fail_not-found.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
! exec hvm install v0.1.2
stderr 'Error: tag "v0\.1\.2" not found in repository\n'
13 changes: 13 additions & 0 deletions cmd/testscripts/install/install_tag_pass.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
exec hvm install v0.138.0
stdout 'Downloading v.* done.\n'
stdout 'Installation of v.* complete.\n'
12 changes: 12 additions & 0 deletions cmd/testscripts/status/status_fail_printPath_exclusive.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
! exec hvm status --printExecPath --printExecPathCached
stderr 'Error: if any flags in the group \[printExecPath printExecPathCached\] are set none of the others can be; \[printExecPath printExecPathCached\] were all set\n'
2 changes: 1 addition & 1 deletion cmd/testscripts/use/use_latest.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
[windows] env AppData=config

# Test
exec hvm use --latest
exec hvm use latest
stdout 'Downloading v.* done.\n'
12 changes: 12 additions & 0 deletions cmd/testscripts/use/use_tag_fail_invalid.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
! exec hvm use x1
stderr 'Error: invalid tag: x1\n'
12 changes: 12 additions & 0 deletions cmd/testscripts/use/use_tag_fail_not-found.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
! exec hvm use v0.1.2
stderr 'Error: tag "v0\.1\.2" not found in repository\n'
12 changes: 12 additions & 0 deletions cmd/testscripts/use/use_tag_pass.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# User cache and config dirs (we use os.UserCacheDir and os.UserCongfigDir)
[darwin] env HOME=home
[darwin] mkdir "$HOME/Library/Caches"
[darwin] mkdir "$HOME/Library/Application Support"
[linux] env XDG_CACHE_HOME=cache
[linux] env XDG_CONFIG_HOME=config
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
exec hvm use v0.138.0
stdout 'Downloading v.* done.\n'
Loading

0 comments on commit afcd4e6

Please sign in to comment.