#!/bin/sh # This is designed to compile a single package to a single binary # so it makes some assumptions about things to simplify config # to output a single binary (in a tarball) just use -o file set -e proc usage { echo "Usage: -o file" exit 1 } test $Argc = 0 && usage while [ $# -gt 1 ] { global flag := $1 matchstr $flag { -o { global out := $2 mkdir -p $[dirname $2] shift } --package { global package := $2 shift } --ldflags { global ldflags := $2 shift } * { echo "Unknown option $1" exit 1 } } shift } test $Argc -gt 0 && usage test -z $out && usage test -z $package && global package := $[basename $out] global dir := ""$GOPATH/src/$package"" mkdir -p $dir # untar input tar xf - -C $dir cd $dir # lint before building >&2 echo "gofmt...> !2 echo "gofmt..." test -z $[gofmt -s -l .| grep -v .pb. | grep -v vendor/ | tee /dev/stderr] >&2 echo "govet...> !2 echo "govet..." test -z $[env GOOS=linux go tool vet -printf=false . !2 > !1 | grep -v vendor/ | tee /dev/stderr] >&2 echo "golint...> !2 echo "golint..." test -z $[find . -type f -name "*.go" -not -path "*/vendor/*" -not -name "*.pb.*" -exec golint {} ';' | tee /dev/stderr] >&2 echo "ineffassign...> !2 echo "ineffassign..." test -z $[find . -type f -name "*.go" -not -path "*/vendor/*" -not -name "*.pb.*" -exec ineffassign {} ';' | tee /dev/stderr] >&2 echo "go build...> !2 echo "go build..." if test $GOOS = "darwin" { if test -z $ldflags { go build -o $out $package } else { go build -o $out -ldflags $(ldflags) $package } } else { go build -o $out -buildmode pie -ldflags "-s -w $(ldflags) -extldflags \"-static\"" $package } tar cf - $out (CommandList children: [ (C {(set)} {(-e)}) (FuncDef name: usage body: (BraceGroup children: [(C {(echo)} {(DQ ("Usage: -o file"))}) (C {(exit)} {(1)})] spids: [23] ) spids: [19 22] ) (AndOr children: [ (C {(Lit_Other "[")} {($ VSub_Pound "$#")} {(Lit_Other "=")} {(0)} {(Lit_Other "]")}) (C {(usage)}) ] op_id: Op_DAmp ) (While cond: [(C {(Lit_Other "[")} {($ VSub_Pound "$#")} {(-gt)} {(1)} {(Lit_Other "]")})] body: (DoGroup children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:flag) op: Equal rhs: {(DQ ($ VSub_Number "$1"))} spids: [70] ) ] spids: [70] ) (Case to_match: {(DQ ($ VSub_Name "$flag"))} arms: [ (case_arm pat_list: [{(-o)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:out) op: Equal rhs: {(DQ ($ VSub_Number "$2"))} spids: [89] ) ] spids: [89] ) (C {(mkdir)} {(-p)} { (DQ (CommandSubPart command_list: (CommandList children: [(C {(dirname)} {($ VSub_Number "$2")})] ) left_token: spids: [100 104] ) ) } ) (C {(shift)}) ] spids: [85 86 111 -1] ) (case_arm pat_list: [{(--package)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:package) op: Equal rhs: {(DQ ($ VSub_Number "$2"))} spids: [118] ) ] spids: [118] ) (C {(shift)}) ] spids: [114 115 127 -1] ) (case_arm pat_list: [{(--ldflags)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ldflags) op: Equal rhs: {(DQ ($ VSub_Number "$2"))} spids: [134] ) ] spids: [134] ) (C {(shift)}) ] spids: [130 131 143 -1] ) (case_arm pat_list: [{(Lit_Other "*")}] action: [ (C {(echo)} {(DQ ("Unknown option ") ($ VSub_Number "$1"))}) (C {(exit)} {(1)}) ] spids: [146 147 -1 163] ) ] spids: [76 82 163] ) (C {(shift)}) ] spids: [67 168] ) ) (AndOr children: [ (C {(Lit_Other "[")} {($ VSub_Pound "$#")} {(-gt)} {(0)} {(Lit_Other "]")}) (C {(usage)}) ] op_id: Op_DAmp ) (AndOr children: [ (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$out"))} {(Lit_Other "]")}) (C {(usage)}) ] op_id: Op_DAmp ) (AndOr children: [ (C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$package"))} {(Lit_Other "]")}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:package) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(basename)} {(DQ ($ VSub_Name "$out"))})] ) left_token: spids: [213 219] ) } spids: [212] ) ] spids: [212] ) ] op_id: Op_DAmp ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:dir) op: Equal rhs: {(DQ ($ VSub_Name "$GOPATH") (/src/) ($ VSub_Name "$package"))} spids: [222] ) ] spids: [222] ) (C {(mkdir)} {(-p)} {($ VSub_Name "$dir")}) (C {(tar)} {(xf)} {(-)} {(-C)} {($ VSub_Name "$dir")}) (C {(cd)} {($ VSub_Name "$dir")}) (SimpleCommand words: [{(echo)} {(DQ (gofmt...))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[259])] ) (C {(test)} {(-z)} { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(gofmt)} {(-s)} {(-l)} {(.)}) (C {(grep)} {(-v)} {(.pb.)}) (C {(grep)} {(-v)} {(vendor/)}) (C {(tee)} {(/dev/stderr)}) ] negated: False ) ] ) left_token: spids: [272 301] ) } ) (SimpleCommand words: [{(echo)} {(DQ (govet...))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[304])] ) (C {(test)} {(-z)} { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (SimpleCommand words: [{(go)} {(tool)} {(vet)} {(-printf) (Lit_Other "=") (false)} {(.)}] redirects: [(Redir op_id:Redir_GreatAnd fd:2 arg_word:{(1)} spids:[333])] more_env: [(env_pair name:GOOS val:{(linux)} spids:[318])] ) (C {(grep)} {(-v)} {(vendor/)}) (C {(tee)} {(/dev/stderr)}) ] negated: False ) ] ) left_token: spids: [317 349] ) } ) (SimpleCommand words: [{(echo)} {(DQ (golint...))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[352])] ) (C {(test)} {(-z)} { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(find)} {(.)} {(-type)} {(f)} {(-name)} {(DQ ("*.go"))} {(-not)} {(-path)} {(DQ ("*/vendor/*"))} {(-not)} {(-name)} {(DQ ("*.pb.*"))} {(-exec)} {(golint)} {(Lit_LBrace "{") (Lit_RBrace "}")} {(EscapedLiteralPart token:)} ) (C {(tee)} {(/dev/stderr)}) ] negated: False ) ] ) left_token: spids: [365 410] ) } ) (SimpleCommand words: [{(echo)} {(DQ (ineffassign...))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[413])] ) (C {(test)} {(-z)} { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(find)} {(.)} {(-type)} {(f)} {(-name)} {(DQ ("*.go"))} {(-not)} {(-path)} {(DQ ("*/vendor/*"))} {(-not)} {(-name)} {(DQ ("*.pb.*"))} {(-exec)} {(ineffassign)} {(Lit_LBrace "{") (Lit_RBrace "}")} {(EscapedLiteralPart token:)} ) (C {(tee)} {(/dev/stderr)}) ] negated: False ) ] ) left_token: spids: [426 471] ) } ) (SimpleCommand words: [{(echo)} {(DQ ("go build..."))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[474])] ) (If arms: [ (if_arm cond: [ (C {(Lit_Other "[")} {(DQ ($ VSub_Name "$GOOS"))} {(Lit_Other "=")} {(DQ (darwin))} {(Lit_Other "]")} ) ] action: [ (If arms: [ (if_arm cond: [(C {(Lit_Other "[")} {(-z)} {(DQ ($ VSub_Name "$ldflags"))} {(Lit_Other "]")})] action: [ (C {(go)} {(build)} {(-o)} {($ VSub_Name "$out")} {(DQ ($ VSub_Name "$package"))}) ] spids: [-1 516] ) ] else_action: [ (C {(go)} {(build)} {(-o)} {($ VSub_Name "$out")} {(-ldflags)} {(DQ (${ VSub_Name ldflags))} {(DQ ($ VSub_Name "$package"))} ) ] spids: [532 556] ) ] spids: [-1 500] ) ] else_action: [ (C {(go)} {(build)} {(-o)} {($ VSub_Name "$out")} {(-buildmode)} {(pie)} {(-ldflags)} { (DQ ("-s -w ") (${ VSub_Name ldflags) (" -extldflags ") (EscapedLiteralPart token:) (-static) (EscapedLiteralPart token:) ) } {(DQ ($ VSub_Name "$package"))} ) ] spids: [558 590] ) (C {(tar)} {(cf)} {(-)} {($ VSub_Name "$out")}) ] )