drrvnbwle80177811 2019-07-18 07:23
浏览 198
已采纳

从exec.Command写入文件

I am trying to write a file from a bash command into a file in Go. Note there are several reasons for using Go over bash here: I have some more logic such as parsing configuration files, I would like to run that code for multiple DBs in parallele and finally performing some more complex data manipulation after.

    dumpStr := fmt.Sprintf("pg_dump -U %s -h %s %s | gzip", DbUserName, DbHost, DbName)
    cmd := exec.Command("bash", "-c", dumpStr)
    cmd.Env = append(cmd.Env, "PGPASSWORD="+DbPassword)

    outfile, err := os.Create(DbName + ".gz")
    if err != nil {
        panic(err)
    }
    outfile = cmd.Stdout
    defer outfile.Close()
    err = cmd.Start()
    if err != nil {
        panic(err)
    }
    cmd.Wait()

However, I am getting an emtpy result. I am getting data if I am executing dumpStr from the CLI but not from that code... What am I missing?

  • 写回答

2条回答 默认 最新

  • doumu9799 2019-07-18 07:37
    关注

    As Flimzy said, you're not capturing the output of pg_dump. You can do that with Go, or you can use pg_dump-s --file. It can also compress with --compress so no need to pipe to gzip. Then there's no need for bash and you can avoid shell quoting issues.

    cmd := exec.Command(
        "pg_dump",
        "--compress=9",
        "--file="+DbName + ".gz",
        "-U"+DbUserName,
        "-h"+DbHost,
        DbName,
    )
    
    log.Print("Running pg_dump...")
    
    if err := cmd.Run(); err != nil {
        log.Fatal(err)
    }
    

    Much simpler and more secure.


    For illustration here's how you'd do it all in Go.

    Use Cmd.StdoutPipe to get an open IO reader to pg_dump's stdout. Then use io.Copy to copy from stdout to your open file.

    @Peter points out that since Cmd.Stdout is an io.Reader it's simpler to assign the open file to cmd.Stdout and let cmd write to it directly.

    // Same as above, but no --file.
    cmd := exec.Command(
        "pg_dump",
        "--compress=9",
        "-U"+DbUserName,
        "-h"+DbHost,
        DbName,
    )
    
    // Open the output file
    outfile, err := os.Create(DbName + ".gz")
    if err != nil {
        log.Fatal(err)
    }
    defer outfile.Close()
    
    // Send stdout to the outfile. cmd.Stdout will take any io.Writer.
    cmd.Stdout = outfile
    
    // Start the command
    if err = cmd.Start(); err != nil {
        log.Fatal(err)
    }
    
    log.Print("Waiting for command to finish...")
    
    // Wait for the command to finish.
    if err = cmd.Wait(); err != nil {
        log.Fatal(err)
    }
    

    In addition, you're only checking if the command started, not if it successfully ran.

    From the docs for Cmd.Start.

    Start starts the specified command but does not wait for it to complete.

    The Wait method will return the exit code and release associated resources once the command exits.

    You're checking cmd.Start for an error, but not cmd.Wait. Checking the error from cmd.Start only means the command started. If there is an error while the program is running you won't know what it is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?