From b3c5d75f9a1f86ef629be544a90aea35f19fc3ed Mon Sep 17 00:00:00 2001 From: Alexandre Date: Mon, 3 Feb 2025 08:42:50 +0100 Subject: [PATCH] Added the pseudo TTY requirement for ssh, nano, vim but signals are not supported yet --- go.mod | 1 + go.sum | 2 ++ main.go | 35 +++++++++++++++++++++++------------ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 4221dfd..098c76e 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.5 require ( github.com/chzyer/readline v1.5.1 // indirect + github.com/creack/pty v1.1.24 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/magiconair/properties v1.8.7 // indirect diff --git a/go.sum b/go.sum index 13073d7..911bd02 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwys github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI= github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk= github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8= +github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= +github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= diff --git a/main.go b/main.go index e28c3cf..cd249c0 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,14 @@ package main import ( "fmt" + "io" "os" "os/exec" "strconv" "strings" "github.com/chzyer/readline" + "github.com/creack/pty" "github.com/spf13/viper" ) @@ -68,7 +70,7 @@ func main() { func loadConfig() { homeDir, _ := os.UserHomeDir() configPath := homeDir + "/.config/gosh/gosh_config.toml" - fmt.Println("Chemin du fichier de configuration:", configPath) // Log pour déboguer + fmt.Println("Chemin du fichier de configuration:", configPath) viper.SetConfigFile(configPath) // Valeurs par défaut @@ -129,12 +131,6 @@ func getPrompt() string { prompt = blue + prompt + reset } - if config.Color == "green" { - green := "\033[32m" - reset := "\033[0m" - prompt = green + prompt + reset - } - return prompt } @@ -156,7 +152,7 @@ func execInput(input string) error { case "exit": os.Exit(0) case "version": - fmt.Println("GoShell Version 1.0.0") + fmt.Println("GoShell Version 2.1.2") return nil case "set": if len(args) < 3 { @@ -165,11 +161,26 @@ func execInput(input string) error { return setConfig(args[1], strings.Join(args[2:], " ")) } - // Exécuter la commande système + // Exécuter la commande système dans un PTY cmd := exec.Command(args[0], args[1:]...) - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - return cmd.Run() + ptmx, err := pty.Start(cmd) + if err != nil { + return fmt.Errorf("Erreur lors du démarrage du PTY: %v", err) + } + defer ptmx.Close() + + // Rediriger les entrées/sorties entre le terminal parent et le PTY + go func() { + io.Copy(ptmx, os.Stdin) // Rediriger stdin vers le PTY + }() + io.Copy(os.Stdout, ptmx) // Rediriger stdout du PTY vers le terminal + + // Attendre la fin de la commande + if err := cmd.Wait(); err != nil { + return fmt.Errorf("Erreur lors de l'exécution de la commande: %v", err) + } + + return nil } // Fonction pour modifier la configuration à la volée