-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Codegen: a new script for subprojects to use #117262
Codegen: a new script for subprojects to use #117262
Conversation
/retest |
/triage accepted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is complicated enough to make me reach for: https://google.github.io/styleguide/shellguide.html#when-to-use-shell
defaulter-gen | ||
) | ||
# shellcheck disable=2046 # printf word-splitting is intentional | ||
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the whitespace after %s
within the quotes intentional here?
also is the printf + word splitting necessary? we could just put k8s.io/code-generator/cmd/
in front of the three BINS
entries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the space is needed to produce the right output.
As you raised above, there is a POTENTIAL whitespace issue, but not an ACTUAL issue - all the places we do this are controlled inputs which do not include the user-owned path. They are all either Go package names (as returned by go list
- no spaces) or binary names enumerated immediately before. So One option is to leave it as printf.
Exploring:
$ # foo just prints info about its args
$ function foo() { local i=1; for arg; do echo "\$$i: $arg"; i=$((i+1)); done; }
$ foo a b "c d e"
$1: a
$2: b
$3: c d e
$ X=("a" "b" "c d e")
$ printf -- "--arg=\"%s\" " "${X[@]}"; echo;
--arg="a" --arg="b" --arg="c d e"
That looks OK, but...
$ foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg="a"
$2: --arg="b"
$3: --arg="c
$4: d
$5: e"
The shell evaluation of quoted strings doesn't survive. We could do:
$ eval foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg=a
$2: --arg=b
$3: --arg=c d e
...but that is easy to misuse or just forget about. Bash arrays and functions are a mess, which is why we have things like read-array which is deeeeep dark magic
or else we take it into our own hands:
$ A=(); for arg in "${X[@]}"; do A+=("--arg=\"$arg\""); done
$ foo "${A[@]}"
$1: --arg="a"
$2: --arg="b"
$3: --arg="c d e"
It's not so bad, but it can't be a function.
Another option is to use the IFS trick ((IFS=,; foo "${X[*]}")
- which, while terse, is also a bit brittle (IFS has to be set on a distinct command and [@]
does not work). This passes a single --input-dirs=foo,bar,bat
rather than --input-dirs=foo --input-dirs=bar --input-dirs=bat
.
I'll add commits in a couple styles - you tell me which you think make sense, and then I will back-integrate :)
staging/src/k8s.io/apiextensions-apiserver/examples/client-go/hack/update-codegen.sh
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pushed with WIP commits as the only changes.
staging/src/k8s.io/apiextensions-apiserver/examples/client-go/hack/update-codegen.sh
Show resolved
Hide resolved
defaulter-gen | ||
) | ||
# shellcheck disable=2046 # printf word-splitting is intentional | ||
GO111MODULE=on go install $(printf "k8s.io/code-generator/cmd/%s " "${BINS[@]}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the space is needed to produce the right output.
As you raised above, there is a POTENTIAL whitespace issue, but not an ACTUAL issue - all the places we do this are controlled inputs which do not include the user-owned path. They are all either Go package names (as returned by go list
- no spaces) or binary names enumerated immediately before. So One option is to leave it as printf.
Exploring:
$ # foo just prints info about its args
$ function foo() { local i=1; for arg; do echo "\$$i: $arg"; i=$((i+1)); done; }
$ foo a b "c d e"
$1: a
$2: b
$3: c d e
$ X=("a" "b" "c d e")
$ printf -- "--arg=\"%s\" " "${X[@]}"; echo;
--arg="a" --arg="b" --arg="c d e"
That looks OK, but...
$ foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg="a"
$2: --arg="b"
$3: --arg="c
$4: d
$5: e"
The shell evaluation of quoted strings doesn't survive. We could do:
$ eval foo $(printf -- "--arg=\"%s\" " "${X[@]}")
$1: --arg=a
$2: --arg=b
$3: --arg=c d e
...but that is easy to misuse or just forget about. Bash arrays and functions are a mess, which is why we have things like read-array which is deeeeep dark magic
or else we take it into our own hands:
$ A=(); for arg in "${X[@]}"; do A+=("--arg=\"$arg\""); done
$ foo "${A[@]}"
$1: --arg="a"
$2: --arg="b"
$3: --arg="c d e"
It's not so bad, but it can't be a function.
Another option is to use the IFS trick ((IFS=,; foo "${X[*]}")
- which, while terse, is also a bit brittle (IFS has to be set on a distinct command and [@]
does not work). This passes a single --input-dirs=foo,bar,bat
rather than --input-dirs=foo --input-dirs=bar --input-dirs=bat
.
I'll add commits in a couple styles - you tell me which you think make sense, and then I will back-integrate :)
ping this - I have more stacked up behind it and it feeds into my go workspaces branch :) Sorry!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/approve
/hold
LGTM label has been added. Git tree hash: 24d964bc91620bf8d944a80e389c3fcb3bc26c72
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: BenTheElder, thockin The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
...rc/k8s.io/kube-aggregator/pkg/client/informers/externalversions/apiregistration/interface.go
Show resolved
Hide resolved
One question on the generation order, no other comments from me. I didn't review the script changes closely, will defer to @BenTheElder's review |
2) is probably slightly easier for most contributors to read and modify? I don't have a super strong preference though, they're both reasonable enough. |
This script (k8s.io/code-generator/kube_codegen.sh) should be somewhat easier to understand and maintain. This commit converts one caller (sample-apiserver) to use it, which exercises all 3 functions. More to follow.
This caused some of the generated code to be sorted where it wasn't before.
b678f13
to
fb4d015
Compare
So say we all. Applied as fixups (the net result was just reordering and squashing commits, no code changes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
LGTM label has been added. Git tree hash: 7f5b83957bef8e4b0310ef2640ee4f701d7b1bf4
|
Add a new way for subprojects to do codegen
This new script (k8s.io/code-generator/kube_codegen.sh) should be somewhat easier to understand and maintain. This PR includes commits which convert all of the internal subprojects to use it.
/kind cleanup