1
0

More than 1 year has passed since last update.

Go言語で外部コマンド実行するときの、標準出力取得するorしない、panicするorしない、の実装まとめ

Posted at

Go言語で外部コマンド実行するときに、標準出力取得するorしない、panicするorしない、という以下の4パターンを使うので、それぞれの実装をまとめました

  1. 外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はpanicする
  2. 外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はerrorを返す
  3. 外部コマンドを実行し、標準出力を返す。エラーの場合はpanicする
  4. 外部コマンドを実行し、標準出力とエラーを返す

ソース

package main

import (
    "fmt"
    "os"
    "os/exec"
    "strings"
)

func runCommandGetOutputWithoutPanicByArray(command string, args ...string) (string, error) {
    stdout, err := exec.Command(command, args...).CombinedOutput()
    return string(stdout), err
}

func runCommandByArrayWithoutPanic(command string, args ...string) error {
    cmd := exec.Command(command, args...)
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    return cmd.Run()
}

// 1.外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はpanicする
func RunCommand(command string) {
    var tokens = strings.Split(command, " ")
    c := tokens[0]
    args := tokens[1:]
    err := runCommandByArrayWithoutPanic(c, args...)
    if err != nil {
        panic(err)
    }
}

// 2.外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はerrorを返す
func RunCommandWithoutPanic(command string) error {
    var tokens = strings.Split(command, " ")
    c := tokens[0]
    args := tokens[1:]
    return runCommandByArrayWithoutPanic(c, args...)
}

// 3.外部コマンドを実行し、標準出力を返す。エラーの場合はpanicする
func RunCommandGetOutput(command string) string {
    var tokens = strings.Split(command, " ")
    c := tokens[0]
    args := tokens[1:]
    stdout, err := runCommandGetOutputWithoutPanicByArray(c, args...)
    if err != nil {
        panic(err)
    }
    return stdout
}

// 4.外部コマンドを実行し、標準出力とエラーを返す
func RunCommandGetOutputWithoutPanic(command string) (string, error) {
    var tokens = strings.Split(command, " ")
    c := tokens[0]
    args := tokens[1:]
    return runCommandGetOutputWithoutPanicByArray(c, args...)
}

使い方

func main() {
    fmt.Println("1.外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はpanicする")
    RunCommand("echo hoge")

    fmt.Println("2.外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はerrorを返す")
    err := RunCommandWithoutPanic("does_not_exist_command")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("3.外部コマンドを実行し、標準出力を返す。エラーの場合はpanicする")
    stdout := RunCommandGetOutput("echo hoge")
    fmt.Println(stdout)

    fmt.Println("4.外部コマンドを実行し、標準出力とエラーを返す")
    stdout2, err2 := RunCommandGetOutputWithoutPanic("does_not_exist_command")
    if err2 != nil {
        fmt.Println(err2)
    } else {
        fmt.Println(stdout2)
    }
}

実行結果

1.外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はpanicする
hoge
2.外部コマンドを実行し、標準出力と標準エラーを印字し、エラーの場合はerrorを返す
exec: "does_not_exist_command": executable file not found in $PATH
3.外部コマンドを実行し、標準出力を返す。エラーの場合はpanicする
hoge

4.外部コマンドを実行し、標準出力とエラーを返す
exec: "does_not_exist_command": executable file not found in $PATH
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0