Go ์ธ์ด๋ก ์์ฑ๋ ๋ช ๋ น์ด ์คํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค. ์ธ๋ถ ๋ช ๋ น์ด๋ฅผ ๊ฐํธํ๊ฒ ์คํํ๊ณ ํ์ค ์ ์ถ๋ ฅ์ ์ ์ดํ ์ ์๋ ๋ํผ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
go get github.com/seungyeop-lee/easycmdpackage main
import (
"github.com/seungyeop-lee/easycmd"
)
func main() {
cmd := easycmd.New()
err := cmd.Run("echo hello world")
if err != nil {
panic(err)
}
}package main
import (
"bytes"
"fmt"
"github.com/seungyeop-lee/easycmd"
)
func main() {
out := &bytes.Buffer{}
cmd := easycmd.New(easycmd.WithStdOut(out))
err := cmd.Run("echo hello world")
if err != nil {
panic(err)
}
fmt.Println("์ถ๋ ฅ:", out.String()) // ์ถ๋ ฅ: hello world
}cmd := easycmd.New()
// bash๋ก ๋ํ๋ ๋ช
๋ น์ด ์คํ
err := cmd.RunShell("(cd .. && pwd)")
// ๋ฉํฐ๋ผ์ธ shell ์คํฌ๋ฆฝํธ ์คํ
err = cmd.RunShell(`
pwd
ls -al
`)cmd := easycmd.New()
err := cmd.RunPowershell("Get-Location")cmd := easycmd.New()
// ๊ธฐ๋ณธ ๋ช
๋ น์ด๋ฅผ ํน์ ๋๋ ํ ๋ฆฌ์์ ์คํ
err := cmd.RunWithDir("ls", "/tmp")
// Shell ๋ช
๋ น์ด๋ฅผ ํน์ ๋๋ ํ ๋ฆฌ์์ ์คํ
err = cmd.RunShellWithDir("pwd && ls", "/tmp")
// PowerShell ๋ช
๋ น์ด๋ฅผ ํน์ ๋๋ ํ ๋ฆฌ์์ ์คํ
err = cmd.RunPowershellWithDir("Get-ChildItem", "C:\\temp")import (
"os"
"strings"
"time"
)
cmd := easycmd.New(
easycmd.WithStdIn(strings.NewReader("input")), // ํ์ค ์
๋ ฅ ์ค์
easycmd.WithStdOut(os.Stdout), // ํ์ค ์ถ๋ ฅ ์ค์
easycmd.WithStdErr(os.Stderr), // ํ์ค ์๋ฌ ์ค์
easycmd.WithTimeoutSeconds(30), // ํ์์์ ์ค์ (30์ด)
easycmd.WithEnv([]string{"VAR=value"}), // ํ๊ฒฝ๋ณ์ ์ค์
)
err := cmd.Run("cat") // ํ์ค ์
๋ ฅ์์ "input"์ ์ฝ์ด์ ์ถ๋ ฅimport (
"bytes"
"fmt"
)
// ๊ธฐ๋ณธ ๋๋ฒ๊ทธ ๋ชจ๋ (๋๋ฒ๊ทธ ์ถ๋ ฅ์ stderr๋ก)
cmd := easycmd.New(easycmd.WithDebug())
err := cmd.Run("echo hello world")
// ์ปค์คํ
๋๋ฒ๊ทธ ์ถ๋ ฅ ์คํธ๋ฆผ
debugOut := &bytes.Buffer{}
cmd = easycmd.New(easycmd.WithDebug(debugOut))
err = cmd.Run("echo hello world")
fmt.Println("๋๋ฒ๊ทธ ์ถ๋ ฅ:", debugOut.String())// ๊ถ์ฅ: ์ง๊ด์ ์ธ API ์ฌ์ฉ
cmd := easycmd.New(easycmd.WithTimeoutSeconds(5)) // 5์ด
err := cmd.Run("sleep 3") // ์ ์ ์๋ฃ
cmd = easycmd.New(easycmd.WithTimeoutMillis(1500)) // 1.5์ด
err = cmd.Run("sleep 3") // 1.5์ด ํ ํ์์์
// time.Duration ์ง์ ์ฌ์ฉ๋ ๊ฐ๋ฅ
import "time"
cmd = easycmd.New(easycmd.WithTimeout(5 * time.Second))
err = cmd.Run("sleep 10") // 5์ด ํ ํ์์์
if err != nil {
fmt.Printf("๋ช
๋ น์ด ์คํ ์คํจ: %v\n", err)
}WithTimeout()์ ์ซ์๋ง ์ ๋ฌํ๋ฉด ๋๋
ธ์ด ๋จ์๋ก ํด์๋ฉ๋๋ค!
// โ ์๋ชป๋ ์ฌ์ฉ๋ฒ
cmd := easycmd.New(easycmd.WithTimeout(5)) // 5๋๋
ธ์ด (๊ฑฐ์ ์ฆ์ ํ์์์)
// โ
์ฌ๋ฐ๋ฅธ ์ฌ์ฉ๋ฒ
cmd := easycmd.New(easycmd.WithTimeoutSeconds(5)) // 5์ด
cmd := easycmd.New(easycmd.WithTimeout(5 * time.Second)) // 5์ดcmd := easycmd.New(
easycmd.WithEnv([]string{
"MY_VAR=hello",
"ANOTHER_VAR=world",
}),
)
// ํ๊ฒฝ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ์ ๋ช
๋ น์ด ์คํ
err := cmd.RunShell("echo $MY_VAR $ANOTHER_VAR")import (
"bytes"
"strings"
"time"
)
out := &bytes.Buffer{}
debugOut := &bytes.Buffer{}
input := strings.NewReader("test input\n")
cmd := easycmd.New(
easycmd.WithStdIn(input),
easycmd.WithStdOut(out),
easycmd.WithStdErr(os.Stderr),
easycmd.WithDebug(debugOut),
easycmd.WithTimeoutSeconds(10), // 10์ด ํ์์์
easycmd.WithEnv([]string{"LANG=ko_KR.UTF-8"}),
)
err := cmd.RunShell("cat && echo 'processing...' >&2")New(configApplies ...configApply) *Cmd: ์๋ก์ด Cmd ์ธ์คํด์ค ์์ฑRun(commandStr string) error: ๊ธฐ๋ณธ ๋ช ๋ น์ด ์คํRunShell(commandStr string) error: bash๋ก ๋ํ๋ ๋ช ๋ น์ด ์คํRunPowershell(commandStr string) error: PowerShell๋ก ๋ํ๋ ๋ช ๋ น์ด ์คํRunWithDir(commandStr string, runDirStr string) error: ํน์ ๋๋ ํ ๋ฆฌ์์ ๊ธฐ๋ณธ ๋ช ๋ น์ด ์คํRunShellWithDir(commandStr string, runDirStr string) error: ํน์ ๋๋ ํ ๋ฆฌ์์ Shell ๋ช ๋ น์ด ์คํRunPowershellWithDir(commandStr string, runDirStr string) error: ํน์ ๋๋ ํ ๋ฆฌ์์ PowerShell ๋ช ๋ น์ด ์คํ
WithStdIn(reader io.Reader) configApply: ํ์ค ์ ๋ ฅ ์ค์ WithStdOut(writer io.Writer) configApply: ํ์ค ์ถ๋ ฅ ์ค์ WithStdErr(writer io.Writer) configApply: ํ์ค ์๋ฌ ์ค์ WithDebug(debugOut ...io.Writer) configApply: ๋๋ฒ๊ทธ ๋ชจ๋ ํ์ฑํ ๋ฐ ๋๋ฒ๊ทธ ์ถ๋ ฅ ์คํธ๋ฆผ ์ค์ WithTimeout(timeout time.Duration) configApply: ๋ช ๋ น์ด ์คํ ํ์์์ ์ค์ (time.Duration)WithTimeoutSeconds(seconds int) configApply: ๋ช ๋ น์ด ์คํ ํ์์์ ์ค์ (์ด ๋จ์) โญ ๊ถ์ฅWithTimeoutMillis(millis int) configApply: ๋ช ๋ น์ด ์คํ ํ์์์ ์ค์ (๋ฐ๋ฆฌ์ด ๋จ์) โญ ๊ถ์ฅWithEnv(env []string) configApply: ํ๊ฒฝ๋ณ์ ์ค์
๋๋ฒ๊ทธ ๋ชจ๋๊ฐ ํ์ฑํ๋๋ฉด ๋ค์ ์ ๋ณด๋ค์ด ์ถ๋ ฅ๋ฉ๋๋ค:
- ํ์ฑ๋ ๋ช ๋ น์ด ๋ฌธ์์ด
- ์คํ๋ ๋ช ๋ น์ด ์ด๋ฆ
- ๋ช ๋ น์ด ์ธ์ ๋ฐฐ์ด
- ์คํ ๋๋ ํ ๋ฆฌ (์ค์ ๋ ๊ฒฝ์ฐ)
- ํ์์์ ์ค์ (์ค์ ๋ ๊ฒฝ์ฐ)
- ํ๊ฒฝ๋ณ์ ๊ฐ์ (์ค์ ๋ ๊ฒฝ์ฐ)
- ๋ช ๋ น์ด ์คํ ์์/์๋ฃ/์คํจ ๋ฉ์์ง
- ๋ช ๋ น์ด ์คํ ์๊ฐ ์ธก์
cmd := easycmd.New()
err := cmd.Run("invalid-command")
if err != nil {
fmt.Printf("๋ช
๋ น์ด ์คํ ์คํจ: %v\n", err)
}
// ๋น ๋ช
๋ น์ด์ ๋ํ ํน๋ณํ ์๋ฌ
if errors.Is(err, easycmd.EmptyCmdError) {
fmt.Println("๋น ๋ช
๋ น์ด์
๋๋ค")
}ํ์์์ ์ค์ ์ ๋ฐ์ํ ์ ์๋ ๋ ๊ฐ์ง ์๋ฌ ์ ํ:
cmd := easycmd.New(easycmd.WithTimeoutMillis(1)) // ๋งค์ฐ ์งง์ ํ์์์
err := cmd.Run("sleep 1")
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "๋ช
๋ น์ด ์์ ์คํจ") {
fmt.Println("๋ช
๋ น์ด ์์ ์ ํ์์์ ๋ฐ์")
} else if strings.Contains(errMsg, "๋ช
๋ น์ด ์คํ ํ์์์") {
fmt.Println("๋ช
๋ น์ด ์คํ ์ค ํ์์์ ๋ฐ์")
}
}์ด ํ๋ก์ ํธ๋ Apache License 2.0 ํ์ ๋ฐฐํฌ๋ฉ๋๋ค. ์์ธํ ๋ด์ฉ์ LICENSE ํ์ผ์ ์ฐธ์กฐํ์ธ์.