├── readme.md ├── .travis.yml ├── license ├── runcmd.go └── patents /readme.md: -------------------------------------------------------------------------------- 1 | runcmd [![Build Status](https://secure.travis-ci.org/facebookgo/runcmd.png)](https://travis-ci.org/facebookgo/runcmd) 2 | ====== 3 | 4 | Documentation: https://godoc.org/github.com/facebookgo/runcmd 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | 3 | go: 4 | - 1.2 5 | - 1.3 6 | 7 | matrix: 8 | fast_finish: true 9 | 10 | before_install: 11 | - go get -v code.google.com/p/go.tools/cmd/vet 12 | - go get -v github.com/golang/lint/golint 13 | - go get -v code.google.com/p/go.tools/cmd/cover 14 | 15 | install: 16 | - go install -race -v std 17 | - go get -race -t -v ./... 18 | - go install -race -v ./... 19 | 20 | script: 21 | - go vet ./... 22 | - $HOME/gopath/bin/golint . 23 | - go test -cpu=2 -race -v ./... 24 | - go test -cpu=2 -covermode=atomic ./... 25 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | BSD License 2 | 3 | For runcmd software 4 | 5 | Copyright (c) 2015, Facebook, Inc. All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | * Neither the name Facebook nor the names of its contributors may be used to 18 | endorse or promote products derived from this software without specific 19 | prior written permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /runcmd.go: -------------------------------------------------------------------------------- 1 | // Package runcmd provides a convenience Run function for exec.Cmd which 2 | // includes the original command along with the output and error streams. 3 | package runcmd 4 | 5 | import ( 6 | "bytes" 7 | "fmt" 8 | "os/exec" 9 | "strings" 10 | ) 11 | 12 | // CommandError is returned by run and provides an error string which includes 13 | // the full command and the contents of the output and error streams. 14 | type CommandError struct { 15 | fullCommand string 16 | streams *Streams 17 | } 18 | 19 | // Error provides a helpful error text. 20 | func (e *CommandError) Error() string { 21 | return fmt.Sprintf( 22 | "error executing: %s:\n%s\n%s", 23 | e.fullCommand, 24 | e.streams.Stderr().Bytes(), 25 | bytes.TrimSpace(e.streams.Stdout().Bytes()), 26 | ) 27 | } 28 | 29 | // Streams provides access to the output and error buffers. 30 | type Streams struct { 31 | out *bytes.Buffer 32 | err *bytes.Buffer 33 | } 34 | 35 | // Stderr returns the underlying buffer with the contents of the error stream. 36 | func (s *Streams) Stderr() *bytes.Buffer { 37 | return s.err 38 | } 39 | 40 | // Stdout returns the underlying buffer with the contents of the output stream. 41 | func (s *Streams) Stdout() *bytes.Buffer { 42 | return s.out 43 | } 44 | 45 | // Run the command and return the associated streams, and error if any. The 46 | // error may be a CommandError. 47 | func Run(cmd *exec.Cmd) (*Streams, error) { 48 | var bout, berr bytes.Buffer 49 | streams := &Streams{ 50 | out: &bout, 51 | err: &berr, 52 | } 53 | cmd.Stdout = &bout 54 | cmd.Stderr = &berr 55 | if err := cmd.Run(); err != nil { 56 | return streams, &CommandError{ 57 | fullCommand: cmd.Path + " " + strings.Join(cmd.Args, " "), 58 | streams: streams, 59 | } 60 | } 61 | return streams, nil 62 | } 63 | -------------------------------------------------------------------------------- /patents: -------------------------------------------------------------------------------- 1 | Additional Grant of Patent Rights Version 2 2 | 3 | "Software" means the runcmd software distributed by Facebook, Inc. 4 | 5 | Facebook, Inc. ("Facebook") hereby grants to each recipient of the Software 6 | ("you") a perpetual, worldwide, royalty-free, non-exclusive, irrevocable 7 | (subject to the termination provision below) license under any Necessary 8 | Claims, to make, have made, use, sell, offer to sell, import, and otherwise 9 | transfer the Software. For avoidance of doubt, no license is granted under 10 | Facebook’s rights in any patent claims that are infringed by (i) modifications 11 | to the Software made by you or any third party or (ii) the Software in 12 | combination with any software or other technology. 13 | 14 | The license granted hereunder will terminate, automatically and without notice, 15 | if you (or any of your subsidiaries, corporate affiliates or agents) initiate 16 | directly or indirectly, or take a direct financial interest in, any Patent 17 | Assertion: (i) against Facebook or any of its subsidiaries or corporate 18 | affiliates, (ii) against any party if such Patent Assertion arises in whole or 19 | in part from any software, technology, product or service of Facebook or any of 20 | its subsidiaries or corporate affiliates, or (iii) against any party relating 21 | to the Software. Notwithstanding the foregoing, if Facebook or any of its 22 | subsidiaries or corporate affiliates files a lawsuit alleging patent 23 | infringement against you in the first instance, and you respond by filing a 24 | patent infringement counterclaim in that lawsuit against that party that is 25 | unrelated to the Software, the license granted hereunder will not terminate 26 | under section (i) of this paragraph due to such counterclaim. 27 | 28 | A "Necessary Claim" is a claim of a patent owned by Facebook that is 29 | necessarily infringed by the Software standing alone. 30 | 31 | A "Patent Assertion" is any lawsuit or other action alleging direct, indirect, 32 | or contributory infringement or inducement to infringe any patent, including a 33 | cross-claim or counterclaim. 34 | --------------------------------------------------------------------------------