Skip to content

Commit

Permalink
init: auto default shell detection based on $SHELL
Browse files Browse the repository at this point in the history
  • Loading branch information
mroth committed Jan 17, 2022
1 parent c4c8e68 commit c65a6d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions commands/inits/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inits
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -97,5 +98,13 @@ There are a number of flags to customize the shell integration.
// defaultShell returns the shellType assumed if user does not specify.
// in the future, we may wish to customize this based on the $SHELL variable.
func defaultShellType() string {
if shellenv, ok := os.LookupEnv("SHELL"); ok {
base := filepath.Base(shellenv)
switch base {
case "sh", "bash", "zsh", "fish":
return base
}
}

return "sh"
}
27 changes: 27 additions & 0 deletions commands/inits/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package inits

import "testing"

func Test_defaultShellType(t *testing.T) {
tests := []struct {
shellenv string
want string
}{
// supported shells at a bunch of different locations
{"/bin/zsh", "zsh"},
{"/usr/bin/zsh", "zsh"},
{"/usr/local/bin/zsh", "zsh"},
{"/bin/bash", "bash"},
{"/usr/local/bin/fish", "fish"},

// edge cases
{"", "sh"},
{"/bin/unsupported", "sh"},
}
for _, tt := range tests {
t.Setenv("SHELL", tt.shellenv)
if got := defaultShellType(); got != tt.want {
t.Errorf("defaultShellType(%v) = %v, want %v", tt.shellenv, got, tt.want)
}
}
}

0 comments on commit c65a6d9

Please sign in to comment.