This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
71 lines (61 loc) · 1.67 KB
/
main.go
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
package main
import (
"fmt"
"os"
"strings"
"syscall"
_ "github.com/mislav/go-travis/commands"
"github.com/mislav/go-travis/config"
"github.com/mislav/go-utils/cli"
"github.com/mislav/go-utils/pathname"
)
func main() {
args := cli.NewArgs(os.Args)
cmdName := args.Peek(0)
if cmdName == "" {
cmdName = "builds"
}
repoFlag, args := args.ExtractFlag("-r", "--repo", "REPOSITORY")
tokenFlag, args := args.ExtractFlag("-t", "--token", "TOKEN")
debugFlag, args := args.ExtractFlag("", "--debug", false)
if repoFlag.IsProvided() {
os.Setenv("TRAVIS_REPO", repoFlag.String())
}
if tokenFlag.IsProvided() {
os.Setenv("TRAVIS_TOKEN", tokenFlag.String())
}
if debugFlag.IsProvided() {
if debugFlag.Bool() {
os.Setenv("TRAVIS_DEBUG", "1")
} else {
os.Setenv("TRAVIS_DEBUG", "")
}
}
cmdFunc := cli.Lookup(cmdName)
if cmdFunc != nil {
cmd := cli.NewCmd(args.SubcommandArgs(cmdName))
cmdFunc(cmd)
} else {
exeName := args.ProgramName() + "-" + cmdName
results := pathname.FindInPath(exeName, strings.Split(os.Getenv("PATH"), ":"))
if len(results) > 0 {
exeCmd := results[0]
argv := []string{exeName}
argv = append(argv, args.Slice(1)...)
if !repoFlag.IsProvided() && os.Getenv("TRAVIS_REPO") == "" {
os.Setenv("TRAVIS_REPO", config.RepoSlugFromGit())
}
if !tokenFlag.IsProvided() && os.Getenv("TRAVIS_TOKEN") == "" {
os.Setenv("TRAVIS_TOKEN", config.TokenForHost("api.travis-ci.org"))
}
err := syscall.Exec(exeCmd.String(), argv, os.Environ())
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", exeName, err)
os.Exit(1)
}
} else {
fmt.Fprintf(os.Stderr, "%s: command not found\n", exeName)
os.Exit(1)
}
}
}