项目作者: sebastianwebber

项目描述 :
golang helpers to call OS commands
高级语言: Go
项目地址: git://github.com/sebastianwebber/cmdr.git
创建时间: 2018-01-14T17:20:01Z
项目社区:https://github.com/sebastianwebber/cmdr

开源协议:

下载


cmdr

Build Status Go Report Card codecov
FOSSA Status

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:

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/sebastianwebber/cmdr"
  5. )
  6. func main() {
  7. // *** short version ***********
  8. out, err := cmdr.New(true, "ls", "-lh", "~/tmp2/*").Run()
  9. fmt.Println("Output:", string(out))
  10. if err != nil {
  11. fmt.Println("OOPS:", err.Error())
  12. }
  13. // *** verbose version ***********
  14. // New is a helper to create a Command
  15. // You can call it by a shell like bash if you want (useful to process expressions like *)
  16. cmd := cmdr.New(true, "ls", "-lh", "~/tmp/*")
  17. // You can declare the variable as well:
  18. // cmd := cmdr.Command{ }
  19. // You can also parse a command into a Command:
  20. // cmd := cmdr.Parse(`psql -At -c 'select now();'`)
  21. // Enable timeout if you want (5s by example)
  22. cmd.Options.Timeout = 5
  23. // To check if the inputed command is valid, use the IsValid function.
  24. // It checks if the command exists in PATH
  25. if cmd.IsValid() {
  26. // To execute the command, just call the Run function
  27. out, err := cmd.Run()
  28. if err != nil {
  29. panic(err)
  30. }
  31. // here comes the output
  32. fmt.Println(string(out))
  33. }
  34. }

Grouping commands

Its possible to group a list of commands:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/sebastianwebber/cmdr"
  5. )
  6. func main() {
  7. // Group options (experimental)
  8. total, err := cmdr.Group(
  9. cmdr.AbortOnError,
  10. cmdr.New(false, "ls", "-lh"),
  11. cmdr.New(false, "pwd 123q6236"),
  12. cmdr.New(false, "cat", "/etc/hosts"),
  13. )
  14. fmt.Printf("%d commands executed without error. \n", total)
  15. if err != nil {
  16. fmt.Printf("Houston, we have a problem! %v\n", err)
  17. }
  18. }

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

License

FOSSA Status