#!/bin/bash # Copyright 2014 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # GoFmt apparently is changing @ head... set -o errexit set -o nounset set -o pipefail global KUBE_ROOT := "$[dirname $(BASH_SOURCE)]/.." source "$(KUBE_ROOT)/hack/lib/init.sh" cd $(KUBE_ROOT) # Prefer bazel's gofmt. global gofmt := '"external/io_bazel_rules_go_toolchain/bin/gofmt'" if [[ ! -x "${gofmt}" ]] { global gofmt := $[which gofmt] kube::golang::verify_go_version } proc find_files { find . -not '(' \ '(' \ -wholename './output' \ -o -wholename './_output' \ -o -wholename './_gopath' \ -o -wholename './release' \ -o -wholename './target' \ -o -wholename '*/third_party/*' \ -o -wholename '*/vendor/*' \ -o -wholename './staging/src/k8s.io/client-go/*vendor/*' \ -o -wholename '*/bindata.go' \ ')' -prune \ ')' -name '*.go' } # gofmt exits with non-zero exit code if it finds a problem unrelated to # formatting (e.g., a file does not parse correctly). Without "|| true" this # would have led to no useful error message from gofmt, because the script would # have failed before getting to the "echo" in the block below. global diff := $[find_files | xargs $(gofmt) -d -s !2 > !1] || true if [[ -n "${diff}" ]] { echo $(diff) exit 1 } (CommandList children: [ (C {(set)} {(-o)} {(errexit)}) (C {(set)} {(-o)} {(nounset)}) (C {(set)} {(-o)} {(pipefail)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:KUBE_ROOT) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [(C {(dirname)} {(DQ (${ VSub_Name BASH_SOURCE))})] ) left_token: spids: [68 76] ) (/..) } spids: [67] ) ] spids: [67] ) (C {(source)} {(DQ (${ VSub_Name KUBE_ROOT) (/hack/lib/init.sh))}) (C {(cd)} {(DQ (${ VSub_Name KUBE_ROOT))}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:gofmt) op: Equal rhs: {(DQ (external/io_bazel_rules_go_toolchain/bin/gofmt))} spids: [101] ) ] spids: [101] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id:BoolUnary_x child:{(DQ (${ VSub_Name gofmt))}) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:gofmt) op: Equal rhs: { (CommandSubPart command_list: (CommandList children:[(C {(which)} {(gofmt)})]) left_token: spids: [127 131] ) } spids: [126] ) ] spids: [126] ) (C {(kube) (Lit_Other ":") (Lit_Other ":") (golang) (Lit_Other ":") (Lit_Other ":") (verify_go_version) } ) ] spids: [-1 123] ) ] spids: [-1 142] ) (FuncDef name: find_files body: (BraceGroup children: [ (C {(find)} {(.)} {(-not)} {(EscapedLiteralPart token:)} {(EscapedLiteralPart token:)} {(-wholename)} {(SQ <./output>)} {(-o)} {(-wholename)} {(SQ <./_output>)} {(-o)} {(-wholename)} {(SQ <./_gopath>)} {(-o)} {(-wholename)} {(SQ <./release>)} {(-o)} {(-wholename)} {(SQ <./target>)} {(-o)} {(-wholename)} {(SQ <"*/third_party/*">)} {(-o)} {(-wholename)} {(SQ <"*/vendor/*">)} {(-o)} {(-wholename)} {(SQ <"./staging/src/k8s.io/client-go/*vendor/*">)} {(-o)} {(-wholename)} {(SQ <"*/bindata.go">)} {(EscapedLiteralPart token:)} {(-prune)} {(EscapedLiteralPart token:)} {(-name)} {(SQ <"*.go">)} ) ] spids: [149] ) spids: [145 148] ) (AndOr children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:diff) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (Pipeline children: [ (C {(find_files)}) (SimpleCommand words: [{(xargs)} {(${ VSub_Name gofmt)} {(-d)} {(-s)}] redirects: [ (Redir op_id: Redir_GreatAnd fd: 2 arg_word: {(1)} spids: [299] ) ] ) ] negated: False ) ] ) left_token: spids: [284 301] ) } spids: [283] ) ] spids: [283] ) (C {(true)}) ] op_id: Op_DPipe ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr:(BoolUnary op_id:BoolUnary_n child:{(DQ (${ VSub_Name diff))})) terminator: ) ] action: [(C {(echo)} {(DQ (${ VSub_Name diff))}) (C {(exit)} {(1)})] spids: [-1 322] ) ] spids: [-1 338] ) ] )