summaryrefslogtreecommitdiff
path: root/linuxstatus/old/bash.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-01-06 17:51:41 -0600
committerJeff Carr <[email protected]>2024-01-06 17:51:41 -0600
commit0148cec0b28d88f348b88b54ddd9dcbc7c71c823 (patch)
treeae1741261377d339779266beb862d1ef59dc2e36 /linuxstatus/old/bash.go
parent15d9f9769360b1cb8c748de8ee995030ade5eb35 (diff)
purge years of old test code
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'linuxstatus/old/bash.go')
-rw-r--r--linuxstatus/old/bash.go61
1 files changed, 61 insertions, 0 deletions
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)
+ }
+}