nbluis / cmdr

golang helpers to call OS commands

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cmdr

Build Status Go Report Card codecov

cmdr (pronounced "commander") is a go package to abstract and simplify execution of commands on the operation system.

how to use it

Basically create a Command and call the Run function. Take a look:

package main

import (
    "fmt"

    "github.com/sebastianwebber/cmdr"
)

func main() {
    
    // New is a helper to create a Command
    // You can call it by a shell like bash if you want (useful to process expressions like *)
    cmd := cmdr.New(true, "ls", "-lh", "~/tmp/*")
    
    // You can declare the variable as well:
    // cmd := cmdr.Command{  }

    // Enable timeout if you want (5s by example)
    cmd.Options.Timeout = 5


    // To check if the inputed command is valid, use the IsValid function.
    // It checks if the command exists in PATH
    if cmd.IsValid() {

        // To execute the command, just call the Run function
        out, err := cmd.Run()
        if err != nil {
            panic(err)
        }
        
        // here comes the output
        fmt.Println(string(out))
    }
}

Grouping commands

Its possible to group a list of commands:

package main

import (
    "fmt"

    "github.com/sebastianwebber/cmdr"
)

func main() {
    // Group options (experimental)
    total, err := cmdr.Group(
        cmdr.AbortOnError,
        cmdr.New(false, "ls", "-lh"),
        cmdr.New(false, "pwd 123q6236"),
        cmdr.New(false, "cat", "/etc/hosts"),
    )
    fmt.Printf("%d commands executed without error. \n", total)

    if err != nil {
        fmt.Printf("Houston, we have a problem! %v\n", err)
    }
}

This is a work in progress.

TODO List

  • Add option to timeout
  • Enable way to group commands
  • Print output of each command in the group (perhaps adding a name option?)
  • Pipe support
  • add support por multiple commands

About

golang helpers to call OS commands


Languages

Language:Go 100.0%