Categorygithub.com/sourcegraph/run
modulepackage
0.12.0
Repository: https://github.com/sourcegraph/run.git
Documentation: pkg.go.dev

# README

🏃‍♂️ run

Go Reference Tests Coverage

A new way to execute commands in Go

Example usage

package main

import (
  "bytes"
  "context"
  "fmt"
  "io"
  "log"
  "os"

  "github.com/sourcegraph/run"
)

func main() {
  ctx := context.Background()

  // Easily stream all output back to standard out
  err := run.Cmd(ctx, "echo", "hello world").Run().Stream(os.Stdout)
  if err != nil {
    log.Fatal(err.Error())
  }

  // Or collect, map, and modify output, then collect string lines from it
  lines, err := run.Cmd(ctx, "ls").Run().
    Map(func(ctx context.Context, line []byte, dst io.Writer) (int, error) {
      if !bytes.HasSuffix(line, []byte(".go")) {
        return 0, nil
      }
      return dst.Write(bytes.TrimSuffix(line, []byte(".go")))
    }).
    Lines()
  if err != nil {
    log.Fatal(err.Error())
  }
  for i, l := range lines {
    fmt.Printf("line %d: %q\n", i, l)
  }

  // Errors include standard error by default, so we can just stream stdout.
  err = run.Cmd(ctx, "ls", "foobar").StdOut().Run().Stream(os.Stdout)
  if err != nil {
    println(err.Error()) // exit status 1: ls: foobar: No such file or directory
  }

  // Generate data from a file, replacing tabs with spaces for Markdown purposes
  var exampleData bytes.Buffer
  exampleData.Write([]byte(exampleStart + "\n\n```go\n"))
  if err = run.Cmd(ctx, "cat", "cmd/example/main.go").Run().
    Map(func(ctx context.Context, line []byte, dst io.Writer) (int, error) {
      return dst.Write(bytes.ReplaceAll(line, []byte("\t"), []byte("  ")))
    }).
    Stream(&exampleData); err != nil {
    log.Fatal(err)
  }
  exampleData.Write([]byte("```\n\n" + exampleEnd))

  // Render new README file
  var readmeData bytes.Buffer
  if err = run.Cmd(ctx, "cat", "README.md").Run().Stream(&readmeData); err != nil {
    log.Fatal(err)
  }
  replaced := exampleBlockRegexp.ReplaceAll(readmeData.Bytes(), exampleData.Bytes())

  // Pipe data to command
  err = run.Cmd(ctx, "cp /dev/stdin README.md").Input(bytes.NewReader(replaced)).Run().Wait()
  if err != nil {
    log.Fatal(err)
  }
}

# Packages

No description provided by the author

# Functions

Arg quotes a value such that it gets treated as an argument by a command.
Bash joins all the parts and builds a command from it to be run by 'bash -c'.
BashWith appends all the given bash options to the bash command with '-o'.
Cmd joins all the parts and builds a command from it.
DefaultTraceAttributes adds Args and Dir as attributes.
ExitCode returns the exit code associated with err if there is one, otherwise 1.
LogCommands enables logging on all commands executed by sourcegraph/run within this context.
MapJQ creates a LineMap that executes a JQ query against each line and replaces the output with the result.
NewErrorOutput creates an Output that just returns error.
TraceCommands toggles OpenTelemetry tracing on all usages of sourcegraph/run within this context.

# Constants

'errexit' lets bash exit with an err exit code if a command fails .
'pipefail' instructs bash to fail an entire statement if any command in a pipefails.

# Variables

StrictBashOpts contains options that effectively enforce safe execution of bash commands.

# Structs

Command builds a command for execution.
ExecutedCommand represents a command that has been started.

# Interfaces

ExitCoder is an error that also denotes an exit code to exit with.
Output configures output and aggregation from a command.

# Type aliases

BashOpt denotes options for running bash commands.
LineMap allows modifications of individual lines from Output and enables callbacks that operate on lines from Output.
LogFunc can be used to generate a log entry for the executed command.
TraceAttributesFunc can be used to generate attributes to attach to a span for the executed command.