Exemplo n.º 1
0
// Run the goshim
func Run(args []string) int {
	if len(args) < 1 || (len(args) == 1 && helpReg.MatchString(args[0])) {
		printUsage()
		return 1
	}
	subcmdOrSrcdir := args[0]

	// no subcommands for now
	srcdir := subcmdOrSrcdir
	fi, err := os.Stat(srcdir)
	if err != nil || !fi.IsDir() {
		log.Fatalf("not a directory: %s", srcdir)
		return 1
	}
	list, err := srcList(srcdir)
	if err != nil {
		log.Fatal(err)
		return 1
	}
	dst := binDst(srcdir)
	if isRebuildRequired(srcdir, dst, list) {
		err := build(srcdir, dst)
		if err != nil {
			log.Fatal(err)
			return 1
		}
	}
	err = execFunc(dst, append([]string{dst}, args[1:]...), os.Environ())
	return wrapcommander.ResolveExitCode(err)
}
Exemplo n.º 2
0
func run(args []string) int {
	o, err := parseArgs(args)
	if err != nil {
		if err.Error() != "" {
			fmt.Fprintln(os.Stderr, err)
		}
		writeHelp()
		return 2
	}
	err = o.run()
	return wrapcommander.ResolveExitCode(err)
}
Exemplo n.º 3
0
// Run the horenso
func Run(args []string) int {
	p, o, cmdArgs, err := parseArgs(args)
	if err != nil || len(cmdArgs) < 1 {
		if ferr, ok := err.(*flags.Error); !ok || ferr.Type != flags.ErrHelp {
			p.WriteHelp(os.Stderr)
		}
		return 2
	}
	r, err := o.run(cmdArgs)
	if err != nil {
		return wrapcommander.ResolveExitCode(err)
	}
	return *r.ExitCode
}
Exemplo n.º 4
0
func (o *opts) run(args []string) (Report, error) {
	hostname, _ := os.Hostname()
	r := Report{
		Command:     shellquote.Join(args...),
		CommandArgs: args,
		Tag:         o.Tag,
		Hostname:    hostname,
	}
	cmd := exec.Command(args[0], args[1:]...)

	stdoutPipe, err := cmd.StdoutPipe()
	if err != nil {
		return o.failReport(r, err.Error()), err
	}
	stderrPipe, err := cmd.StderrPipe()
	if err != nil {
		stdoutPipe.Close()
		return o.failReport(r, err.Error()), err
	}

	var bufStdout bytes.Buffer
	var bufStderr bytes.Buffer
	var bufMerged bytes.Buffer

	var wtr io.Writer = &bufMerged
	if o.TimeStamp {
		wtr = newTimestampWriter(&bufMerged)
	}
	stdoutPipe2 := io.TeeReader(stdoutPipe, io.MultiWriter(&bufStdout, wtr))
	stderrPipe2 := io.TeeReader(stderrPipe, io.MultiWriter(&bufStderr, wtr))

	r.StartAt = now()
	err = cmd.Start()
	if err != nil {
		stderrPipe.Close()
		stdoutPipe.Close()
		return o.failReport(r, err.Error()), err
	}
	if cmd.Process != nil {
		r.Pid = &cmd.Process.Pid
	}
	done := make(chan struct{})
	go func() {
		o.runNoticer(r)
		done <- struct{}{}
	}()

	go func() {
		defer stdoutPipe.Close()
		io.Copy(os.Stdout, stdoutPipe2)
	}()

	go func() {
		defer stderrPipe.Close()
		io.Copy(os.Stderr, stderrPipe2)
	}()

	err = cmd.Wait()
	r.EndAt = now()
	ex := wrapcommander.ResolveExitCode(err)
	r.ExitCode = &ex
	r.Result = fmt.Sprintf("command exited with code: %d", *r.ExitCode)
	if *r.ExitCode > 128 {
		r.Result = fmt.Sprintf("command died with signal: %d", *r.ExitCode&127)
	}
	r.Stdout = bufStdout.String()
	r.Stderr = bufStderr.String()
	r.Output = bufMerged.String()
	if p := cmd.ProcessState; p != nil {
		durPtr := func(t time.Duration) *float64 {
			f := float64(t) / float64(time.Second)
			return &f
		}
		r.UserTime = durPtr(p.UserTime())
		r.SystemTime = durPtr(p.SystemTime())
	}
	o.runReporter(r)
	<-done

	return r, nil
}
Exemplo n.º 5
0
func main() {
	defaultFlags := declareFlags("")
	outFlags := declareFlags("out")
	errFlags := declareFlags("err")
	force := flag.Bool("force", false, "Force colorizing.")
	flag.Parse()

	if *outFlags.prefix == "" {
		*outFlags.prefix = *defaultFlags.prefix
	}
	if *errFlags.prefix == "" {
		*errFlags.prefix = *defaultFlags.prefix
	}

	if *defaultFlags.pattern == "" {
		*defaultFlags.pattern = ".*"
	}
	if *outFlags.pattern == "" {
		*outFlags.pattern = *defaultFlags.pattern
	}
	if *errFlags.pattern == "" {
		*errFlags.pattern = *defaultFlags.pattern
	}

	args := flag.Args()
	if len(args) <= 0 {
		fatal(1, "No command specified")
	}

	cmd := exec.Command(args[0], args[1:]...)

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		fatal(2, err.Error())
	}

	stderr, err := cmd.StderrPipe()
	if err != nil {
		stdout.Close()
		fatal(2, err.Error())
	}

	sig := make(chan os.Signal, 1)
	signal.Notify(sig,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGTERM,
		syscall.SIGQUIT,
	)
	go func() {
		for {
			if s, ok := (<-sig).(syscall.Signal); ok {
				if cmd.Process == nil {
					os.Exit(int(s) | 0x80)
				}
				syscall.Kill(cmd.Process.Pid, s)
			} else {
				if cmd.Process == nil {
					os.Exit(1)
				}
				cmd.Process.Kill()
			}
		}
	}()

	err = cmd.Start()
	if err != nil {
		stdout.Close()
		stderr.Close()
		fatal(3, err.Error())
	}

	outColor := outFlags.ApplyTo(defaultFlags.ToColor())
	if *force {
		outColor.Force()
	}

	outPipe, err := newPipe(*outFlags.prefix, *outFlags.pattern, outColor)
	if err != nil {
		stdout.Close()
		stderr.Close()
		fatal(4, err.Error())
	}

	errColor := errFlags.ApplyTo(defaultFlags.ToColor())
	if *force {
		errColor.Force()
	}

	errPipe, err := newPipe(*errFlags.prefix, *errFlags.pattern, errColor)
	if err != nil {
		stdout.Close()
		stderr.Close()
		fatal(4, err.Error())
	}

	outDone := make(chan struct{})
	go func() {
		defer stdout.Close()
		output := colorable.NewColorableStdout()
		if err := outPipe.Copy(output, stdout); err != nil {
			fmt.Fprintln(os.Stderr, err.Error())
		}
		outDone <- struct{}{}
	}()

	errDone := make(chan struct{})
	go func() {
		defer stderr.Close()
		output := colorable.NewColorableStderr()
		if err := errPipe.Copy(output, stderr); err != nil {
			fmt.Fprintln(os.Stderr, err.Error())
		}
		errDone <- struct{}{}
	}()

	<-outDone
	<-errDone
	err = cmd.Wait()

	os.Exit(wrapcommander.ResolveExitCode(err))
}