Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gen command to generate alias scripts #24

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/hvm
cover.out
NOTES.md
release-header.md
68 changes: 8 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Use hvm in your development environment if you need to frequently switch between

## How it works

When you run the `hvm use` command, it will display a list of recent Hugo releases. You can then select a version to use in the current directory. The hvm application will download, extract, and cache the release asset for your operating system and architecture. It will also create an `.hvm` file in the current directory. This file contains the path to the cached Hugo executable.
When you run the `hvm use` command, it will display a list of recent Hugo releases. You can then select a version to use in the current directory. The hvm application will download, extract, and cache the release asset for your operating system and architecture. It will also create an `.hvm` file in the current directory. This file contains the version identifier.

In the second step of the installation instructions, you will create an alias for the `hugo` command. This alias will obtain the path to the cached Hugo executable from the `.hvm` file.
In the second step of the installation instructions, you will create an alias for the `hugo` command. This alias will obtain the path to the cached Hugo executable based on the version identifier in the `.hvm` file.

To use a different version of Hugo in the same directory, run the `hvm use` command again. To use the Hugo executable in your system PATH, run the `hvm disable` command.

Expand All @@ -46,64 +46,11 @@ go install github.com/jmooring/hvm@latest
Create an alias for the `hugo` command that overrides the executable path if a
valid `.hvm` file exists in the current directory.

<details>
<summary>Darwin</summary>
Add this function to $HOME/.zshrc

```zsh
# Hugo Version Manager: override path to the hugo executable.
hugo() {
hvm_show_status=true
hugo_bin=$(hvm status --printExecPath 2> /dev/null)
if [ -z "${hugo_bin}" ]; then
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
else
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
fi
"${hugo_bin}" "$@"
}
```

</details>

<details>
<summary>Linux</summary>
Add this function to $HOME/.bashrc

```bash
# Hugo Version Manager: override path to the hugo executable.
hugo() {
hvm_show_status=true
hugo_bin=$(hvm status --printExecPath 2> /dev/null)
if [ -z "${hugo_bin}" ]; then
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
else
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
fi
"${hugo_bin}" "$@"
}
```

</details>

<details>
<summary>Windows</summary>

TBD
1. Run `hvm gen alias --help` to find the subcommand for the desired shell.
2. Run `hvm gen alias <shell> --help` to see the installation instructions.
3. Run `hvm gen alias <shell>` to generate the alias script.

</details>
The `hvm gen alias` command generates alias scripts for bash, fish, zsh, and Windows PowerShell.

## Usage

Expand All @@ -116,6 +63,7 @@ Available Commands:
completion Generate the autocompletion script for the specified shell
config Display the current configuration
disable Disable version management in the current directory
gen Generate various files
help Help about any command
install Install a default version to use when version management is disabled
remove Remove the default version
Expand All @@ -127,7 +75,7 @@ Flags:
-h, --help Display help
-v, --version Display the hvm version

Use "hvm [command] --help" for more information about a command.
Use "hvm [command] --help" for more information about a command
```

## Configuration
Expand Down
35 changes: 35 additions & 0 deletions cmd/hvm/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright © 2023 Joe Mooring <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/spf13/cobra"
)

// aliasCmd represents the alias command
var aliasCmd = &cobra.Command{
Use: "alias",
Short: "Generate the alias script for the specified shell",
Long: `Generate the alias script for the specified shell.
See each sub-command's help for details on how to use the generated script.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
genCmd.AddCommand(aliasCmd)
}
20 changes: 20 additions & 0 deletions cmd/hvm/alias_scripts/bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Hugo Version Manager: override path to the hugo executable.
hugo() {
hvm_show_status=true
if [ -f ".hvm" ]; then
if ! hugo_bin=$(hvm status --printExecPath); then
return 1
else
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
fi
else
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
fi
"${hugo_bin}" "$@"
}
20 changes: 20 additions & 0 deletions cmd/hvm/alias_scripts/fish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Hugo Version Manager: override path to the hugo executable.
hugo() {
hvm_show_status=true
if [ -f ".hvm" ]; then
if ! hugo_bin=$(hvm status --printExecPath); then
return 1
else
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
fi
else
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
fi
"${hugo_bin}" "$@"
}
3 changes: 3 additions & 0 deletions cmd/hvm/alias_scripts/powershell.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hugo Version Manager: override path to the hugo executable.

TBD
20 changes: 20 additions & 0 deletions cmd/hvm/alias_scripts/zsh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Hugo Version Manager: override path to the hugo executable.
hugo() {
hvm_show_status=true
if [ -f ".hvm" ]; then
if ! hugo_bin=$(hvm status --printExecPath); then
return 1
else
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
fi
else
if ! hugo_bin=$(which hugo); then
>&2 printf "Command not found.\\n"
return 1
fi
fi
"${hugo_bin}" "$@"
}
43 changes: 43 additions & 0 deletions cmd/hvm/bash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright © 2023 Joe Mooring <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
_ "embed"
"fmt"

"github.com/spf13/cobra"
)

//go:embed alias_scripts/bash.sh
var bashScript string

// bashCmd represents the bash command
var bashCmd = &cobra.Command{
Use: "bash",
Short: "Generate the alias script for bash",
Long: `Generate the alias script for the bash shell.

Add the output from this command to $HOME/.bashrc or $HOME/.bash_aliases.
Open a new shell to activate the alias.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(bashScript)
},
}

func init() {
aliasCmd.AddCommand(bashCmd)
}
2 changes: 1 addition & 1 deletion cmd/hvm/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean the cache",
Long: `Cleans the cache, excluding the version installed with the "install"
Long: `Clean the cache, excluding the version installed with the "install"
command.`,
Run: func(cmd *cobra.Command, args []string) {
err := clean()
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
var configCmd = &cobra.Command{
Use: "config",
Short: "Display the current configuration",
Long: "Displays the current configuration and the path to the configuration file.",
Long: "Display the current configuration and the path to the configuration file.",
Run: func(cmd *cobra.Command, args []string) {
err := displayConfig()
cobra.CheckErr(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/hvm/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
var disableCmd = &cobra.Command{
Use: "disable",
Short: "Disable version management in the current directory",
Long: "Disables version management in the current directory.",
Long: "Disable version management in the current directory.",
Run: func(cmd *cobra.Command, args []string) {
err := disable()
cobra.CheckErr(err)
Expand Down
43 changes: 43 additions & 0 deletions cmd/hvm/fish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright © 2023 Joe Mooring <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
_ "embed"
"fmt"

"github.com/spf13/cobra"
)

//go:embed alias_scripts/fish.sh
var fishScript string

// fishCmd represents the fish command
var fishCmd = &cobra.Command{
Use: "fish",
Short: "Generate the alias script for fish",
Long: `Generate the alias script for the fish shell.

Add the output from this command to $HOME/.config/fish/config.fish.
Open a new shell to activate the alias.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(fishScript)
},
}

func init() {
aliasCmd.AddCommand(fishCmd)
}
34 changes: 34 additions & 0 deletions cmd/hvm/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright © 2023 Joe Mooring <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/spf13/cobra"
)

// genCmd represents the gen command
var genCmd = &cobra.Command{
Use: "gen",
Short: "Generate various files",
Long: "Generate various files.",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
rootCmd.AddCommand(genCmd)
}
40 changes: 40 additions & 0 deletions cmd/hvm/powershell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright © 2023 Joe Mooring <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
_ "embed"
"fmt"

"github.com/spf13/cobra"
)

//go:embed alias_scripts/powershell.ps1
var powershellScript string

// powershellCmd represents the powershell command
var powershellCmd = &cobra.Command{
Use: "powershell",
Short: "Generate the alias script for zsh",
Long: `Generate the alias script for powershell.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(powershellScript)
},
}

func init() {
aliasCmd.AddCommand(powershellCmd)
}
Loading
Loading