Skip to content

Commit

Permalink
cmd/status: Prompt to install if dot file version not cached
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
jmooring committed Sep 23, 2023
1 parent 22a6fd4 commit 2d67804
Show file tree
Hide file tree
Showing 35 changed files with 317 additions and 164 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ jobs:
- name: Install hvm
run: go install
- name: Test
env:
HVM_GITHUBTOKEN: ${{ secrets.HVM_GITHUBTOKEN }}
run: go test ./...
7 changes: 6 additions & 1 deletion cmd/hvm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ 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)
Expand Down Expand Up @@ -152,10 +153,14 @@ 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.WorkingDir = wd
Expand Down
35 changes: 35 additions & 0 deletions cmd/hvm/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,39 @@ 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/install",
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
},
})
}
17 changes: 10 additions & 7 deletions cmd/hvm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,24 @@ 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()
useVersionInDotFile, err := cmd.Flags().GetBool("useVersionInDotFile")
cobra.CheckErr(err)

err = install(useVersionInDotFile)
cobra.CheckErr(err)
},
}

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")
installCmd.Flags().Bool("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 {
//
//gocyclo:ignore
func install(useVersionInDotFile bool) error {
repo := newRepository()
asset := newAsset()

Expand All @@ -64,10 +67,10 @@ func install() error {
if err != nil {
return err
}
asset.tag = version
if asset.tag == "" {
if version == "" {
return fmt.Errorf("the current directory does not contain an " + App.DotFileName + " file")
}
asset.tag = version
} else {
msg := "Select a version to use when version management is disabled"
err := repo.selectTag(asset, msg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
58 changes: 39 additions & 19 deletions cmd/hvm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ 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()
printExecPath, err := cmd.Flags().GetBool("printExecPath")
cobra.CheckErr(err)

err = status(printExecPath)
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, "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
// cache location. The "default" directory created by the "install" command is
// excluded.
func status() error {
//
//gocyclo:ignore
func status(printExecPath bool) error {
wd, err := os.Getwd()
if err != nil {
return err
Expand All @@ -75,7 +78,35 @@ func status() error {
if version == "" {
fmt.Println("Version management is disabled in the current directory.")
} else {
fmt.Printf("The current directory is configured to use Hugo %s.\n", version)
exists, err := helpers.Exists(filepath.Join(App.CacheDirPath, version, getExecName()))
if err != nil {
return err
}
if exists {
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 \n", App.DotFileName)
fmt.Printf("Hugo version (%s) that is not cached.\n", version)
fmt.Println()
for {
fmt.Printf("Would you like to install it now? (Y/n): ")
fmt.Scanln(&r)
if len(r) == 0 || strings.ToLower(string(r[0])) == "y" {
install(true)
fmt.Println()
break
}
if strings.ToLower(string(r[0])) == "n" {
err = disable()
if err != nil {
return err
}
fmt.Println()
break
}
}
}
}
}

Expand Down Expand Up @@ -167,10 +198,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)
}
Expand All @@ -181,15 +211,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
}
29 changes: 0 additions & 29 deletions cmd/hvm/testscripts/clean.txt

This file was deleted.

41 changes: 41 additions & 0 deletions cmd/hvm/testscripts/clean.txtar
Original file line number Diff line number Diff line change
@@ -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.'
stdout 'Are you sure you want to clean the cache\? \(y/N\): '
stdout 'Canceled'
[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.'
stdout 'Are you sure you want to clean the cache\? \(y/N\): '
stdout 'Cache cleaned.'
[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
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ exec hvm config
stdout 'githubToken ='
stdout 'numTagsToDisplay = 30'
stdout 'sortAscending = true'
stdout 'Configuration file: '
[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config.toml'
[linux] stdout 'Configuration file: .+/config/hvm/config.toml'
[windows] stdout 'Configuration file: .+\\config\\hvm\\config.toml'
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
! exec hvm config
stderr 'Error: configuration: numTagsToDisplay must be a non-zero integer'

# 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"
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
[windows] env AppData=config

env HVM_GITHUBTOKEN=my-token
env HVM_NUMTAGSTODISPLAY=42
env HVM_SORTASCENDING=true
env HVM_NUMTAGSTODISPLAY=67
env HVM_SORTASCENDING=false

# Test
exec hvm config
stdout 'githubToken = ''my-token'''
stdout 'numTagsToDisplay = 42'
stdout 'sortAscending = true'
stdout 'Configuration file: '
stdout 'githubToken ='
stdout 'numTagsToDisplay = 67'
stdout 'sortAscending = false'
[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config.toml'
[linux] stdout 'Configuration file: .+/config/hvm/config.toml'
[windows] stdout 'Configuration file: .+\\config\\hvm\\config.toml'
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ exec hvm config
stdout 'githubToken ='
stdout 'numTagsToDisplay = 42'
stdout 'sortAscending = false'
stdout 'Configuration file: '
[darwin] stdout 'Configuration file: .+/home/Library/Application Support/hvm/config.toml'
[linux] stdout 'Configuration file: .+/config/hvm/config.toml'
[windows] stdout 'Configuration file: .+\\config\\hvm\\config.toml'

# Files for darwin
# Files
-- home/Library/Application Support/hvm/config.toml --
githubtoken = "my-token"
numtagstodisplay = 42
sortascending = false

# Files for linux and windows
sortAscending = false
-- config/hvm/config.toml --
githubtoken = "my-token"
numtagstodisplay = 42
sortascending = false
sortAscending = false
-- config\\hvm\\config.toml --
numtagstodisplay = 42
sortAscending = false
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
! exists '.hvm'

-- .hvm --
zzz
v0.118.2
12 changes: 12 additions & 0 deletions cmd/hvm/testscripts/gen.txt → cmd/hvm/testscripts/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ stdout 'Generate an alias function for the specified shell.'
exec hvm gen alias bash
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.'

exec hvm gen alias fish
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.'

exec hvm gen alias powershell
stdout '# Hugo Version Manager: override path to the hugo executable.'

exec hvm gen alias powershell --help
stdout 'Generate an alias function for Windows PowerShell.'

exec hvm gen alias zsh
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.'
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 2d67804

Please sign in to comment.