Skip to content

Commit

Permalink
cmd/install: Add flag to install version specified in dot file
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
jmooring committed Sep 20, 2023
1 parent 2399cc1 commit f27b735
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 8 deletions.
Empty file added .hugo_build.lock
Empty file.
5 changes: 5 additions & 0 deletions cmd/hvm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type application struct {
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
WorkingDir string // current working directory
}

// A configuration contains the current configuration parameters from environment
Expand Down Expand Up @@ -150,9 +151,13 @@ func initApp() {
userCacheDir, err := os.UserCacheDir()
cobra.CheckErr(err)

wd, err := os.Getwd()
cobra.CheckErr(err)

App.CacheDirPath = filepath.Join(userCacheDir, App.Name)
App.ConfigFilePath = viper.ConfigFileUsed()
App.DefaultDirPath = filepath.Join(userCacheDir, App.Name, App.DefaultDirName)
App.WorkingDir = wd

err = os.MkdirAll(App.CacheDirPath, 0777)
cobra.CheckErr(err)
Expand Down
28 changes: 21 additions & 7 deletions cmd/hvm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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 @@ -48,6 +49,8 @@ you will be prompted to add it when installation is complete.`,

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"))
}

// install sets the version of the Hugo executable to use when version
Expand All @@ -56,13 +59,24 @@ func install() error {
repo := newRepository()
asset := newAsset()

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
if viper.GetBool("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
}
}

exists, err := helpers.Exists(filepath.Join(App.CacheDirPath, asset.tag))
Expand Down
4 changes: 3 additions & 1 deletion cmd/hvm/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ func getVersionFromDotFile(path string) (string, error) {
return "", err
}
if !exists {
return "", fmt.Errorf("the %s file in the current directory contains an invalid version (%s): %s", App.DotFileName, dotHvmContent, theFix)
if !viper.GetBool("useVersionInDotFile") {
return "", fmt.Errorf("the %s file in the current directory contains an invalid version (%s): %s", App.DotFileName, dotHvmContent, theFix)
}
}

return (dotHvmContent), nil
Expand Down
15 changes: 15 additions & 0 deletions cmd/hvm/testscripts/install_use_dot_file_fail.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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
! exec hvm install --useVersionInDotFile
stderr 'Error: the current directory does not contain a \.hvm file\n'
20 changes: 20 additions & 0 deletions cmd/hvm/testscripts/install_use_dot_file_pass.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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
exec hvm install --useVersionInDotFile
stdout 'Downloading v0.54.0... done.\n'
stdout 'Installation of v0.54.0 complete.\n'

# Files
-- .hvm --
v0.54.0
11 changes: 11 additions & 0 deletions cmd/hvm/testscripts/status-print-execpath_fail.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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 status --printExecPath
9 changes: 9 additions & 0 deletions public/categories/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Categories on </title>
<link>/categories/</link>
<description>Recent content in Categories on </description>
<generator>Hugo -- gohugo.io</generator><atom:link href="/categories/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
9 changes: 9 additions & 0 deletions public/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title></title>
<link>/</link>
<description>Recent content on </description>
<generator>Hugo -- gohugo.io</generator><atom:link href="/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
11 changes: 11 additions & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>/</loc>
</url><url>
<loc>/categories/</loc>
</url><url>
<loc>/tags/</loc>
</url>
</urlset>
9 changes: 9 additions & 0 deletions public/tags/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Tags on </title>
<link>/tags/</link>
<description>Recent content in Tags on </description>
<generator>Hugo -- gohugo.io</generator><atom:link href="/tags/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>

0 comments on commit f27b735

Please sign in to comment.