Skip to content

Commit

Permalink
cmd: Add gen command to generate alias scripts
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
jmooring committed Sep 20, 2023
1 parent 8dca82d commit d24f141
Show file tree
Hide file tree
Showing 20 changed files with 343 additions and 81 deletions.
79 changes: 6 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,79 +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>
<br>
Add this function to $HOME/.zshrc
<br>

```zsh
# 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}" "$@"
}
```

</details>

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

```bash
# 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}" "$@"
}
```

</details>

<details>
<summary>Windows</summary>
<br>
TBD
<br>

```powershell
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 @@ -131,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 @@ -142,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 <joe.mooring@veriphor.com>
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 <joe.mooring@veriphor.com>
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 <joe.mooring@veriphor.com>
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 <joe.mooring@veriphor.com>
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 <joe.mooring@veriphor.com>
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

0 comments on commit d24f141

Please sign in to comment.