Esempio n. 1
0
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)
		}
	}
}
Esempio n. 2
0
func (c *Client) PerformRequest(method, path string, body io.Reader, configure func(*http.Request)) (*Response, error) {
	res, err := c.http.PerformRequest(method, path, nil, func(req *http.Request) {
		req.Header.Set("Travis-API-Version", "3")
		if token := config.TokenForHost(req.Host); token != "" {
			req.Header.Set("Authorization", "token "+token)
		}
		if configure != nil {
			configure(req)
		}
	})

	if err == nil {
		return &Response{Response: res}, nil
	} else {
		return nil, err
	}
}