* fixing bug with tab
All checks were successful
Go / build-test-publish (1.23.4) (push) Successful in 1m2s

This commit is contained in:
Denys Seredenko
2025-01-14 14:25:18 +01:00
parent 89289a53a4
commit c2d06f2b7f
2 changed files with 39 additions and 3 deletions

View File

@@ -6,9 +6,11 @@ import (
"github.com/creack/pty" "github.com/creack/pty"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/term"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"syscall"
) )
var execCmd = &cobra.Command{ var execCmd = &cobra.Command{
@@ -18,6 +20,7 @@ var execCmd = &cobra.Command{
if err := cobra.ExactArgs(1)(cmd, args); err != nil { if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err return err
} }
data := viper.GetStringMapString("data") data := viper.GetStringMapString("data")
if _, ok := data[args[0]]; !ok { if _, ok := data[args[0]]; !ok {
@@ -28,17 +31,49 @@ var execCmd = &cobra.Command{
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
data := viper.GetStringMapString("data") data := viper.GetStringMapString("data")
command := exec.Command("sh", "-c", data[args[0]]) commandStr, ok := data[args[0]]
if !ok {
fmt.Fprintf(os.Stderr, "No command found for alias '%s'\n", args[0])
os.Exit(1)
}
command := exec.Command("bash", "-c", commandStr)
command.Env = append(os.Environ(), "TERM=xterm-256color")
ptmx, err := pty.Start(command) ptmx, err := pty.Start(command)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error starting PTY: %v\n", err) fmt.Fprintf(os.Stderr, "Error starting PTY: %v\n", err)
os.Exit(1) os.Exit(1)
} }
defer func() { _ = ptmx.Close() }() defer func() { _ = ptmx.Close() }()
go func() {
for {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
fmt.Fprintf(os.Stderr, "Error resizing PTY: %v\n", err)
break
}
}
}()
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
fmt.Fprintf(os.Stderr, "Error setting raw mode: %v\n", err)
os.Exit(1)
}
defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }()
go func() { _, _ = io.Copy(ptmx, os.Stdin) }() go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
_, _ = io.Copy(os.Stdout, ptmx) _, _ = io.Copy(os.Stdout, ptmx)
if err := command.Wait(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
if ws, ok := exitError.Sys().(syscall.WaitStatus); ok {
os.Exit(ws.ExitStatus())
}
}
fmt.Fprintf(os.Stderr, "Command execution failed: %v\n", err)
}
}, },
} }

3
go.mod
View File

@@ -28,7 +28,8 @@ require (
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect golang.org/x/sys v0.29.0 // indirect
golang.org/x/term v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect golang.org/x/text v0.21.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect