Skip to content

Commit 677b174

Browse files
author
David May
committed
Add setup cmd. Rename shell wrapper files. Reformat setup output.
1 parent a5b66ea commit 677b174

File tree

12 files changed

+109
-85
lines changed

12 files changed

+109
-85
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var rootCmd = &cobra.Command{
3232
return err
3333
}
3434

35-
if config.RunUserEnvironmentCheck() == true {
35+
if config.RunUserEnvironmentChecks() == true {
3636
os.Exit(1) // User asked to update environment.
3737
}
3838
return nil

cmd/setup.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"github.com/davfive/gitspaces/v2/internal/config"
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// setupCmd represents the setup command
12+
var setupCmd = &cobra.Command{
13+
Use: "setup",
14+
Short: "(Re)run gitspaces setup wizard",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
config.ForceUserEnvironmentSetup()
17+
},
18+
}
19+
20+
func init() {
21+
rootCmd.AddCommand(setupCmd)
22+
}

internal/config/setup.go

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,122 +5,123 @@ import (
55
"github.com/davfive/gitspaces/v2/internal/utils"
66
)
77

8-
// RunUserEnvironmentCheck() runs a series of checks to ensure the user's environment
8+
func ForceUserEnvironmentSetup() {
9+
console.Println("============================")
10+
console.Println("== GitSpaces Setup Wizard ==")
11+
console.Println("============================")
12+
runUserEnvironmentChecks(true)
13+
}
14+
15+
func RunUserEnvironmentChecks() bool {
16+
return runUserEnvironmentChecks(false)
17+
}
18+
19+
// runUserEnvironmentChecks() runs a series of checks to ensure the user's environment
920
// is properly configured for GitSpaces. Returns true if any checks failed.
1021
// When a check fails, the user is prompted to update their environment. Once that is
1122
// done, the user would be able to run the gitspaces command again successfully
12-
func RunUserEnvironmentCheck() (checkFailed bool) {
23+
func runUserEnvironmentChecks(force bool) (checkFailed bool) {
1324
shellUpdated := false
1425

15-
if runProjectPathsCheck() == true {
26+
if runProjectPathsCheck(force) == true {
1627
checkFailed = true
1728
}
18-
if runShellWrapperCheck() == true {
29+
if runShellWrapperCheck(force) == true {
1930
shellUpdated = true
2031
checkFailed = true
2132
}
2233

2334
if checkFailed {
24-
console.Println("\nYou are ready to use GitSpaces (assuming you followed the instructions).")
35+
console.Println("\nSee https://github.com/davfive/gitspaces README for full setup and use instructions.")
36+
console.Println("You are ready to use GitSpaces (assuming you followed the instructions).")
2537
if shellUpdated {
26-
console.Println("Open a new shell and rerun 'gitspaces' (the shell wrapper) to start using GitSpaces.")
38+
console.Println("Open a new shell and run 'gitspaces' (the shell wrapper) to start using GitSpaces.")
2739
} else {
28-
console.Println("Rerun 'gitspaces' (the shell wrapper) to start using GitSpaces.")
40+
console.Println("Run 'gitspaces' (the shell wrapper) to start using GitSpaces.")
2941
}
3042
}
3143
return checkFailed
3244
}
3345

3446
// runProjectPathsCheck() prompts the user to set project paths in the config file
3547
// if they are not already set. Returns true if user asked to update paths.
36-
func runProjectPathsCheck() bool {
37-
if len(User.projectPaths) > 0 {
48+
func runProjectPathsCheck(force bool) bool {
49+
if !force && len(User.projectPaths) > 0 {
3850
return false
3951
}
4052

41-
console.Println("** Warning - Empty ProjectPaths found in %s **\n", User.GetConfigFile())
42-
console.Println("GitSpaces uses the ProjectPaths field to know where to find your projects")
43-
console.Println("Fill in the ProjectPaths in your config file with something like:")
53+
console.PrintSeparateln("== Setup GitSpaces ProjectPaths")
54+
console.Println("GitSpaces uses the ProjectPaths field to know where to find your projects.")
55+
console.Println("")
56+
console.Println("Fill in the ProjectPaths in the GitSpaces config file with something like:")
4457
console.Println("ProjectPaths:")
45-
console.Println(" - {{.HomeDir}}/code/projects")
46-
console.Println(" - {{.HomeDir}}/code/play")
58+
console.Println(" - %s/code/projects", utils.GetUserHomeDir())
59+
console.Println(" - %s/code/play", utils.GetUserHomeDir())
60+
console.Println("")
61+
console.Println("The config file is located at: %s", User.GetConfigFile())
4762
console.Println("")
4863

4964
if console.NewConfirm().Prompt("Edit config file?").Run() == true {
5065
if err := utils.OpenFileInDefaultApp(User.GetConfigFile()); err != nil {
5166
console.Errorln("Editing config file failed: %s", err)
67+
} else {
68+
console.NewInput().Prompt("Press <enter> when done editing the file ...").Run()
5269
}
5370
}
5471
return true
5572
}
5673

5774
// runProjectPathsCheck() prompts the user to set project paths in the config file
5875
// if they are not already set. Returns true if user asked to update paths.
59-
func runShellWrapperCheck() bool {
60-
if User.wrapped {
76+
func runShellWrapperCheck(force bool) bool {
77+
if !force && User.wrapped {
6178
return false
6279
}
6380

64-
console.Println("** Warning - GitSpaces not run from shell wrapper **\n")
81+
console.PrintSeparateln("== Setup GitSpaces Shell Wrapper")
6582
console.Println("GitSpaces requires a wrapper function in your shell profile/rc file.")
6683
console.Println("The wrapper handles when a 'gitspaces <command>' needs to 'cd' to a new directory.")
67-
console.Println("\nWrappers are available for common shells (bash, zsh, pwsh).")
68-
console.Println("See https://github.com/davfive/gitspaces README for all setup instructions.")
6984
console.Println("")
7085

86+
if !User.wrapped {
87+
console.Println("** Warning - GitSpaces not run from shell wrapper **\n")
88+
}
89+
90+
shellFiles := GetShellFiles()
7191
if Debug {
72-
shellFiles := GetShellFiles()
7392
console.Println("The following wrapper files were created:")
7493
for _, key := range utils.SortKeys(shellFiles) {
7594
console.Println(" %s", shellFiles[key].path)
7695
}
7796
console.Println("")
7897
}
7998

80-
if User.pterm != "" {
81-
console.Println("Your current shell is: %s", User.pterm)
99+
console.Println("Shell Wrapper Setup Instructions:")
100+
console.Println("1. Copy the following lines:")
101+
if User.pterm == "pwsh" {
102+
console.Println(". %s", shellFiles["ps1ScriptBlock"].path)
103+
console.Println("Set-Alias -Name gs -Value gitspaces # optional")
104+
} else {
105+
console.Println(". %s", shellFiles["shellFunction"].path)
106+
console.Println("alias gs=gitspaces")
82107
}
83-
if console.NewConfirm().Prompt("Would you like to configure it now?").Run() == true {
84-
switch User.pterm {
85-
case "bash", "zsh":
86-
setupBashZshWrapper()
87-
case "pwsh":
88-
setupPwshWrapper()
89-
default:
90-
console.Println("Unable to determine your current shell rc file. Assuming *nix-style.")
91-
console.Println("Copy/paste these lines into your shell's rc file:")
92-
console.Println(". %s/gitspaces.sh", utils.CygwinizePath(User.dotDir))
93-
console.Println("alias gs=gitspaces")
94-
}
95-
}
96-
97-
return true
98-
}
108+
console.Println("2. Paste the lines into your shell profile or rc file.")
109+
console.Println("3. Open a new shell and run 'gitspaces' to start using GitSpaces.")
99110

100-
func setupBashZshWrapper() {
101-
shellrc := User.getShellRcFile()
102-
console.Println("Copy/paste these lines into your shell's rc file:")
103-
console.Println(". %s/gitspaces.sh", utils.CygwinizePath(User.dotDir))
104-
console.Println("alias gs=gitspaces")
105-
console.Println("\nYour shell rc file is located at: %s", shellrc)
106-
if console.NewConfirm().Prompt("Edit shell rc file?").Run() == true {
107-
if err := utils.OpenFileInDefaultApp(shellrc); err != nil {
108-
console.Errorln("Editing shell rc file failed: %s", err)
111+
if User.pterm != "" {
112+
shellRcFile := User.getShellRcFile()
113+
console.Println("\nYour current shell is: %s", User.pterm)
114+
console.Println("Your shell profile/rc file is located at: %s", shellRcFile)
115+
console.Println("")
116+
if console.NewConfirm().Prompt("Edit %s?", shellRcFile).Run() == true {
117+
utils.CreateEmptyFileIfNotExists(shellRcFile)
118+
if err := utils.OpenFileInDefaultApp(shellRcFile); err != nil {
119+
console.Errorln("Editing shell rc file failed: %s", err)
120+
} else {
121+
console.NewInput().Prompt("Press <enter> when done editing the file ...").Run()
122+
}
109123
}
110124
}
111-
}
112125

113-
func setupPwshWrapper() {
114-
shellFiles := GetShellFiles()
115-
shellrc := User.getShellRcFile()
116-
console.Println("Copy/paste these lines into your PowerShell $PROFILE file:")
117-
console.Println(". %s", shellFiles["ps1ScriptBlock"].path)
118-
console.Println("Set-Alias -Name gs -Value gitspaces # optional")
119-
console.Println("\nYour PowerShell profile is located at: %s", shellrc)
120-
// console.Println("For more information on your PowerShell profile, see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.4#the-profile-variable")
121-
if console.NewConfirm().Prompt("Edit PowerShell $PROFILE?").Run() == true {
122-
if err := utils.OpenFileInDefaultApp(shellrc); err != nil {
123-
console.Errorln("Editing PowerShell $PROFILE failed: %s", err)
124-
}
125-
}
126+
return true
126127
}

internal/config/shellfiles.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
"github.com/davfive/gitspaces/v2/internal/utils"
1010
)
1111

12-
//go:embed templates/gitspaces.tmpl.sh
13-
var shellTmpl []byte
12+
//go:embed templates/gitspaces.function.tmpl.sh
13+
var shellFunctionTmpl []byte
1414

15-
//go:embed templates/gitspaces.tmpl.ps1
16-
var ps1Tmpl []byte
15+
//go:embed templates/gitspaces.cmdlet.tmpl.ps1
16+
var ps1CmdletTmpl []byte
1717

1818
//go:embed templates/gitspaces.scriptblock.tmpl.ps1
1919
var ps1ScriptBlockTmpl []byte
@@ -29,12 +29,12 @@ type shellFileStruct struct {
2929

3030
func GetShellFiles() map[string]*shellFileStruct {
3131
shellFiles := map[string]*shellFileStruct{
32-
"shellScript": NewShellFile("Shell").
33-
File("gitspaces.sh").
34-
Template(string(shellTmpl)),
35-
"ps1Script": NewShellFile("ps1Script").
36-
File("gitspaces.ps1").
37-
Template(string(ps1Tmpl)),
32+
"shellFunction": NewShellFile("shellFunction").
33+
File("gitspaces.function.sh").
34+
Template(string(shellFunctionTmpl)),
35+
"ps1Cmdlet": NewShellFile("ps1Cmdlet").
36+
File("gitspaces.cmdlet.ps1").
37+
Template(string(ps1CmdletTmpl)),
3838
"ps1ScriptBlock": NewShellFile("ps1ScriptBlock").
3939
File("gitspaces.scriptblock.ps1").
4040
Template(string(ps1ScriptBlockTmpl)),

internal/config/templates/config.yaml.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
# The ProjectPaths directories would contain your project folders
33
# e.g.,
44
# ProjectPaths:
5-
# - {{.HomeDir}}/code/projects
6-
# - {{.HomeDir}}/code/play
5+
# - {{ .homeDir }}/code/projects
6+
# - {{ .homeDir }}/code/play
77
ProjectPaths:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
new-module -scriptblock {
22
function gitspaces {
3-
. {{ .ps1ScriptPath }} $args
3+
. {{ .ps1CmdletPath }} $args
44
}
55
} -name gitspaces-scriptblock -force -export

internal/config/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func initUser(ppidFlag int) (user *userStruct, err error) {
5858
func (user *userStruct) getShellTmplVars(shellFiles map[string]*shellFileStruct) map[string]interface{} {
5959
tmplVars := map[string]interface{}{
6060
"exePath": utils.Executable(),
61+
"homeDir": utils.GetUserHomeDir(),
6162
"userDotDir": user.dotDir,
6263
}
6364
for _, shellFile := range shellFiles {

internal/console/prompt_confirm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (c *Confirm) Title(title string) *Confirm {
4242
return c
4343
}
4444

45-
func (c *Confirm) Prompt(title string) *Confirm {
46-
c.title = title
45+
func (c *Confirm) Prompt(title string, a ...any) *Confirm {
46+
c.title = fmt.Sprintf(title, a...)
4747
return c
4848
}
4949

0 commit comments

Comments
 (0)