From 1191d1548470a7ed1049f1d78de6f17b77e0d36d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 6 Jan 2024 17:51:41 -0600 Subject: purge years of old test code Signed-off-by: Jeff Carr --- linuxstatus/old/bash.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 linuxstatus/old/bash.go (limited to 'linuxstatus/old/bash.go') diff --git a/linuxstatus/old/bash.go b/linuxstatus/old/bash.go new file mode 100644 index 0000000..7143c1f --- /dev/null +++ b/linuxstatus/old/bash.go @@ -0,0 +1,61 @@ +package main + +import ( + "io" + "os" + "os/exec" + "os/signal" + "syscall" + + "github.com/creack/pty" + "golang.org/x/term" + + "go.wit.com/log" +) + +func test() error { + // Create arbitrary command. + c := exec.Command("bash") + + // Start the command with a pty. + ptmx, err := pty.Start(c) + if err != nil { + return err + } + // Make sure to close the pty at the end. + defer func() { _ = ptmx.Close() }() // Best effort. + + // Handle pty size. + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGWINCH) + go func() { + for range ch { + if err := pty.InheritSize(os.Stdin, ptmx); err != nil { + log.Println("error resizing pty: %s", err) + } + } + }() + ch <- syscall.SIGWINCH // Initial resize. + defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done. + + // Set stdin in raw mode. + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort. + + // Copy stdin to the pty and the pty to stdout. + // NOTE: The goroutine will keep reading until the next keystroke before returning. + go func() { _, _ = io.Copy(ptmx, os.Stdin) }() + _, _ = io.Copy(os.Stdout, ptmx) + + return nil +} + +func mainBash() { + if err := test(); err != nil { + log.Error(err, "exit in mainBash()") + log.Exit(err) + } +} -- cgit v1.2.3