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 f7e48c5 commit eaf4190
Show file tree
Hide file tree
Showing 42 changed files with 525 additions and 341 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 use --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 use --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 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"
}
}
}
}
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 use --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: 10 additions & 3 deletions 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 @@ -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)
}

Expand All @@ -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
28 changes: 28 additions & 0 deletions cmd/hvm/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
})
}
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
60 changes: 9 additions & 51 deletions cmd/hvm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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))
Expand All @@ -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
}
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 eaf4190

Please sign in to comment.