Added the pseudo TTY requirement for ssh, nano, vim but signals are not supported yet

This commit is contained in:
Alexandre 2025-02-03 08:42:50 +01:00
parent e64ed1373c
commit b3c5d75f9a
3 changed files with 26 additions and 12 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.23.5
require ( require (
github.com/chzyer/readline v1.5.1 // indirect 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/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.7 // indirect

2
go.sum
View File

@ -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 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk= 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/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.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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=

35
main.go
View File

@ -2,12 +2,14 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"github.com/chzyer/readline" "github.com/chzyer/readline"
"github.com/creack/pty"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -68,7 +70,7 @@ func main() {
func loadConfig() { func loadConfig() {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
configPath := homeDir + "/.config/gosh/gosh_config.toml" 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) viper.SetConfigFile(configPath)
// Valeurs par défaut // Valeurs par défaut
@ -129,12 +131,6 @@ func getPrompt() string {
prompt = blue + prompt + reset prompt = blue + prompt + reset
} }
if config.Color == "green" {
green := "\033[32m"
reset := "\033[0m"
prompt = green + prompt + reset
}
return prompt return prompt
} }
@ -156,7 +152,7 @@ func execInput(input string) error {
case "exit": case "exit":
os.Exit(0) os.Exit(0)
case "version": case "version":
fmt.Println("GoShell Version 1.0.0") fmt.Println("GoShell Version 2.1.2")
return nil return nil
case "set": case "set":
if len(args) < 3 { if len(args) < 3 {
@ -165,11 +161,26 @@ func execInput(input string) error {
return setConfig(args[1], strings.Join(args[2:], " ")) 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 := exec.Command(args[0], args[1:]...)
cmd.Stderr = os.Stderr ptmx, err := pty.Start(cmd)
cmd.Stdout = os.Stdout if err != nil {
return cmd.Run() 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 // Fonction pour modifier la configuration à la volée