summaryrefslogtreecommitdiff
path: root/exec.go
blob: 9865d86e6c65175290ba23384fa13b6afb2d19ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main

import (
	"bufio"
	"fmt"
	"log"
	"net/url"
	"os"
	"os/exec"
)

func exec_local_file(cmd_url *url.URL, args, env []string) error {

	cmd := exec.Command(cmd_url.Path, args...)
	cmd.Env = env
	stdout, err := cmd.StdoutPipe()

	if err != nil {
		return nil
	}

	if err == nil {
		if err = cmd.Start(); err == nil {
			buff := bufio.NewScanner(stdout)
			log.Printf("[%s - stout follows: ]\n", cmd.Path)
			for buff.Scan() {
				log.Printf(buff.Text()) // write each line to your log, or anything you need
			}
			err = cmd.Wait()
		}
	}
	return err
}

func exec_url(cmd_url *url.URL, args, env []string) error {

	return nil
}

func exec_tag(tag *SCORSHtag_cfg, args []string, env []string) []error {

	var ret []error

	for _, c := range tag.Commands {
		debug.log("[tag: %s] attempting command: %s\n", tag.Name, c.URL)
		cmd_url, err := url.Parse(c.URL)
		if err != nil {
			log.Printf("[tag: %s] error parsing URL: %s", tag.Name, err)
		} else {
			if cmd_url.Scheme == "file" {
				//if err = check_hash(cmd_url, c.Hash); err == nil {
				err = exec_local_file(cmd_url, args, env)
				//} else {
				//log.Printf("[tag: %s] WARNING!!! HASH MISMATCH FOR %s\n", cmd_url)
				//}
			} else if cmd_url.Scheme == "http" || cmd_url.Scheme == "https" {
				err = exec_url(cmd_url, args, env)
			}
		}
		ret = append(ret, err)
	}
	return ret
}

func set_environment(msg *SCORSHmsg, tag, author, committer string) []string {

	env := os.Environ()
	env = append(env, fmt.Sprintf("SCORSH_REPO=%s", msg.Repo))
	env = append(env, fmt.Sprintf("SCORSH_BRANCH=%s", msg.Branch))
	env = append(env, fmt.Sprintf("SCORSH_OLDREV=%s", msg.Old_rev))
	env = append(env, fmt.Sprintf("SCORSH_NEWREV=%s", msg.New_rev))
	env = append(env, fmt.Sprintf("SCORSH_ID=%s", msg.Id))
	env = append(env, fmt.Sprintf("SCORSH_TAG=%s", tag))
	env = append(env, fmt.Sprintf("SCORSH_AUTHOR=%s", author))
	env = append(env, fmt.Sprintf("SCORSH_COMMITTER=%s", committer))

	return env
}