Code Monkey home page Code Monkey logo

cmdr's Introduction

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

First things first:

go get -u -v go get github.com/sebastianwebber/cmdr

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

package main

import (
	"fmt"

	"github.com/sebastianwebber/cmdr"
)

func main() {
    // *** short version ***********
	out, err := cmdr.New(true, "ls", "-lh", "~/tmp2/*").Run()
	fmt.Println("Output:", string(out))
	if err != nil {
		fmt.Println("OOPS:", err.Error())
    }

    // *** verbose version ***********
	// 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{  }

	// You can also parse a command into a Command:
	// cmd := cmdr.Parse(`psql -At -c 'select now();'`)

	// 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

cmdr's People

Contributors

jg19 avatar nbluis avatar sebastianwebber avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.