Skip to content

Instantly share code, notes, and snippets.

@kadel
Last active January 19, 2017 17:47
Show Gist options
  • Select an option

  • Save kadel/25f5cb9fb0ce21e49b83b3c8550634f9 to your computer and use it in GitHub Desktop.

Select an option

Save kadel/25f5cb9fb0ce21e49b83b3c8550634f9 to your computer and use it in GitHub Desktop.
package main
// This program takes two parameters, first is .travis.yml file and second section from travis file.
// It runs all commands from given section
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
"io/ioutil"
"gopkg.in/yaml.v2"
)
// TravisFile represents parsed travis file
// It includes only things that we are interested in
type TravisFile struct {
BeforeInstall []string `yaml:"before_install"`
Install []string `yaml:"install"`
Script []string `yaml:"script"`
}
// execCmds executes commands from list
// stops executing when one of the commands errors out
func execCmds(list []string) error {
for _, line := range list {
fmt.Printf("running: %s\n", line)
command := strings.Split(line, " ")
cmd := exec.Command(command[0], command[1:]...)
out, err := cmd.Output()
if err != nil {
return err
}
fmt.Println(string(out))
}
return nil
}
func main() {
l := log.New(os.Stderr, "", 0)
if len(os.Args) != 3 {
l.Fatalf("Usage %s <travis file> <before_install|script>", os.Args[0])
}
filename := os.Args[1]
section := os.Args[2]
data, err := ioutil.ReadFile(filename)
if err != nil {
l.Fatal(err)
}
travisFile := TravisFile{}
err = yaml.Unmarshal(data, &travisFile)
if err != nil {
l.Fatal(err)
}
switch section {
case "before_install":
err := execCmds(travisFile.BeforeInstall)
if err != nil {
l.Fatalf("command failed: %s", err)
}
case "install":
err := execCmds(travisFile.Install)
if err != nil {
l.Fatalf("command failed: %s", err)
}
case "script":
err := execCmds(travisFile.Script)
if err != nil {
l.Fatalf("command failed: %s", err)
}
default:
l.Fatal("Unknown section")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment