aboutsummaryrefslogtreecommitdiff
path: root/util/gostd
blob: e9fc783d14a0ac84449023b0105d18ff99bdd932 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash

############################  GLOBAL VARIABLES
regex=' '
branch="master"
max_length=150

REGEX_SUFFIX_GO=".+\.go$"

############################  FUNCTIONS
msg() {
    printf '%b' "$1" >&2
}

die() {
    msg "\33[31m[✘]\33[0m ${1}${2}"
    exit 1
}

succ() {
    msg "\33[34m[√]\33[0m ${1}${2}"
}

gostd() {
    local branch=$1
    local reg4exclude=$2
    local max_length=$3

    for file in `git diff $branch --name-only`
    do
        if ! [[ $file =~ $REGEX_SUFFIX_GO ]] || [[ $file =~ $reg4exclude ]]; then
            continue
        fi

        error=`go fmt $file 2>&1`
        if ! [ $? -eq 0 ]; then
            die "go fmt $file:" "$error"
        fi

        succ "$file\n"

        grep -n -E --color=always ".{$max_length}" $file | awk '{ printf ("%4s %s\n", "", $0) }'
    done
}

get_options() {
    while getopts "b:e:hl:" opts
    do
        case $opts in
            b)
                branch=$OPTARG
                ;;
            e)
                regex=$OPTARG
                ;;
            h)
                usage
                exit 0
                ;;
            l)
                max_length=$OPTARG
                ;;
            \?)
                usage
                exit 1
                ;;
        esac
    done
}

usage () {
    cat << _EOC_
Usage:
    gostd [options]

Options:
    -b <branch/commit>        Specify the git diff branch or commit.
                              (default: master)
    -e <regex>                Regex for excluding file or directory.
    -h                        Print this usage.
    -l <length>               Show files that exceed the limit line length.
                              (default: 150)

Examples:
    gostd
    gostd -b master -l 100
    gostd -b 59d532a -e weed/pb -l 100
_EOC_
}

main() {
    get_options "$@"

    gostd "$branch" "$regex" "$max_length"
}

############################  MAIN()
main "$@"