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 24, 2023
1 parent 22a6fd4 commit 006351b
Show file tree
Hide file tree
Showing 41 changed files with 409 additions and 190 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 ./...
12 changes: 8 additions & 4 deletions cmd/hvm/alias_scripts/bash.sh
Original file line number Diff line number Diff line change
@@ -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 install --useVersionInDotFile
else
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
fi
fi
"${hugo_bin}" "$@"
Expand Down
12 changes: 8 additions & 4 deletions cmd/hvm/alias_scripts/fish.sh
Original file line number Diff line number Diff line change
@@ -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 install --useVersionInDotFile
else
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
fi
fi
"${hugo_bin}" "$@"
Expand Down
13 changes: 9 additions & 4 deletions cmd/hvm/alias_scripts/powershell.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# 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."
echo "Run 'hvm status' for details, or 'hvm disable' to disable.`n"
}
& "$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 install --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"
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/hvm/alias_scripts/zsh.sh
Original file line number Diff line number Diff line change
@@ -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 install --useVersionInDotFile
else
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
fi
fi
"${hugo_bin}" "$@"
Expand Down
9 changes: 8 additions & 1 deletion cmd/hvm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
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
},
})
}
10 changes: 2 additions & 8 deletions cmd/hvm/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"fmt"
"os"
"path/filepath"

"github.com/jmooring/hvm/pkg/helpers"
"github.com/spf13/cobra"
Expand All @@ -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
}
Expand Down
19 changes: 11 additions & 8 deletions cmd/hvm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,36 @@ 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()

if useVersionInDotFile {
version, err := getVersionFromDotFile(filepath.Join(App.WorkingDir, App.DotFileName))
version, err := getVersionFromDotFile(App.DotFilePath)
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
Loading

0 comments on commit 006351b

Please sign in to comment.