Skip to content

Commit

Permalink
shell
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Jun 25, 2024
1 parent 757bef4 commit 4c6bb94
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 37 deletions.
20 changes: 11 additions & 9 deletions internal/shell/sh/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ func (b *BashShell) WriteVMEnvToShell() {
installPath := cnf.GetVMRWorkDir()
vmEnvConfPath := b.VMEnvConfPath()

content, _ := os.ReadFile(vmEnvConfPath)
oldEnvStr := strings.TrimSpace(string(content))
// content, _ := os.ReadFile(vmEnvConfPath)
// oldEnvStr := strings.TrimSpace(string(content))
envStr := fmt.Sprintf(vmEnvZsh, FormatPathString(installPath))
if !strings.Contains(oldEnvStr, envStr) {
if oldEnvStr != "" {
envStr = envStr + "\n" + oldEnvStr
}
_ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
}
vmrEnvPath := fmt.Sprintf("export PATH=%s:$PATH", FormatPathString(installPath))
UpdateVMRShellFile(vmEnvConfPath, vmrEnvPath, envStr)
// if !strings.Contains(oldEnvStr, envStr) {
// if oldEnvStr != "" {
// envStr = envStr + "\n" + oldEnvStr
// }
// _ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
// }

shellConfig := b.ConfPath()
content, _ = os.ReadFile(shellConfig)
content, _ := os.ReadFile(shellConfig)
data := string(content)

sourceStr := fmt.Sprintf(shellContent, VMDisableEnvName, FormatPathString(vmEnvConfPath))
Expand Down
21 changes: 12 additions & 9 deletions internal/shell/sh/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ func (f *FishShell) WriteVMEnvToShell() {
installPath := cnf.GetVMRWorkDir()
vmEnvConfPath := f.VMEnvConfPath()

content, _ := os.ReadFile(vmEnvConfPath)
oldEnvStr := strings.TrimSpace(string(content))
// content, _ := os.ReadFile(vmEnvConfPath)
// oldEnvStr := strings.TrimSpace(string(content))
envStr := fmt.Sprintf(vmEnvFish, FormatPathString(installPath))
if !strings.Contains(oldEnvStr, envStr) {
if oldEnvStr != "" {
envStr = envStr + "\n" + oldEnvStr
}
_ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
}

vmrEnvPath := fmt.Sprintf("fish_add_path --global %s", FormatPathString(installPath))
UpdateVMRShellFile(vmEnvConfPath, vmrEnvPath, envStr)
// if !strings.Contains(oldEnvStr, envStr) {
// if oldEnvStr != "" {
// envStr = envStr + "\n" + oldEnvStr
// }
// _ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
// }

shellConfig := f.ConfPath()
content, _ = os.ReadFile(shellConfig)
content, _ := os.ReadFile(shellConfig)
data := string(content)

sourceStr := fmt.Sprintf(fishShellContent, VMDisableEnvName, FormatPathString(vmEnvConfPath))
Expand Down
27 changes: 27 additions & 0 deletions internal/shell/sh/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sh
import (
"io/fs"
"os"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -39,3 +40,29 @@ func FormatPathString(p string) (formattedPath string) {
}
return
}

/*
Update vmr.sh or vmr.fish
*/
var ShellRegExp = regexp.MustCompile(`# cd hook start[\w\W]+# cd hook end`)

func UpdateVMRShellFile(fPath, vmrPathEnv, newHookContent string) {
oldData, _ := os.ReadFile(fPath)
oldContent := string(oldData)
if oldContent == "" {
os.WriteFile(fPath, []byte(newHookContent), ModePerm)
return
}
oldHookContent := ShellRegExp.FindString(oldContent)

if !strings.Contains(oldHookContent, vmrPathEnv) {
oldContent = strings.ReplaceAll(oldContent, vmrPathEnv, "")
}

if oldHookContent != "" {
oldContent = strings.ReplaceAll(oldContent, oldHookContent, newHookContent)
} else {
oldContent = newHookContent + "\n" + oldContent
}
_ = os.WriteFile(fPath, []byte(strings.TrimSpace(oldContent)), ModePerm)
}
20 changes: 11 additions & 9 deletions internal/shell/sh/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ func (z *ZshShell) WriteVMEnvToShell() {
installPath := cnf.GetVMRWorkDir()
vmEnvConfPath := z.VMEnvConfPath()

content, _ := os.ReadFile(vmEnvConfPath)
oldEnvStr := strings.TrimSpace(string(content))
// content, _ := os.ReadFile(vmEnvConfPath)
// oldEnvStr := strings.TrimSpace(string(content))
envStr := fmt.Sprintf(vmEnvZsh, FormatPathString(installPath))
if !strings.Contains(oldEnvStr, envStr) {
if oldEnvStr != "" {
envStr = envStr + "\n" + oldEnvStr
}
_ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
}
vmrEnvPath := fmt.Sprintf("export PATH=%s:$PATH", FormatPathString(installPath))
UpdateVMRShellFile(vmEnvConfPath, vmrEnvPath, envStr)
// if !strings.Contains(oldEnvStr, envStr) {
// if oldEnvStr != "" {
// envStr = envStr + "\n" + oldEnvStr
// }
// _ = os.WriteFile(vmEnvConfPath, []byte(envStr), ModePerm)
// }
shellConfig := z.ConfPath()
content, _ = os.ReadFile(shellConfig)
content, _ := os.ReadFile(shellConfig)
data := string(content)

sourceStr := fmt.Sprintf(shellContent, VMDisableEnvName, FormatPathString(vmEnvConfPath))
Expand Down
20 changes: 19 additions & 1 deletion internal/shell/win.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ const (
PathEnvName string = "path"
)

const oldPwsHook string = `function cdhook {
$TRUE_FALSE=(Test-Path $args[0])
if ( $TRUE_FALSE -eq "True" )
{
chdir $args[0]
vmr use -E
}
}
function vmrsource {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
}
Set-Alias -Name cd -Option AllScope -Value cdhook
Set-Alias -Name source -Value vmrsource`

// PowershellHook for Powershell
const PowershellHook string = `# cd hook start
function cdhook {
Expand Down Expand Up @@ -128,7 +144,9 @@ func (s *Shell) cdHook() {
return
}

if content != "" {
if strings.Contains(content, oldPwsHook) {
content = strings.ReplaceAll(content, oldPwsHook, PowershellHook)
} else if content != "" {
content = fmt.Sprintf("%s\n%s", PowershellHook, content)
} else {
content = PowershellHook
Expand Down
21 changes: 12 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
package main

import (
"fmt"
"os"

"github.com/gvcgo/version-manager/internal/cnf"
"github.com/gvcgo/version-manager/internal/shell"
"github.com/gvcgo/version-manager/internal/tui/cmds"
"regexp"
)

func main() {
Expand Down Expand Up @@ -72,12 +70,17 @@ func main() {

// test vmr

sh := shell.NewShell()
sh.WriteVMEnvToShell()
os.Setenv(cnf.VMRLocalProxyEnv, "http://localhost:2023")
ll := cmds.NewTUI()
ll.ListSDKName()
// sh := shell.NewShell()
// sh.WriteVMEnvToShell()
// os.Setenv(cnf.VMRLocalProxyEnv, "http://localhost:2023")
// ll := cmds.NewTUI()
// ll.ListSDKName()

var ShellRegExp = regexp.MustCompile(`# cd hook start[\w\W]+# cd hook end`)

content, _ := os.ReadFile("/home/moqsien/.vmr/vmr.sh")
s := ShellRegExp.FindString(string(content))
fmt.Println(s)
// _, err := gutils.ExecuteSysCommand(
// true,
// "",
Expand Down

0 comments on commit 4c6bb94

Please sign in to comment.