Skip to content

Commit

Permalink
cmd/all: Fix flag usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring committed Sep 22, 2023
1 parent 2f9dd45 commit c341483
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 116 deletions.
1 change: 1 addition & 0 deletions cmd/hvm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var App application = application{
RepositoryName: "hugo",
RepositoryOwner: "gohugoio",
}

var Config configuration

var (
Expand Down
8 changes: 4 additions & 4 deletions cmd/hvm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/jmooring/hvm/pkg/archive"
"github.com/jmooring/hvm/pkg/helpers"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// installCmd represents the install command
Expand All @@ -47,10 +46,11 @@ you will be prompted to add it when installation is complete.`,
},
}

var useVersionInDotFile bool

func init() {
rootCmd.AddCommand(installCmd)
installCmd.Flags().Bool("useVersionInDotFile", false, "Install the version specified by the "+App.DotFileName+" file\nin the current directory")
viper.BindPFlag("useVersionInDotFile", installCmd.Flags().Lookup("useVersionInDotFile"))
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
Expand All @@ -59,7 +59,7 @@ func install() error {
repo := newRepository()
asset := newAsset()

if viper.GetBool("useVersionInDotFile") {
if useVersionInDotFile {
version, err := getVersionFromDotFile(filepath.Join(App.WorkingDir, App.DotFileName))
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions cmd/hvm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/jmooring/hvm/pkg/helpers"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/mod/semver"
)

Expand All @@ -44,10 +43,11 @@ location. The "default" directory created by the "install" command is excluded.`
},
}

var printExecPath bool

func init() {
rootCmd.AddCommand(statusCmd)
statusCmd.Flags().Bool("printExecPath", false, "Print the path to the Hugo executable if version\nmanagement is enabled in the current directory")
viper.BindPFlag("printExecPath", statusCmd.Flags().Lookup("printExecPath"))
statusCmd.Flags().BoolVar(&printExecPath, "printExecPath", false, "Print the path to the Hugo executable if version\nmanagement is enabled in the current directory")
}

// status displays a list of cached assets, the size of the cache, and the
Expand All @@ -64,7 +64,7 @@ func status() error {
return err
}

if viper.GetBool("printExecPath") {
if printExecPath {
if version == "" {
os.Exit(1)
} else {
Expand Down Expand Up @@ -186,7 +186,7 @@ func getVersionFromDotFile(path string) (string, error) {
return "", err
}
if !exists {
if !viper.GetBool("useVersionInDotFile") {
if !useVersionInDotFile {
return "", fmt.Errorf("the %s file in the current directory contains an invalid version (%s): %s", App.DotFileName, dotHvmContent, theFix)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/clean.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Test
stdin input.txt
exec hvm clean
stdout 'This will delete cached versions of the Hugo executable.\n'
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.'

Expand Down
4 changes: 2 additions & 2 deletions cmd/hvm/testscripts/completion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
[windows] env AppData=config

exec hvm completion
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'
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.'
4 changes: 2 additions & 2 deletions cmd/hvm/testscripts/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
# Test
exec hvm config
stdout 'githubToken ='
stdout 'numTagsToDisplay = 30\n'
stdout 'sortAscending = true\n'
stdout 'numTagsToDisplay = 30'
stdout 'sortAscending = true'
stdout 'Configuration file: '
6 changes: 3 additions & 3 deletions cmd/hvm/testscripts/config_with_env_vars.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ env HVM_SORTASCENDING=true

# Test
exec hvm config
stdout 'githubToken = ''my-token''\n'
stdout 'numTagsToDisplay = 42\n'
stdout 'sortAscending = true\n'
stdout 'githubToken = ''my-token'''
stdout 'numTagsToDisplay = 42'
stdout 'sortAscending = true'
stdout 'Configuration file: '
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/config_with_file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Test
exec hvm config
stdout 'githubToken ='
stdout 'numTagsToDisplay = 42\n'
stdout 'numTagsToDisplay = 42'
stdout 'sortAscending = false'
stdout 'Configuration file: '

Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/disable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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.\n'
stdout 'Version management has been disabled in the current directory.'
! exists '.hvm'

-- input.txt --
Expand Down
4 changes: 2 additions & 2 deletions cmd/hvm/testscripts/gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

# Tests
exec hvm gen
stdout 'Generate various files.\n'
stdout 'Generate various files.'

exec hvm gen alias
stdout 'Generate an alias function for the specified shell.\n'
stdout 'Generate an alias function for the specified shell.'

exec hvm gen alias bash
stdout '# Hugo Version Manager: override path to the hugo executable.'
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

# Test
exec hvm help
stdout 'Hugo Version Manager \(hvm\) is a tool that helps you download, manage, and switch\n'
stdout 'Hugo Version Manager \(hvm\) is a tool that helps you download, manage, and switch'
26 changes: 13 additions & 13 deletions cmd/hvm/testscripts/hvm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

exec hvm

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'
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'
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Test
stdin input.txt
exec hvm install
stdout 'Downloading v.*\.\.\. done.\n'
stdout 'Downloading v.*\.\.\. done.'

# Files
-- input.txt --
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/install_use_dot_file_fail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

# Test
! exec hvm install --useVersionInDotFile
stderr 'Error: the current directory does not contain an \.hvm file\n'
stderr 'Error: the current directory does not contain an \.hvm file'
4 changes: 2 additions & 2 deletions cmd/hvm/testscripts/install_use_dot_file_pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

# Test
exec hvm install --useVersionInDotFile
stdout 'Downloading v0.54.0... done.\n'
stdout 'Installation of v0.54.0 complete.\n'
stdout 'Downloading v0.54.0... done.'
stdout 'Installation of v0.54.0 complete.'

# Files
-- .hvm --
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/remove.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Test
exec hvm remove
stdout 'Default version removed.\n'
stdout 'Default version removed.'

# Files
-- home/Library/Caches/hvm/default/hugo --
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/testscripts/remove_nothing_to_remove.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

# Test
exec hvm remove
stdout 'Nothing to remove.\n'
stdout 'Nothing to remove.'
24 changes: 0 additions & 24 deletions cmd/hvm/testscripts/status-print-execpath.txt

This file was deleted.

11 changes: 0 additions & 11 deletions cmd/hvm/testscripts/status-print-execpath_fail.txt

This file was deleted.

34 changes: 30 additions & 4 deletions cmd/hvm/testscripts/status.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,37 @@
[windows] env LocalAppData=cache
[windows] env AppData=config

# Test
# Test 1: dot file exists, cache not empty
exec hvm status
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'
stdout 'The current directory is configured to use Hugo v0.117.0.'
stdout 'Cached versions of the Hugo executable:'
stdout 'v0.117.0'

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'

# 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'

! exec hvm status --printExecPath

# 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.'

! exec hvm status --printExecPath

# Files
-- .hvm --
Expand Down
12 changes: 0 additions & 12 deletions cmd/hvm/testscripts/status_empty_cache.txt

This file was deleted.

22 changes: 0 additions & 22 deletions cmd/hvm/testscripts/status_no_hvm_file.txt

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/hvm/testscripts/use.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# Test
stdin input.txt
exec hvm use
stdout 'Downloading v.*\.\.\. done.\n'
stdout 'Downloading v.*\.\.\. done.'

exec hvm status
stdout 'The current directory is configured to use Hugo v.*\n'
stdout 'The current directory is configured to use Hugo v.*'

# Files
-- input.txt --
Expand Down

0 comments on commit c341483

Please sign in to comment.