Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gyzszabo opened this issue Mar 22, 2023 · 0 comments

Comments

@gyzszabo
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant