Skip to content

Commit

Permalink
init: refactor script collection concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
mroth committed Jan 17, 2022
1 parent 3a50292 commit d578716
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
1 change: 0 additions & 1 deletion commands/inits/data/git_wrapper.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Based on https://github.com/arbelt/fish-plugin-scmpuff,
# with scmpuff-exec support (https://github.com/mroth/scmpuff/pull/49)

functions -e git

set -q SCMPUFF_GIT_CMD; or set -x SCMPUFF_GIT_CMD (which git)
Expand Down
26 changes: 15 additions & 11 deletions commands/inits/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package inits
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

// Since the flags are defined and used in different locations, we need to
// define a variable outside with the correct scope to assign the flag to work
// with.
var includeAliases bool
var legacyShow bool
var wrapGit bool
var shellType string

// CommandInit generates the command handler for `scmpuff init`
func CommandInit() *cobra.Command {
var (
shellType string
includeAliases bool
wrapGit bool
legacyShow bool
)

var InitCmd = &cobra.Command{
Use: "init",
Expand All @@ -39,13 +38,18 @@ There are a number of flags to customize the shell integration.
if legacyShow {
shellType = defaultShellType()
}
switch shellType {

switch strings.ToLower(shellType) {
case "":
cmd.Help()
os.Exit(0)

case "sh", "bash", "zsh", "fish":
printScript()
case "sh", "bash", "zsh":
fmt.Println(bashCollection.Output(wrapGit, includeAliases))
os.Exit(0)

case "fish":
fmt.Println(fishCollection.Output(wrapGit, includeAliases))
os.Exit(0)

default:
Expand Down
42 changes: 27 additions & 15 deletions commands/inits/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package inits

import (
_ "embed"
"fmt"
"strings"
)

//go:embed data/status_shortcuts.sh
Expand All @@ -20,22 +20,34 @@ var scriptGitWrapper string
//go:embed data/git_wrapper.fish
var scriptGitWrapperFish string

func printScript() {
if shellType == "fish" {
fmt.Println(scriptStatusShortcutsFish)
} else {
fmt.Println(scriptStatusShortcuts)
}
type scriptCollection struct {
statusShortcuts string
gitWrapper string
aliases string
}

if includeAliases {
fmt.Println(scriptAliases)
}
var bashCollection = scriptCollection{
statusShortcuts: scriptStatusShortcuts,
gitWrapper: scriptGitWrapper,
aliases: scriptAliases,
}

var fishCollection = scriptCollection{
statusShortcuts: scriptStatusShortcutsFish,
gitWrapper: scriptGitWrapperFish,
aliases: scriptAliases,
}

func (sc scriptCollection) Output(wrapGit, aliases bool) string {
var b strings.Builder
b.WriteString(sc.statusShortcuts)
if wrapGit {
if shellType == "fish" {
fmt.Println(scriptGitWrapperFish)
} else {
fmt.Println(scriptGitWrapper)
}
b.WriteRune('\n')
b.WriteString(sc.gitWrapper)
}
if aliases {
b.WriteRune('\n')
b.WriteString(sc.aliases)
}
return b.String()
}

0 comments on commit d578716

Please sign in to comment.