exec

package module
v0.0.0-...-ae93307 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2025 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/exec"
)

func main() {
	o, _, c := exec.Simple("echo '!olleH' | rev")
	if c != 0 {
		log.Fatal("Unexpected failure.")
	}

	fmt.Println(o)
}
Output:

Hello!

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Verbosity com.Level

	// Timeout can be disabled by setting it to 0
	Timeout time.Duration = time.Second * 20
)

Functions

func Fork

func Fork(cmd string, attr *syscall.ProcAttr) (int, error)

Fork takes your cmd and returns the pid of the started process.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/exec"
)

func main() {
	pid, err := exec.Fork("sleep 1", nil)
	if err != nil {
		log.Fatal("Unexpected failure.")
	}

	if pid > 1 {
		fmt.Println("success")
	} else {
		fmt.Println("failure")
	}

}
Output:

success

func Run

func Run(cmdline string) ([]string, []string, []int, error)

Run takes your cmdline and executes it.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/exec"
)

func main() {
	stdout, stderr, codes, err := exec.Run(`echo "Hello world!"; python3 -c 'import sys; print("error", file=sys.stderr)' && false || echo "success"`)
	if err != nil {
		log.Fatal(err)
	}

	for n, code := range codes {
		// skip the third command (false) that returns 1
		if code != 0 && n != 2 {
			log.Fatal(stderr[n])
		}
	}

	// the python command prints error to stderr
	if codes[1] == 0 {
		if stderr[1] != "error\n" {
			log.Fatal("Stderr output missing.")
		}
	}

	fmt.Println(stdout[0])
}
Output:

Hello world!

func RunWithTokens

func RunWithTokens(cmdline []string) ([]string, []string, []int, error)

RunWithTokens takes your cmdline tokens and does not try to tokenize before executing it.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/exec"
)

func main() {
	stdout, stderr, codes, err := exec.RunWithTokens([]string{"echo", "Hello world!", ";", "python3", "-c", `'import sys; print("error", file=sys.stderr)'`, "&&", "false", "||", "echo", "success"})
	if err != nil {
		log.Fatal(err)
	}

	for n, code := range codes {
		// skip the third command (false) that returns 1
		if code != 0 && n != 2 {
			log.Fatal(stderr[n])
		}
	}

	// the python command prints error to stderr
	if codes[1] == 0 {
		if stderr[1] != "error\n" {
			log.Fatal("Stderr output missing.")
		}
	}

	fmt.Println(stdout[0])
}
Output:

Hello world!

func Simple

func Simple(cmdline string) (string, string, int)

Simple takes your cmdline and executes it.

Example
package main

import (
	"fmt"
	"log"

	"catinello.eu/exec"
)

func main() {
	stdout, stderr, code := exec.Simple(`echo "Hello world!"; python3 -c 'import sys; print("error", file=sys.stderr)' && false || echo "success"`)
	if code == -1 {
		log.Fatal(stderr)
	}

	if code > 0 {
		log.Fatal("Unexpected failure.")
	}

	if code == 0 && stderr != "error\n" {
		log.Fatal("Stderr output missing.")
	}

	fmt.Println(stdout)
}
Output:

Hello world!
success

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL