chzyer / readline

Readline is a pure go(golang) implementation for GNU-Readline kind library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example how to use library with old UNIX CLI tools like Oracle sqlplus or rman

gyzszabo opened this issue · comments

In our environment, it is not possible to install realine and rlwrap tools which make our life easier to use Oracle CLI tools like rman and sqlplus.
Your project is very promising. I am looking for an example of how to communicate this library with rman tool interactive way. Unfortunately, I am a beginner in golang and that's the reason my example does not work. Something like this:
package main

import (
    "bufio"
    "fmt"
    "io"
    "os/exec"

    "github.com/chzyer/readline"
)

func main() {
    // Launch RMAN in interactive mode
    rmanCmd := exec.Command("rman", "target", "/")
    rmanStdin, err := rmanCmd.StdinPipe()
    if err != nil {
        panic(err)
    }
    rmanStdout, err := rmanCmd.StdoutPipe()
    if err != nil {
        panic(err)
    }
    rmanStderr, err := rmanCmd.StderrPipe()
    if err != nil {
        panic(err)
    }
    
    // Merge stdout and stderr pipes
    multiReader := io.MultiReader(rmanStdout, rmanStderr)

    scanner := bufio.NewScanner(multiReader)
    if err := rmanCmd.Start(); err != nil {
        panic(err)
    }

    // Create a new readline instance
    rl, err := readline.NewEx(&readline.Config{
        Prompt:            "RMAN> ",
        HistoryFile:       "/tmp/readline.tmp",
        HistorySearchFold: true,
    })
    if err != nil {
        panic(err)
    }
    defer rl.Close()
    rl.CaptureExitSignal()

    if err != nil {
        panic(err)
    }

    // Loop over readline commands until user enters "quit"
    for {
        // Read a line of input from the user
        line, err := rl.Readline()
        if err != nil {
            fmt.Println("error:", err)
            break
        }

        // Write the command to the RMAN process
        fmt.Fprintln(rmanStdin, line)

        // Read the output from the RMAN process
        for scanner.Scan() {
            fmt.Println(scanner.Text())
        }

        if line == "quit" {
            break
        }
    }

    // Wait for the RMAN process to exit
    if err := rmanCmd.Wait(); err != nil {
        panic(err)
    }
}

So any suggestion or advice is welcome on how to use their library, I guess first I should move to check if the terminal is interactive or not.