diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 70d36dd..fda7275 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -29,4 +29,6 @@ jobs: - name: Install hvm run: go install - name: Test + env: + HVM_GITHUBTOKEN: ${{ secrets.HVM_GITHUBTOKEN }} run: go test ./... diff --git a/cmd/hvm/alias_scripts/bash.sh b/cmd/hvm/alias_scripts/bash.sh index 787c802..878c2cb 100644 --- a/cmd/hvm/alias_scripts/bash.sh +++ b/cmd/hvm/alias_scripts/bash.sh @@ -1,15 +1,19 @@ # Hugo Version Manager: override path to the hugo executable. hugo() { hvm_show_status=true - if hugo_bin=$(hvm status --printExecPath); then + if hugo_bin=$(hvm status --printExecPathCached); then if [ "${hvm_show_status}" == true ]; then >&2 printf "Hugo version management is enabled in this directory.\\n" >&2 printf "Run 'hvm status' for details, or 'hvm disable' to disable.\\n\\n" fi else - if ! hugo_bin=$(which hugo); then - >&2 printf "Command not found.\\n" - return 1 + if hugo_bin=$(hvm status --printExecPath); then + hvm use --useVersionInDotFile + else + if ! hugo_bin=$(which hugo); then + >&2 printf "Command not found.\\n" + return 1 + fi fi fi "${hugo_bin}" "$@" diff --git a/cmd/hvm/alias_scripts/fish.sh b/cmd/hvm/alias_scripts/fish.sh index 787c802..878c2cb 100644 --- a/cmd/hvm/alias_scripts/fish.sh +++ b/cmd/hvm/alias_scripts/fish.sh @@ -1,15 +1,19 @@ # Hugo Version Manager: override path to the hugo executable. hugo() { hvm_show_status=true - if hugo_bin=$(hvm status --printExecPath); then + if hugo_bin=$(hvm status --printExecPathCached); then if [ "${hvm_show_status}" == true ]; then >&2 printf "Hugo version management is enabled in this directory.\\n" >&2 printf "Run 'hvm status' for details, or 'hvm disable' to disable.\\n\\n" fi else - if ! hugo_bin=$(which hugo); then - >&2 printf "Command not found.\\n" - return 1 + if hugo_bin=$(hvm status --printExecPath); then + hvm use --useVersionInDotFile + else + if ! hugo_bin=$(which hugo); then + >&2 printf "Command not found.\\n" + return 1 + fi fi fi "${hugo_bin}" "$@" diff --git a/cmd/hvm/alias_scripts/powershell.ps1 b/cmd/hvm/alias_scripts/powershell.ps1 index 1c9f7dc..b1b3233 100644 --- a/cmd/hvm/alias_scripts/powershell.ps1 +++ b/cmd/hvm/alias_scripts/powershell.ps1 @@ -1,7 +1,7 @@ # Hugo Version Manager: override path to the hugo executable. function Hugo-Override { Set-Variable -Name "hvm_show_status" -Value $true - Set-Variable -Name "hugo_bin" -Value $(hvm status --printExecPath) + Set-Variable -Name "hugo_bin" -Value $(hvm status --printExecPathCached) If ($hugo_bin) { If ($hvm_show_status) { echo "Hugo version management is enabled in this directory." @@ -9,11 +9,16 @@ function Hugo-Override { } & "$hugo_bin" $args } Else { - Set-Variable -Name "hugo_bin" -Value $((gcm hugo.exe).Path 2> $null) + Set-Variable -Name "hugo_bin" -Value $(hvm status --printExecPath) If ($hugo_bin) { - & "$hugo_bin" $args + hvm use --useVersionInDotFile } Else { - echo "Command not found" + Set-Variable -Name "hugo_bin" -Value $((gcm hugo.exe).Path 2> $null) + If ($hugo_bin) { + & "$hugo_bin" $args + } Else { + echo "Command not found" + } } } } diff --git a/cmd/hvm/alias_scripts/zsh.sh b/cmd/hvm/alias_scripts/zsh.sh index 787c802..878c2cb 100644 --- a/cmd/hvm/alias_scripts/zsh.sh +++ b/cmd/hvm/alias_scripts/zsh.sh @@ -1,15 +1,19 @@ # Hugo Version Manager: override path to the hugo executable. hugo() { hvm_show_status=true - if hugo_bin=$(hvm status --printExecPath); then + if hugo_bin=$(hvm status --printExecPathCached); then if [ "${hvm_show_status}" == true ]; then >&2 printf "Hugo version management is enabled in this directory.\\n" >&2 printf "Run 'hvm status' for details, or 'hvm disable' to disable.\\n\\n" fi else - if ! hugo_bin=$(which hugo); then - >&2 printf "Command not found.\\n" - return 1 + if hugo_bin=$(hvm status --printExecPath); then + hvm use --useVersionInDotFile + else + if ! hugo_bin=$(which hugo); then + >&2 printf "Command not found.\\n" + return 1 + fi fi fi "${hugo_bin}" "$@" diff --git a/cmd/hvm/cmd.go b/cmd/hvm/cmd.go index d7b2bee..b32f283 100644 --- a/cmd/hvm/cmd.go +++ b/cmd/hvm/cmd.go @@ -33,10 +33,12 @@ import ( // others depend on the user environment. type application struct { CacheDirPath string // path to the application cache directory - ConfigFilePath string // path to the application configuration + ConfigDirPath string // path to the application configuration directory + ConfigFilePath string // path to the application configuration file DefaultDirName string // name of the "default" directory within the application cache directory DefaultDirPath string // path to the "default" directory within the application cache directory DotFileName string // name of the dot file written to the current directory (e.g., .hvm) + DotFilePath string // path to the dot file Name string // name of the application RepositoryName string // name of the GitHub repository without the .git extension RepositoryOwner string // account owner of the GitHub repository @@ -132,12 +134,12 @@ func initConfig() { // Validate config values. k := "numTagsToDisplay" if viper.GetInt(k) == 0 { - err = fmt.Errorf("configuration: %s must be a non-zero integer: see %s", k, App.ConfigFilePath) + err = fmt.Errorf("configuration: %s must be a non-zero integer: see %s", k, viper.ConfigFileUsed()) cobra.CheckErr(err) } k = "githubToken" if !helpers.IsString(viper.Get(k)) { - err = fmt.Errorf("configuration: %s must be a string: see %s", k, App.ConfigFilePath) + err = fmt.Errorf("configuration: %s must be a string: see %s", k, viper.ConfigFileUsed()) cobra.CheckErr(err) } @@ -152,12 +154,17 @@ func initApp() { userCacheDir, err := os.UserCacheDir() cobra.CheckErr(err) + userConfigDir, err := os.UserConfigDir() + cobra.CheckErr(err) + wd, err := os.Getwd() cobra.CheckErr(err) App.CacheDirPath = filepath.Join(userCacheDir, App.Name) + App.ConfigDirPath = filepath.Join(userConfigDir, App.Name) App.ConfigFilePath = viper.ConfigFileUsed() App.DefaultDirPath = filepath.Join(userCacheDir, App.Name, App.DefaultDirName) + App.DotFilePath = filepath.Join(wd, App.DotFileName) App.WorkingDir = wd err = os.MkdirAll(App.CacheDirPath, 0777) diff --git a/cmd/hvm/cmd_test.go b/cmd/hvm/cmd_test.go index 1f56d47..ec19a75 100644 --- a/cmd/hvm/cmd_test.go +++ b/cmd/hvm/cmd_test.go @@ -31,4 +31,32 @@ func TestCommands(t *testing.T) { return nil }, }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/config", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/remove", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/status", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) + testscript.Run(t, testscript.Params{ + Dir: "testscripts/use", + Setup: func(env *testscript.Env) error { + env.Setenv("HVM_GITHUBTOKEN", os.Getenv("HVM_GITHUBTOKEN")) + return nil + }, + }) } diff --git a/cmd/hvm/disable.go b/cmd/hvm/disable.go index eae134a..765a20d 100644 --- a/cmd/hvm/disable.go +++ b/cmd/hvm/disable.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" "os" - "path/filepath" "github.com/jmooring/hvm/pkg/helpers" "github.com/spf13/cobra" @@ -42,18 +41,13 @@ func init() { // disable disables version management in the current directory. func disable() error { - wd, err := os.Getwd() - if err != nil { - return err - } - - exists, err := helpers.Exists(filepath.Join(wd, App.DotFileName)) + exists, err := helpers.Exists(App.DotFilePath) if err != nil { return err } if exists { - err := os.Remove(filepath.Join(wd, App.DotFileName)) + err := os.Remove(App.DotFilePath) if err != nil { return err } diff --git a/cmd/hvm/install.go b/cmd/hvm/install.go index 1d64a9a..53e9a91 100644 --- a/cmd/hvm/install.go +++ b/cmd/hvm/install.go @@ -18,12 +18,10 @@ package cmd import ( "fmt" - "log" "os" "path/filepath" "slices" - "github.com/jmooring/hvm/pkg/archive" "github.com/jmooring/hvm/pkg/helpers" "github.com/spf13/cobra" ) @@ -46,37 +44,23 @@ you will be prompted to add it when installation is complete.`, }, } -var useVersionInDotFile bool - func init() { rootCmd.AddCommand(installCmd) - installCmd.Flags().BoolVar(&useVersionInDotFile, "useVersionInDotFile", false, "Install the version specified by the "+App.DotFileName+" file\nin the current directory") } // install sets the version of the Hugo executable to use when version // management is disabled in the current directory. func install() error { - repo := newRepository() asset := newAsset() + repo := newRepository() - if useVersionInDotFile { - version, err := getVersionFromDotFile(filepath.Join(App.WorkingDir, App.DotFileName)) - if err != nil { - return err - } - asset.tag = version - if asset.tag == "" { - return fmt.Errorf("the current directory does not contain an " + App.DotFileName + " file") - } - } 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 - } + 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)) @@ -85,33 +69,7 @@ func install() error { } if !exists { - err = repo.fetchURL(asset) - if err != nil { - return err - } - - asset.archiveDirPath, err = os.MkdirTemp("", "") - if err != nil { - return err - } - defer func() { - err := os.RemoveAll(asset.archiveDirPath) - if err != nil { - log.Fatal(err) - } - }() - - err = asset.downloadAsset() - if err != nil { - return err - } - - err = archive.Extract(asset.archiveFilePath, asset.archiveDirPath, true) - if err != nil { - return err - } - - err = helpers.CopyDirectoryContent(asset.archiveDirPath, filepath.Join(App.CacheDirPath, asset.tag)) + err := get(asset, repo) if err != nil { return err } diff --git a/cmd/hvm/reset.go b/cmd/hvm/reset.go index fbff7e9..ece44ef 100644 --- a/cmd/hvm/reset.go +++ b/cmd/hvm/reset.go @@ -60,7 +60,7 @@ func reset() error { if err != nil { return err } - err = os.RemoveAll(App.ConfigFilePath) + err = os.RemoveAll(App.ConfigDirPath) if err != nil { return err } diff --git a/cmd/hvm/status.go b/cmd/hvm/status.go index 627bf57..4383038 100644 --- a/cmd/hvm/status.go +++ b/cmd/hvm/status.go @@ -38,44 +38,103 @@ var statusCmd = &cobra.Command{ Long: `Display a list of cached assets, the size of the cache, and the cache location. The "default" directory created by the "install" command is excluded.`, Run: func(cmd *cobra.Command, args []string) { - err := status() + err := status(cmd) cobra.CheckErr(err) }, } -var printExecPath bool - func init() { rootCmd.AddCommand(statusCmd) - statusCmd.Flags().BoolVar(&printExecPath, "printExecPath", false, "Print the path to the Hugo executable if version\nmanagement is enabled in the current directory") + statusCmd.Flags().Bool("printExecPath", false, `Based on the version specified in the `+App.DotFileName+` file, +print the path to Hugo executable, otherwise +return exit code 1. This path is not verified, and the +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.`) } // status displays a list of cached assets, the size of the cache, and the // cache location. The "default" directory created by the "install" command is // excluded. -func status() error { - wd, err := os.Getwd() +// +//gocyclo:ignore +func status(cmd *cobra.Command) error { + printExecPath, err := cmd.Flags().GetBool("printExecPath") + if err != nil { + return err + } + + printExecPathCached, err := cmd.Flags().GetBool("printExecPathCached") + if err != nil { + return err + } + + version, err := getVersionFromDotFile(App.DotFilePath) if err != nil { return err } - version, err := getVersionFromDotFile(filepath.Join(wd, App.DotFileName)) + execPath := filepath.Join(App.CacheDirPath, version, getExecName()) + execPathExists, err := helpers.Exists(execPath) if err != nil { return err } + // Prints exec path, else exit code 1. if printExecPath { if version == "" { os.Exit(1) } else { - fmt.Println(filepath.Join(App.CacheDirPath, version, getExecName())) + fmt.Println(execPath) + os.Exit(0) } - return nil - } else { + } + + // Prints the exec path if the exec is cached, else exit code 1. + if printExecPathCached { if version == "" { - fmt.Println("Version management is disabled in the current directory.") + os.Exit(1) } else { + if execPathExists { + fmt.Println(execPath) + os.Exit(0) + } else { + os.Exit(1) + } + } + } + + if version == "" { + fmt.Println("Version management is disabled in the current directory.") + } else { + if execPathExists { fmt.Printf("The current directory is configured to use Hugo %s.\n", version) + } else { + var r string + fmt.Printf("The %s file in the current directory refers to a Hugo\n", App.DotFileName) + fmt.Printf("version (%s) that is not cached.\n", version) + fmt.Println() + for { + 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) + if err != nil { + return err + } + fmt.Println() + break + } + if strings.ToLower(string(r[0])) == "n" { + err = disable() + if err != nil { + return err + } + fmt.Println() + break + } + } } } @@ -167,10 +226,9 @@ func getVersionFromDotFile(path string) (string, error) { return "", err } - dotHvmContent := strings.TrimSpace(buf.String()) - - theFix := fmt.Sprintf("run `%[1]s use` to select a version, or `%[1]s disable` to remove the file", App.Name) + theFix := fmt.Sprintf("run \"%[1]s use\" to select a version, or \"%[1]s disable\" to remove the file", App.Name) + dotHvmContent := strings.TrimSpace(buf.String()) if dotHvmContent == "" { return "", fmt.Errorf("the %s file in the current directory is empty: %s", App.DotFileName, theFix) } @@ -181,15 +239,5 @@ func getVersionFromDotFile(path string) (string, error) { return "", fmt.Errorf("the %s file in the current directory has an invalid format: %s", App.DotFileName, theFix) } - exists, err = helpers.Exists(filepath.Join(App.CacheDirPath, dotHvmContent, getExecName())) - if err != nil { - return "", err - } - if !exists { - if !useVersionInDotFile { - return "", fmt.Errorf("the %s file in the current directory contains an invalid version (%s): %s", App.DotFileName, dotHvmContent, theFix) - } - } - return (dotHvmContent), nil } diff --git a/cmd/hvm/testscripts/clean.txtar b/cmd/hvm/testscripts/clean.txtar new file mode 100644 index 0000000..0fb75fe --- /dev/null +++ b/cmd/hvm/testscripts/clean.txtar @@ -0,0 +1,41 @@ +# 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 1: answer no +stdin input-no.txt +exec hvm clean +stdout 'This will delete cached versions of the Hugo executable\.\n' +stdout 'Are you sure you want to clean the cache\? \(y/N\): ' +stdout 'Canceled\.\n' +[darwin] exists home/Library/Caches/hvm/v0.118.0/hugo +[linux] exists cache/hvm/v0.118.0/hugo +[windows] exists cache\\hvm\\v0.118.0\\hugo.exe + + +# Test 2: answer yes +stdin input-yes.txt +exec hvm clean +stdout 'This will delete cached versions of the Hugo executable\.\n' +stdout 'Are you sure you want to clean the cache\? \(y/N\): ' +stdout 'Cache cleaned\.\n' +[darwin] ! exists home/Library/Caches/hvm/v0.118.0 +[linux] ! exists cache/hvm/v0.118.0 +[windows] ! exists cache\\hvm\\v0.118.0 + +# Files +-- input-no.txt -- +n +-- input-yes.txt -- +y +-- home/Library/Caches/hvm/v0.118.0/hugo -- +darwin-exec-bytes +-- cache/hvm/v0.118.0/hugo -- +linux-exec-bytes +-- cache\\hvm\\v0.118.0\\hugo.exe -- +windows-exec-bytes diff --git a/cmd/hvm/testscripts/completion.txt b/cmd/hvm/testscripts/completion.txtar similarity index 91% rename from cmd/hvm/testscripts/completion.txt rename to cmd/hvm/testscripts/completion.txtar index 5fc726f..4b2d7b2 100644 --- a/cmd/hvm/testscripts/completion.txt +++ b/cmd/hvm/testscripts/completion.txtar @@ -8,5 +8,5 @@ [windows] env AppData=config exec hvm completion -stdout 'Generate the autocompletion script for hvm for the specified shell.' -stdout 'See each sub-command''s help for details on how to use the generated script.' +stdout 'Generate the autocompletion script for hvm for the specified shell\.\n' +stdout 'See each sub-command''s help for details on how to use the generated script\.\n' diff --git a/cmd/hvm/testscripts/config.txt b/cmd/hvm/testscripts/config.txt deleted file mode 100644 index 94b0738..0000000 --- a/cmd/hvm/testscripts/config.txt +++ /dev/null @@ -1,15 +0,0 @@ -# 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 config -stdout 'githubToken =' -stdout 'numTagsToDisplay = 30' -stdout 'sortAscending = true' -stdout 'Configuration file: ' diff --git a/cmd/hvm/testscripts/config_bad_value.txt b/cmd/hvm/testscripts/config/config_bad_value.txtar similarity index 85% rename from cmd/hvm/testscripts/config_bad_value.txt rename to cmd/hvm/testscripts/config/config_bad_value.txtar index 4e56699..21d6146 100644 --- a/cmd/hvm/testscripts/config_bad_value.txt +++ b/cmd/hvm/testscripts/config/config_bad_value.txtar @@ -9,12 +9,12 @@ # Test ! exec hvm config -stderr 'Error: configuration: numTagsToDisplay must be a non-zero integer' +stderr 'Error: configuration: numTagsToDisplay must be a non-zero integer: see .+config.toml\n' -# Files for darwin +# Files -- home/Library/Application Support/hvm/config.toml -- numtagstodisplay = "x" - -# Files for linux and windows -- config/hvm/config.toml -- numtagstodisplay = "x" +-- config\\hvm\\config.toml -- +numtagstodisplay = "x" diff --git a/cmd/hvm/testscripts/config/config_with_env_vars.txtar b/cmd/hvm/testscripts/config/config_with_env_vars.txtar new file mode 100644 index 0000000..889760b --- /dev/null +++ b/cmd/hvm/testscripts/config/config_with_env_vars.txtar @@ -0,0 +1,21 @@ +# 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 + +env HVM_GITHUBTOKEN=my-token +env HVM_NUMTAGSTODISPLAY=67 +env HVM_SORTASCENDING=false + +# Test +exec hvm config +stdout 'githubToken = ''my-token''\n' +stdout 'numTagsToDisplay = 67\n' +stdout 'sortAscending = false\n' +[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config\.toml\n' +[linux] stdout 'Configuration file: .+/config/hvm/config\.toml\n' +[windows] stdout 'Configuration file: .+\\config\\hvm\\config\.toml\n' diff --git a/cmd/hvm/testscripts/config/config_with_file.txtar b/cmd/hvm/testscripts/config/config_with_file.txtar new file mode 100644 index 0000000..cc9090f --- /dev/null +++ b/cmd/hvm/testscripts/config/config_with_file.txtar @@ -0,0 +1,31 @@ +# 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 config +stdout 'githubToken = ''.*''\n' +stdout 'numTagsToDisplay = 42\n' +stdout 'sortAscending = false\n' +[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config\.toml\n' +[linux] stdout 'Configuration file: .+/config/hvm/config\.toml\n' +[windows] stdout 'Configuration file: .+\\config\\hvm\\config\.toml\n' + +# Files +-- home/Library/Application Support/hvm/config.toml -- +githubToken = 'my-token' +numtagstodisplay = 42 +sortAscending = false +-- config/hvm/config.toml -- +githubToken = 'my-token' +numtagstodisplay = 42 +sortAscending = false +-- config\\hvm\\config.toml -- +githubToken = 'my-token' +numtagstodisplay = 42 +sortAscending = false diff --git a/cmd/hvm/testscripts/config_with_env_vars.txt b/cmd/hvm/testscripts/config/config_without_file.txtar similarity index 51% rename from cmd/hvm/testscripts/config_with_env_vars.txt rename to cmd/hvm/testscripts/config/config_without_file.txtar index b4d3aca..1a8370a 100644 --- a/cmd/hvm/testscripts/config_with_env_vars.txt +++ b/cmd/hvm/testscripts/config/config_without_file.txtar @@ -7,13 +7,11 @@ [windows] env LocalAppData=cache [windows] env AppData=config -env HVM_GITHUBTOKEN=my-token -env HVM_NUMTAGSTODISPLAY=42 -env HVM_SORTASCENDING=true - # Test exec hvm config -stdout 'githubToken = ''my-token''' -stdout 'numTagsToDisplay = 42' -stdout 'sortAscending = true' -stdout 'Configuration file: ' +stdout 'githubToken = ''.*''\n' +stdout 'numTagsToDisplay = 30\n' +stdout 'sortAscending = true\n' +[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config\.toml\n' +[linux] stdout 'Configuration file: .+/config/hvm/config\.toml\n' +[windows] stdout 'Configuration file: .+\\config\\hvm\\config\.toml\n' diff --git a/cmd/hvm/testscripts/config_with_file.txt b/cmd/hvm/testscripts/config_with_file.txt deleted file mode 100644 index 5765c2f..0000000 --- a/cmd/hvm/testscripts/config_with_file.txt +++ /dev/null @@ -1,27 +0,0 @@ -# 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 config -stdout 'githubToken =' -stdout 'numTagsToDisplay = 42' -stdout 'sortAscending = false' -stdout 'Configuration file: ' - -# Files for darwin --- home/Library/Application Support/hvm/config.toml -- -githubtoken = "my-token" -numtagstodisplay = 42 -sortascending = false - -# Files for linux and windows --- config/hvm/config.toml -- -githubtoken = "my-token" -numtagstodisplay = 42 -sortascending = false diff --git a/cmd/hvm/testscripts/status_err_2.txt b/cmd/hvm/testscripts/disable.txtar similarity index 74% rename from cmd/hvm/testscripts/status_err_2.txt rename to cmd/hvm/testscripts/disable.txtar index 915208c..98b1ec6 100644 --- a/cmd/hvm/testscripts/status_err_2.txt +++ b/cmd/hvm/testscripts/disable.txtar @@ -8,8 +8,9 @@ [windows] env AppData=config # Test -! exec hvm status -stderr 'the \.hvm file in the current directory has an invalid format' +exec hvm disable +stdout 'Version management has been disabled in the current directory\.\n' +! exists '.hvm' -- .hvm -- -zzz +v0.118.2 diff --git a/cmd/hvm/testscripts/gen.txt b/cmd/hvm/testscripts/gen.txtar similarity index 61% rename from cmd/hvm/testscripts/gen.txt rename to cmd/hvm/testscripts/gen.txtar index 5027156..8e0d9f7 100644 --- a/cmd/hvm/testscripts/gen.txt +++ b/cmd/hvm/testscripts/gen.txtar @@ -9,19 +9,28 @@ # Tests exec hvm gen -stdout 'Generate various files.' - -exec hvm gen alias -stdout 'Generate an alias function for the specified shell.' +stdout 'Generate various files\.\n' exec hvm gen alias bash -stdout '# Hugo Version Manager: override path to the hugo executable.' +stdout '# Hugo Version Manager: override path to the hugo executable\.' + +exec hvm gen alias bash --help +stdout 'Generate an alias function for the bash shell\.\n' exec hvm gen alias fish -stdout '# Hugo Version Manager: override path to the hugo executable.' +stdout '# Hugo Version Manager: override path to the hugo executable\.' + +exec hvm gen alias fish --help +stdout 'Generate an alias function for the fish shell\.\n' exec hvm gen alias powershell -stdout '# Hugo Version Manager: override path to the hugo executable.' +stdout '# Hugo Version Manager: override path to the hugo executable\.' + +exec hvm gen alias powershell --help +stdout 'Generate an alias function for Windows PowerShell\.\n' exec hvm gen alias zsh -stdout '# Hugo Version Manager: override path to the hugo executable.' +stdout '# Hugo Version Manager: override path to the hugo executable\.' + +exec hvm gen alias zsh --help +stdout 'Generate an alias function for the zsh shell\.\n' diff --git a/cmd/hvm/testscripts/help.txt b/cmd/hvm/testscripts/help.txtar similarity index 93% rename from cmd/hvm/testscripts/help.txt rename to cmd/hvm/testscripts/help.txtar index 4c09312..f0ea4fb 100644 --- a/cmd/hvm/testscripts/help.txt +++ b/cmd/hvm/testscripts/help.txtar @@ -9,4 +9,4 @@ # Test exec hvm help -stdout 'Hugo Version Manager \(hvm\) is a tool that helps you download, manage, and switch' +stdout 'Hugo Version Manager \(hvm\) is a tool that helps you download, manage, and switch\n' diff --git a/cmd/hvm/testscripts/hvm.txt b/cmd/hvm/testscripts/hvm.txtar similarity index 56% rename from cmd/hvm/testscripts/hvm.txt rename to cmd/hvm/testscripts/hvm.txtar index 395266e..b4dcb54 100644 --- a/cmd/hvm/testscripts/hvm.txt +++ b/cmd/hvm/testscripts/hvm.txtar @@ -9,16 +9,16 @@ exec hvm -stdout 'Hugo Version Manager \(hvm\) is a tool that helps you download, manage, and switch' -stdout 'between different versions of the Hugo static site generator. You can also use' -stdout 'hvm to install Hugo as a standalone application.' -stdout 'clean\s+Clean the cache' -stdout 'completion\s+Generate the autocompletion script for the specified shell' -stdout 'config\s+Display the current configuration' -stdout 'disable\s+Disable version management in the current directory' -stdout 'gen\s+Generate various files' -stdout 'help\s+Help about any command' -stdout 'install\s+Install a default version to use when version management is disabled' -stdout 'status\s+Display the status' -stdout 'use\s+Select a version to use in the current directory' -stdout 'version\s+Display the hvm version' +stdout 'Hugo Version Manager \(hvm\) is a tool that helps you download, manage, and switch\n' +stdout 'between different versions of the Hugo static site generator. You can also use\n' +stdout 'hvm to install Hugo as a standalone application\.\n' +stdout 'clean\s+Clean the cache\n' +stdout 'completion\s+Generate the autocompletion script for the specified shell\n' +stdout 'config\s+Display the current configuration\n' +stdout 'disable\s+Disable version management in the current directory\n' +stdout 'gen\s+Generate various files\n' +stdout 'help\s+Help about any command\n' +stdout 'install\s+Install a default version to use when version management is disabled\n' +stdout 'status\s+Display the status\n' +stdout 'use\s+Select a version to use in the current directory\n' +stdout 'version\s+Display the hvm version\n' diff --git a/cmd/hvm/testscripts/install.txt b/cmd/hvm/testscripts/install.txt deleted file mode 100644 index 50fd2b7..0000000 --- a/cmd/hvm/testscripts/install.txt +++ /dev/null @@ -1,20 +0,0 @@ -# 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 - -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - -# Test -stdin input.txt -exec hvm install -stdout 'Downloading v.*\.\.\. done.' - -# Files --- input.txt -- -1 diff --git a/cmd/hvm/testscripts/install.txtar b/cmd/hvm/testscripts/install.txtar new file mode 100644 index 0000000..0cd359e --- /dev/null +++ b/cmd/hvm/testscripts/install.txtar @@ -0,0 +1,30 @@ +# 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 +env HVM_NUMTAGSTODISPLAY=-1 +stdin input.txt +exec hvm install +stdout 'Select a version to use when version management is disabled: ' +stdout 'Downloading v0\.54\.0\.\.\. done\.\n' +stdout 'Installation of v0\.54\.0 complete\.\n' +[darwin] stdout 'Please add home/Library/Caches/hvm/default to the PATH environment variable\.\n' +[linux] stdout 'Please add cache/hvm/default to the PATH environment variable\.\n' +[windows] stdout 'Please add cache\\hvm\\default to the PATH environment variable\.\n' +stdout 'Open a new terminal after making the change\.\n' +[darwin] exists 'home/Library/Caches/hvm/v0.54.0/hugo' +[darwin] exists 'home/Library/Caches/hvm/default/hugo' +[linux] exists 'cache/hvm/v0.54.0/hugo' +[linux] exists 'cache/hvm/default/hugo' +[windows] exists 'cache\\hvm\\v0.54.0\\hugo.exe' +[windows] exists 'cache\\hvm\\default\\hugo.exe' + +# Files +-- input.txt -- +1 diff --git a/cmd/hvm/testscripts/remove.txt b/cmd/hvm/testscripts/remove/remove.txtar similarity index 76% rename from cmd/hvm/testscripts/remove.txt rename to cmd/hvm/testscripts/remove/remove.txtar index 1e6077d..3bf8667 100644 --- a/cmd/hvm/testscripts/remove.txt +++ b/cmd/hvm/testscripts/remove/remove.txtar @@ -9,7 +9,10 @@ # Test exec hvm remove -stdout 'Default version removed.' +stdout 'Default version removed\.\n' +[darwin] ! exists 'Library/Caches/hvm/default' +[linux] ! exists 'cache/hvm/default' +[windows] ! exists 'cache/hvm/default' # Files -- home/Library/Caches/hvm/default/hugo -- diff --git a/cmd/hvm/testscripts/remove_nothing_to_remove.txt b/cmd/hvm/testscripts/remove/remove_nothing_to_remove.txtar similarity index 91% rename from cmd/hvm/testscripts/remove_nothing_to_remove.txt rename to cmd/hvm/testscripts/remove/remove_nothing_to_remove.txtar index 3fe6e10..e28a50d 100644 --- a/cmd/hvm/testscripts/remove_nothing_to_remove.txt +++ b/cmd/hvm/testscripts/remove/remove_nothing_to_remove.txtar @@ -9,4 +9,4 @@ # Test exec hvm remove -stdout 'Nothing to remove.' +stdout 'Nothing to remove\.\n' diff --git a/cmd/hvm/testscripts/reset.txt b/cmd/hvm/testscripts/reset.txt deleted file mode 100644 index 4b294db..0000000 --- a/cmd/hvm/testscripts/reset.txt +++ /dev/null @@ -1,25 +0,0 @@ -# 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 - -[darwin] skip -[windows] skip - -# Test -stdin input.txt -exec hvm reset -! exists cache/v0.118.0/hugo -! exists config/hvm/config.toml - -# Files --- input.txt -- -y --- cache/hvm/v0.118.0/hugo -- -linux-exec-bytes --- config/hvm/config.toml -- -numtagstodisplay = 42 diff --git a/cmd/hvm/testscripts/reset.txtar b/cmd/hvm/testscripts/reset.txtar new file mode 100644 index 0000000..f09da72 --- /dev/null +++ b/cmd/hvm/testscripts/reset.txtar @@ -0,0 +1,48 @@ +# 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 1: answer no +stdin input-no.txt +exec hvm reset +stdout 'This will reset the configuration and remove the cache directory\.\n' +stdout 'Are you sure you want to do this\? \(y/N\): ' +stdout 'Canceled\.\n' + +# Test 2: answer yes +stdin input-yes.txt +exec hvm reset +stdout 'This will reset the configuration and remove the cache directory\.\n' +stdout 'Are you sure you want to do this\? \(y/N\): ' +stdout 'Version management has been disabled in the current directory\.\n' +[darwin] ! exists home/Library/Caches/hvm +[darwin] ! exists home/Library/Application Support/hvm" +[linux] ! exists cache/hvm +[linux] ! exists config/hvm +[windows] ! exists cache\\hvm +[windows] ! exists config\\hvm + +# Files +-- input-no.txt -- +n +-- input-yes.txt -- +y +-- home/Library/Caches/hvm/v0.118.0/hugo -- +darwin-exec-bytes +-- home/Library/Application Support/hvm/config.toml -- +numtagstodisplay = 42 + +-- cache/hvm/v0.118.0/hugo -- +linux-exec-bytes +-- config/hvm/config.toml -- +numtagstodisplay = 42 + +-- cache\\hvm\\v0.118.0\\hugo.exe -- +windows-exec-bytes +-- config\\hvm\\config.toml -- +numtagstodisplay = 42 diff --git a/cmd/hvm/testscripts/status.txt b/cmd/hvm/testscripts/status/status.txtar similarity index 67% rename from cmd/hvm/testscripts/status.txt rename to cmd/hvm/testscripts/status/status.txtar index 163e12e..40b8953 100644 --- a/cmd/hvm/testscripts/status.txt +++ b/cmd/hvm/testscripts/status/status.txtar @@ -9,35 +9,33 @@ # Test 1: dot file exists, cache not empty exec hvm status -stdout 'The current directory is configured to use Hugo v0.117.0.' -stdout 'Cached versions of the Hugo executable:' -stdout 'v0.117.0' - +stdout 'The current directory is configured to use Hugo v0\.117\.0\.\n' +stdout 'Cached versions of the Hugo executable:\n' +stdout 'v0\.117\.0\n' exec hvm status --printExecPath [darwin] stdout 'home/Library/Caches/hvm/v0.117.0/hugo' [linux] stdout 'cache/hvm/v0.117.0/hugo' [windows] stdout 'cache\\hvm\\v0.117.0\\hugo.exe' +exec hvm status --printExecPathCached # Test 2: dot file does not exist, cache not empty rm .hvm - exec hvm status -stdout 'Version management is disabled in the current directory.' -stdout 'Cached versions of the Hugo executable:' -stdout 'v0.117.0' - +stdout 'Version management is disabled in the current directory\.\n' +stdout 'Cached versions of the Hugo executable:\n' +stdout 'v0\.117\.0\n' ! exec hvm status --printExecPath +! exec hvm status --printExecPathCached # Test 3: dot file does not exist, cache empty rm home/Library/Caches/hvm/v0.117.0 rm cache/hvm/v0.117.0 rm cache/hvm/v0.117.0 - exec hvm status -stdout 'Version management is disabled in the current directory.' -stdout 'The cache is empty.' - +stdout 'Version management is disabled in the current directory\.\n' +stdout 'The cache is empty\.\n' ! exec hvm status --printExecPath +! exec hvm status --printExecPathCached # Files -- .hvm -- diff --git a/cmd/hvm/testscripts/disable.txt b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_no.txtar similarity index 53% rename from cmd/hvm/testscripts/disable.txt rename to cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_no.txtar index 51c8623..b3f8855 100644 --- a/cmd/hvm/testscripts/disable.txt +++ b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_no.txtar @@ -7,18 +7,19 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test stdin input.txt -exec hvm use exec hvm status -stdout 'The current directory is configured to use Hugo' -stdout 'Cached versions of the Hugo executable' -exec hvm disable -stdout 'Version management has been disabled in the current directory.' -! exists '.hvm' +stdout 'The \.hvm file in the current directory refers to a Hugo\n' +stdout 'version \(v0\.118\.2\) that is not cached\.\n' +stdout 'Would you like to get it now\? \(Y/n\): ' +stdout 'Version management has been disabled in the current directory\.\n' +[darwin] ! exists 'Library/Caches/hvm/default/v0.118.2/hugo' +[linux] ! exists 'cache/hvm/default/v0.118.2/hugo' +[windows] ! exists 'cache\\hvm\\default\\v0.118.2\\hugo.exe' +# Files -- input.txt -- -1 +n +-- .hvm -- +v0.118.2 diff --git a/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_yes.txtar b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_yes.txtar new file mode 100644 index 0000000..8ddaf8c --- /dev/null +++ b/cmd/hvm/testscripts/status/status_dot_file_version_not_cached_answer_yes.txtar @@ -0,0 +1,25 @@ +# 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 +stdin input.txt +exec hvm status +stdout 'The \.hvm file in the current directory refers to a Hugo\n' +stdout 'version \(v0\.118\.2\) that is not cached\.\n' +stdout 'Would you like to get it now\? \(Y/n\): ' +stdout 'Downloading v0\.118\.2\.\.\. done\.\n' +[darwin] exists 'home/Library/Caches/hvm/v0.118.2/hugo' +[linux] exists 'cache/hvm/v0.118.2/hugo' +[windows] exists 'cache\\hvm\\v0.118.2\\hugo.exe' + +# Files +-- input.txt -- +y +-- .hvm -- +v0.118.2 diff --git a/cmd/hvm/testscripts/status_err_1.txt b/cmd/hvm/testscripts/status/status_err_1.txtar similarity index 71% rename from cmd/hvm/testscripts/status_err_1.txt rename to cmd/hvm/testscripts/status/status_err_1.txtar index 2eaef61..329982c 100644 --- a/cmd/hvm/testscripts/status_err_1.txt +++ b/cmd/hvm/testscripts/status/status_err_1.txtar @@ -9,6 +9,6 @@ # Test ! exec hvm status -stderr 'the \.hvm file in the current directory is empty' +stderr 'Error: the \.hvm file in the current directory is empty: run "hvm use" to select a version, or "hvm disable" to remove the file\n' -- .hvm -- diff --git a/cmd/hvm/testscripts/status_err_3.txt b/cmd/hvm/testscripts/status/status_err_2.txtar similarity index 83% rename from cmd/hvm/testscripts/status_err_3.txt rename to cmd/hvm/testscripts/status/status_err_2.txtar index 04404ea..9928af1 100644 --- a/cmd/hvm/testscripts/status_err_3.txt +++ b/cmd/hvm/testscripts/status/status_err_2.txtar @@ -9,7 +9,7 @@ # Test ! exec hvm status -stderr 'the \.hvm file in the current directory has an invalid format' +stderr 'the \.hvm file in the current directory has an invalid format: run "hvm use" to select a version, or "hvm disable" to remove the file\n' -- .hvm -- xxx diff --git a/cmd/hvm/testscripts/clean.txt b/cmd/hvm/testscripts/status/status_print_exec_path_cached.txtar similarity index 61% rename from cmd/hvm/testscripts/clean.txt rename to cmd/hvm/testscripts/status/status_print_exec_path_cached.txtar index 6db1433..865c5c3 100644 --- a/cmd/hvm/testscripts/clean.txt +++ b/cmd/hvm/testscripts/status/status_print_exec_path_cached.txtar @@ -7,20 +7,18 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# Test 1: cache is not empty -stdin input.txt -exec hvm clean -stdout 'This will delete cached versions of the Hugo executable.' -stdout 'Are you sure you want to clean the cache\? \(y/N\): ' -stdout 'Cache cleaned.' +# Test 1: dot file exists, exec exists +exec hvm status --printExecPathCached -# Test 2: cache is empty -exec hvm clean -stdout 'The cache is already empty.' +# Test 2: dot file exists, exec does not exist +rm home/Library/Caches/hvm/v0.117.0 +rm cache/hvm/v0.117.0 +rm cache/hvm/v0.117.0 +! exec hvm status --printExecPathCached # Files --- input.txt -- -y +-- .hvm -- +v0.117.0 -- home/Library/Caches/hvm/v0.117.0/hugo -- darwin-exec-bytes -- cache/hvm/v0.117.0/hugo -- diff --git a/cmd/hvm/testscripts/use.txt b/cmd/hvm/testscripts/use/use.txtar similarity index 53% rename from cmd/hvm/testscripts/use.txt rename to cmd/hvm/testscripts/use/use.txtar index 59e6758..02fcfb0 100644 --- a/cmd/hvm/testscripts/use.txt +++ b/cmd/hvm/testscripts/use/use.txtar @@ -7,16 +7,17 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test +env HVM_NUMTAGSTODISPLAY=-1 stdin input.txt exec hvm use -stdout 'Downloading v.*\.\.\. done.' - +stdout 'Select a version to use in the current directory: ' +stdout 'Downloading v0\.54\.0\.\.\.\ done.' exec hvm status -stdout 'The current directory is configured to use Hugo v.*' +stdout 'The current directory is configured to use Hugo v0\.54\.0\.\n' +[darwin] exists 'home/Library/Caches/hvm/v0.54.0/hugo' +[linux] exists 'cache/hvm/v0.54.0/hugo' +[windows] exists 'cache\\hvm\\v0.54.0\\hugo.exe' # Files -- input.txt -- diff --git a/cmd/hvm/testscripts/use_check_sort_order.txt b/cmd/hvm/testscripts/use/use_check_sort_order.txtar similarity index 91% rename from cmd/hvm/testscripts/use_check_sort_order.txt rename to cmd/hvm/testscripts/use/use_check_sort_order.txtar index 64dafa4..238c1d5 100644 --- a/cmd/hvm/testscripts/use_check_sort_order.txt +++ b/cmd/hvm/testscripts/use/use_check_sort_order.txtar @@ -7,9 +7,6 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test 1 env HVM_NUMTAGSTODISPLAY=-1 env HVM_SORTASCENDING=true diff --git a/cmd/hvm/testscripts/use_invalid_selection.txt b/cmd/hvm/testscripts/use/use_invalid_selection.txtar similarity index 87% rename from cmd/hvm/testscripts/use_invalid_selection.txt rename to cmd/hvm/testscripts/use/use_invalid_selection.txtar index eb7af24..0c59cbb 100644 --- a/cmd/hvm/testscripts/use_invalid_selection.txt +++ b/cmd/hvm/testscripts/use/use_invalid_selection.txtar @@ -7,9 +7,6 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test env HVM_NUMTAGSTODISPLAY=12 stdin input.txt diff --git a/cmd/hvm/testscripts/install_use_dot_file_fail.txt b/cmd/hvm/testscripts/use/use_use_dot_file_fail.txtar similarity index 77% rename from cmd/hvm/testscripts/install_use_dot_file_fail.txt rename to cmd/hvm/testscripts/use/use_use_dot_file_fail.txtar index e37607a..0e35fa4 100644 --- a/cmd/hvm/testscripts/install_use_dot_file_fail.txt +++ b/cmd/hvm/testscripts/use/use_use_dot_file_fail.txtar @@ -7,9 +7,6 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test -! exec hvm install --useVersionInDotFile -stderr 'Error: the current directory does not contain an \.hvm file' +! exec hvm use --useVersionInDotFile +stderr 'Error: the current directory does not contain an \.hvm file\n' diff --git a/cmd/hvm/testscripts/install_use_dot_file_pass.txt b/cmd/hvm/testscripts/use/use_use_dot_file_pass.txtar similarity index 59% rename from cmd/hvm/testscripts/install_use_dot_file_pass.txt rename to cmd/hvm/testscripts/use/use_use_dot_file_pass.txtar index 1b59c19..c05fca6 100644 --- a/cmd/hvm/testscripts/install_use_dot_file_pass.txt +++ b/cmd/hvm/testscripts/use/use_use_dot_file_pass.txtar @@ -7,14 +7,13 @@ [windows] env LocalAppData=cache [windows] env AppData=config -# https://github.com/actions/runner-images/issues/602 -[darwin] skip - # Test -exec hvm install --useVersionInDotFile -stdout 'Downloading v0.54.0... done.' -stdout 'Installation of v0.54.0 complete.' +exec hvm use --useVersionInDotFile +stdout 'Downloading v0\.118\.2\.\.\. done\.\n' +[darwin] exists 'home/Library/Caches/hvm/v0.118.2/hugo' +[linux] exists 'cache/hvm/v0.118.2/hugo' +[windows] exists 'cache\\hvm\\v0.118.2\\hugo.exe' # Files -- .hvm -- -v0.54.0 +v0.118.2 diff --git a/cmd/hvm/testscripts/version.txt b/cmd/hvm/testscripts/version.txtar similarity index 100% rename from cmd/hvm/testscripts/version.txt rename to cmd/hvm/testscripts/version.txtar diff --git a/cmd/hvm/use.go b/cmd/hvm/use.go index 549c2ea..4eca9b4 100644 --- a/cmd/hvm/use.go +++ b/cmd/hvm/use.go @@ -47,13 +47,18 @@ to use in the current directory. It then downloads, extracts, and caches the release asset for your operating system and architecture and writes the version tag to an .hvm file.`, Run: func(cmd *cobra.Command, args []string) { - err := use() + useVersionInDotFile, err := cmd.Flags().GetBool("useVersionInDotFile") + cobra.CheckErr(err) + + err = use(useVersionInDotFile) 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") + } // A repository is a GitHub repository. @@ -75,17 +80,29 @@ type asset struct { } // use sets the version of the Hugo executable to use in the current directory. -func use() error { - repo := newRepository() +func use(useVersionInDotFile bool) error { asset := newAsset() + repo := newRepository() - msg := "Select a version to use in the current directory" - 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 useVersionInDotFile { + version, err := getVersionFromDotFile(App.DotFilePath) + if err != nil { + return err + } + if version == "" { + fmt.Fprintln(os.Stderr, "Error: the current directory does not contain an "+App.DotFileName+" file") + os.Exit(1) + } + asset.tag = version + } else { + msg := "Select a version to use in the current directory" + 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)) @@ -96,33 +113,7 @@ func use() error { if exists { fmt.Printf("Using %s from cache.\n", asset.tag) } else { - err = repo.fetchURL(asset) - if err != nil { - return err - } - - asset.archiveDirPath, err = os.MkdirTemp("", "") - if err != nil { - return err - } - defer func() { - err := os.RemoveAll(asset.archiveDirPath) - if err != nil { - log.Fatal(err) - } - }() - - err = asset.downloadAsset() - if err != nil { - return err - } - - err = archive.Extract(asset.archiveFilePath, asset.archiveDirPath, true) - if err != nil { - return err - } - - err = helpers.CopyDirectoryContent(asset.archiveDirPath, filepath.Join(App.CacheDirPath, asset.tag)) + err := get(asset, repo) if err != nil { return err } @@ -136,6 +127,15 @@ func use() error { return nil } +// newAsset creates a new asset object, returning a pointer to same. +func newAsset() *asset { + a := asset{ + execName: getExecName(), + } + + return &a +} + // newRepository creates a new repository object, returning a pointer to same. func newRepository() *repository { var client *github.Client @@ -161,13 +161,40 @@ func newRepository() *repository { return &r } -// newAsset creates a new asset object, returning a pointer to same. -func newAsset() *asset { - a := asset{ - execName: getExecName(), +// get downloads and extracts the release asset. +func get(asset *asset, repo *repository) error { + err := repo.fetchDownloadURL(asset) + if err != nil { + return err } - return &a + asset.archiveDirPath, err = os.MkdirTemp("", "") + if err != nil { + return err + } + defer func() { + err := os.RemoveAll(asset.archiveDirPath) + if err != nil { + log.Fatal(err) + } + }() + + err = asset.downloadAsset() + if err != nil { + return err + } + + err = archive.Extract(asset.archiveFilePath, asset.archiveDirPath, true) + if err != nil { + return err + } + + err = helpers.CopyDirectoryContent(asset.archiveDirPath, filepath.Join(App.CacheDirPath, asset.tag)) + if err != nil { + return err + } + + return nil } // fetchTags fetches tags associated with recent releases. @@ -275,8 +302,8 @@ func (r *repository) selectTag(a *asset, msg string) error { return nil } -// fetchURL fetches the download URL for the user-selected tag. -func (r *repository) fetchURL(a *asset) error { +// fetchDownloadURL fetches the download URL for the user-selected tag. +func (r *repository) fetchDownloadURL(a *asset) error { // The archive file name was different with v0.102.3 and earlier. version := a.tag[1:] pattern := "" @@ -388,12 +415,7 @@ func (a *asset) getExecPath() string { // createDotFile creates an application dot file in the current directory. func (a *asset) createDotFile() error { - wd, err := os.Getwd() - if err != nil { - return err - } - - err = os.WriteFile(filepath.Join(wd, App.DotFileName), []byte(a.tag), 0644) + err := os.WriteFile(App.DotFilePath, []byte(a.tag), 0644) if err != nil { return err }