aboutsummaryrefslogtreecommitdiffstats
path: root/_examples/merge_base/helpers.go
blob: 179a817656c8dcf6ae05322b03d488af45d595aa (plain) (blame)
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
package main

import (
	"fmt"
	"os"
	"strings"

	"gopkg.in/src-d/go-git.v4/plumbing/object"
)

func checkIfError(err error, code exitCode, mainReason string, v ...interface{}) {
	if err == nil {
		return
	}

	printErr(wrapErr(err, mainReason, v...))
	os.Exit(int(code))
}

func helpAndExit(s string, helpMsg string, code exitCode) {
	if code == exitCodeSuccess {
		printMsg("%s", s)
	} else {
		printErr(fmt.Errorf(s))
	}

	fmt.Println(strings.Replace(helpMsg, "%_COMMAND_NAME_%", os.Args[0], -1))

	os.Exit(int(code))
}

func printErr(err error) {
	fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
}

func printMsg(format string, args ...interface{}) {
	fmt.Printf("%s\n", fmt.Sprintf(format, args...))
}

func printCommits(commits []*object.Commit) {
	for _, commit := range commits {
		if os.Getenv("LOG_LEVEL") == "verbose" {
			fmt.Printf(
				"\x1b[36;1m%s \x1b[90;21m%s\x1b[0m %s\n",
				commit.Hash.String()[:7],
				commit.Hash.String(),
				strings.Split(commit.Message, "\n")[0],
			)
		} else {
			fmt.Println(commit.Hash.String())
		}
	}
}

func wrapErr(err error, s string, v ...interface{}) error {
	if err != nil {
		return fmt.Errorf("%s\n  %s", fmt.Sprintf(s, v...), err)
	}

	return nil
}