Skip to content

Commit 8c72a67

Browse files
authored
Merge pull request #544 from reubenmiller/fix-powershell-detection
fix: Installation on powershell 5.x on Windows
2 parents a63e882 + 140b536 commit 8c72a67

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

docs/go-c8y-cli/docs/installation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ The convenience script can be customized, for example you can choose how it is i
5454
<TabItem value="wget">
5555

5656
```bash
57-
wget -qO - https://docs-easy-installer--goc8ycli.netlify.app/install.sh | sh -s -- --help
57+
wget -qO - https://goc8ycli.netlify.app/install.sh | sh -s -- --help
5858
```
5959

6060
</TabItem>
6161
<TabItem value="curl">
6262

6363
```bash
64-
curl -fsSL https://docs-easy-installer--goc8ycli.netlify.app/install.sh | sh -s -- --help
64+
curl -fsSL https://goc8ycli.netlify.app/install.sh | sh -s -- --help
6565
```
6666

6767
</TabItem>
@@ -72,7 +72,7 @@ The convenience script can be customized, for example you can choose how it is i
7272
%%c8y%% can be installed using PowerShell using the following one-liner.
7373

7474
```bash
75-
. { Invoke-WebRequest https://docs-easy-installer--goc8ycli.netlify.app/install.ps1 } | Invoke-Expression; install-c8y
75+
. { Invoke-WebRequest https://goc8ycli.netlify.app/install.ps1 } | Invoke-Expression; install-c8y
7676
```
7777
7878
### Alternative Installation Methods

pkg/cmd/cli/install/install.manual.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var ErrInstallFailed = errors.New("failed to install one or more profiles")
2424
var validShells = shell.SupportedShells()
2525

2626
// Skip sh when installing all shells as it generally does not have a profile defined
27-
var defaultShells = []string{shell.ShellBash, shell.ShellFish, shell.ShellPowershell, shell.ShellZsh}
27+
var defaultShells = []string{shell.ShellBash, shell.ShellFish, shell.ShellPowershell, shell.ShellPwsh, shell.ShellZsh}
2828

2929
type CmdInstall struct {
3030
*subcommand.SubCommand
@@ -85,7 +85,8 @@ func (n *CmdInstall) RunE(cmd *cobra.Command, args []string) error {
8585
shell.ShellBash: {Name: "bash", Binary: "bash"},
8686
shell.ShellZsh: {Name: "zsh", Binary: "zsh"},
8787
shell.ShellPosixShell: {Name: "sh", Binary: "sh"},
88-
shell.ShellPowershell: {Name: "powershell", Binary: "pwsh"},
88+
shell.ShellPwsh: {Name: "pwsh", Binary: "pwsh"},
89+
shell.ShellPowershell: {Name: "powershell", Binary: "powershell"},
8990
shell.ShellFish: {Name: "fish", Binary: "fish"},
9091
}
9192

@@ -151,6 +152,17 @@ func (n *CmdInstall) RunE(cmd *cobra.Command, args []string) error {
151152
return summaryErr
152153
}
153154

155+
func addExecutableBinaryToPowerShellPath(existingSnippet string) (string, error) {
156+
exePath, err := os.Executable()
157+
if err != nil {
158+
return existingSnippet, err
159+
}
160+
exeDir := filepath.Dir(exePath)
161+
162+
snippet := fmt.Sprintf("$env:PATH += '%c%s'; ", filepath.ListSeparator, exeDir) + existingSnippet
163+
return snippet, nil
164+
}
165+
154166
func (n *CmdInstall) InstallProfile(shellType string) (bool, error) {
155167
changed := false
156168
cfg, err := n.factory.Config()
@@ -188,8 +200,19 @@ func (n *CmdInstall) InstallProfile(shellType string) (bool, error) {
188200
profilePath = "~/.config/fish/config.fish"
189201
profileSnippet = "c8y cli profile --shell fish | source"
190202
case shell.ShellPowershell:
203+
profilePath = "~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1"
204+
if snippet, err := addExecutableBinaryToPowerShellPath("c8y cli profile --shell powershell | Out-String | Invoke-Expression"); err == nil {
205+
profileSnippet = snippet
206+
} else {
207+
cfg.Logger.Warnf("Failed to detect binary path. %s", err)
208+
}
209+
case shell.ShellPwsh:
191210
profilePath = "~/.config/powershell/Microsoft.PowerShell_profile.ps1"
192-
profileSnippet = "c8y cli profile --shell powershell | Out-String | Invoke-Expression"
211+
if snippet, err := addExecutableBinaryToPowerShellPath("c8y cli profile --shell powershell | Out-String | Invoke-Expression"); err == nil {
212+
profileSnippet = snippet
213+
} else {
214+
cfg.Logger.Warnf("Failed to detect binary path. %s", err)
215+
}
193216
}
194217

195218
if profilePath == "" {

pkg/cmd/cli/profile/profile.manual.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (n *CmdProfile) RunE(cmd *cobra.Command, args []string) error {
113113
script = &scriptPosixShell
114114
case shell.ShellFish:
115115
script = &scriptFish
116-
case shell.ShellPowershell:
116+
case shell.ShellPowershell, shell.ShellPwsh:
117117
script = &scriptPowerShell
118118
}
119119

pkg/cmd/completion/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func NewCmdCompletion() *CmdCompletion {
9292
err = cmd.Root().GenZshCompletion(os.Stdout)
9393
case shell.ShellFish:
9494
err = cmd.Root().GenFishCompletion(os.Stdout, true)
95-
case shell.ShellPowershell:
95+
case shell.ShellPowershell, shell.ShellPwsh:
9696
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
9797
case shell.ShellPosixShell:
9898
err = fmt.Errorf("posix shell (sh) does not support tab completion")

pkg/shell/shell.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var (
2727
ShellBash = "bash"
2828
ShellZsh = "zsh"
2929
ShellPowershell = "powershell"
30+
ShellPwsh = "pwsh"
3031
ShellPosixShell = "sh"
3132
ShellAuto = "auto"
3233
)
@@ -42,7 +43,7 @@ func SupportedShells(additional ...string) []string {
4243
}
4344

4445
func SupportTabCompletionShells(additional ...string) []string {
45-
values := []string{ShellFish, ShellBash, ShellZsh, ShellPowershell}
46+
values := []string{ShellFish, ShellBash, ShellZsh, ShellPowershell, ShellPwsh}
4647
for _, v := range additional {
4748
if !slices.Contains(values, v) {
4849
values = append(values, v)

0 commit comments

Comments
 (0)