-
Notifications
You must be signed in to change notification settings - Fork 16
go generate
support for njs/headersmore
#109
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
Conversation
scripts/generate/public_git.sh
Outdated
generate_command="go run ./cmd/generate --src-path=$tmp --directive-map=$directive_map --match-func=$match_func" | ||
generate_command="$generate_command > $path" | ||
|
||
eval "$generate_command" |
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.
Generally should avoid eval unless there is a good reason to.
generate_command="go run ./cmd/generate --src-path=$tmp --directive-map=$directive_map --match-func=$match_func" | |
generate_command="$generate_command > $path" | |
eval "$generate_command" | |
go run ./cmd/generate "--src-path=$tmp" "--directive-map=$directive_map" "--match-func=$match_func" > $path |
should do just fine, no?
If you want to make this a little neater and avoid long lines the pattern I like (requires /bin/bash at the top) is:
generate_command="go run ./cmd/generate --src-path=$tmp --directive-map=$directive_map --match-func=$match_func" | |
generate_command="$generate_command > $path" | |
eval "$generate_command" | |
genArgs=( | |
"--src-path=$tmp" | |
# line comments are ok here to describe what you're doing | |
"--directive-map=$directive_map" | |
"--match-func=$match_func" | |
) | |
go run ./cmd/generate "${genArgs[@]}" > "$path" |
genArgs is a bash array that we then splat (with quoting intact) onto the run command. The really nice thing about this array construct is that it doesn't require the \
to continue lines that makes comments not work and is error prone as you add/remove lines.
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.
Another benefit of the bash array is that you can append to it, e.g. genArgs+=(--directive-map=foo)
With that, and the fact that it looks like a lot of the args are being passed through to cmd/generate can you do more of the error handling in there?
E.g. if the the case statement was
--match_func | -m)
genArgs+=("--match-func=$2")
shift
;;
and if the cmd/generate doesn't get a match function it raises the error?
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.
genArgs is a bash array that we then splat (with quoting intact) onto the run command.
Cool! I didn't know that before. Thanks for telling me this. You are right, the reason I used eval
was I don't want long line and bash array seems a perfect solution. I applied it in latest commit.
With that, and the fact that it looks like a lot of the args are being passed through to cmd/generate can you do more of the error handling in there?
E.g. if the the case statement was
--match_func | -m)
genArgs+=("--match-func=$2")
shift
;;
and if the cmd/generate doesn't get a match function it raises the error?
I thought I already did the check here? Just after the arguments reading part
if [ "$url" = "" ]; then
echo "url can't be empty"
exit 1
fi
if [ "$path" = "" ]; then
echo "path can't be empty"
exit 1
fi
if [ "$directive_map" = "" ]; then
echo "directive-map can't be empty"
exit 1
fi
if [ "$match_func" = "" ]; then
echo "match-func can't be empty"
exit 1
fi
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.
yeah the array-splat is one of my favorite bash tricks, e.g. look at the use of "opts" separately in most functions in https://gitlab.com/f5/nginx/nginxazurelb/azure-infrastructure/-/snippets/2371626
analyze.go
Outdated
@@ -7,6 +7,9 @@ | |||
|
|||
package crossplane | |||
|
|||
//go:generate sh ./scripts/generate/public_git.sh --url https://github.com/openresty/headers-more-nginx-module.git -p ./analyze_headersMore_directives.gen.go -m HeadersMoreDirectivesMatchFn -d moduleHeadersMoreDirectives |
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.
I gave some ideas for parsing the args separately below, but maybe public_git
should just do the clone and not call generate.
E.g. what about having this line be:
//go:generate sh ./scripts/generate/public_git.sh --url https://github.com/openresty/headers-more-nginx-module.git -p ./analyze_headersMore_directives.gen.go -m HeadersMoreDirectivesMatchFn -d moduleHeadersMoreDirectives | |
//go:generate sh go run ./cmd/generate --src-path=$(./scripts/generate/public_git.sh --url https://github.com/openresty/headers-more-nginx-module.git) -p ./analyze_headersMore_directives.gen.go -m HeadersMoreDirectivesMatchFn -d moduleHeadersMoreDirectives |
public_git does the clone and prints out the the directory that becomes the src-path. Now public-git doesn't need to know anything about cmd/generate or its args.
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.
Oh there are some considerations for OSS and Lua in next MR. In next MR we will add OSS and Lua into go generate
, and the arguments(-filter
and -override
) for ./cmd/generate
will be long. Here the command line for lua:
go run ./cmd/generate --src-path=/var/folders/pz/_91zbp7d7fb4mpclbtmzw4rr0000gp/T/tmp.uc45dimtKO --directive-map=moduleLuaDirectives --match-func=LuaDirectivesMatchFn -filter=lua_fake_shm -override="content_by_lua_block:ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake1" -override="rewrite_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake1" -override="ssl_certificate_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxConfTake1" -override="ssl_session_fetch_by_lua_block:ngxHTTPMainConf | ngxConfTake1" -override="ssl_session_store_by_lua_block:ngxHTTPMainConf | ngxConfTake1" -override="set_by_lua_block:ngxHTTPSrvConf | ngxHTTPSifConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake2" -override="ssl_client_hello_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxConfTake1" -override="body_filter_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake1" -override="init_by_lua_block:ngxHTTPMainConf | ngxConfTake1" -override="init_worker_by_lua_block:ngxHTTPMainConf | ngxConfTake1" -override="header_filter_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake1" -override="log_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake1" -override="access_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxHTTPLocConf | ngxHTTPLifConf | ngxConfTake1" -override="balancer_by_lua_block:ngxHTTPUpsConf | ngxConfTake1" -override="server_rewrite_by_lua_block:ngxHTTPMainConf | ngxHTTPSrvConf | ngxConfTake1" -override="exit_worker_by_lua_block:ngxHTTPMainConf | ngxConfTake1" > ./analyze_lua_directives.go
We don't want this as a long line of go:generate
comment. We plan to put the -filter
and -override
arguments in a separate shell script, or in a file and let public-git.sh
to read that file in next MR. So we put the part of invoking ./cmd/generate
here. And the purpose of public_git.sh
is only called by go generate
to update .gen.go
files. I just added some comments to illustrate this.
I haven't worked in this repo yet so I'm coming in a bit blind; trying to assemble some pieces here:
Your suggesting a user might do the git clone manually and call Idea 1 - lots of shell
idea 2 - more go
(i'll try and come back here with more detail later, but have to go now) |
Oh I should've provided more context here.
Nah, I provided a default directive-map/match-func for third party library. I just wanted some convenience when they invoke |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #109 +/- ##
==========================================
+ Coverage 78.82% 79.09% +0.26%
==========================================
Files 26 26
Lines 1124 1124
==========================================
+ Hits 886 889 +3
+ Misses 182 179 -3
Partials 56 56 ☔ View full report in Codecov by Sentry. |
Proposed changes
Update: apply json config, merged with #112
We plan to provide a tool to update supporting files(
analyze_*_directives.go
) bygo generate
. This MR is to providego generate
update for njs/headersmore.go generate
only targets for dynamic modules(or OSS, NPlus) we want to provide long-lasting supports for. To provide temporary support for a module, please refer to #101Here are the detailed changes:
.gen.go
suffix to generated files and ignore them ingolangci-lint
go:generate
comments for njs/headersmore inanalyze.go
, now we can updateanalyze_headersMore_directives.gen.go
andanalyze_njs_directives.gen.go
bygo generate
Checklist
Before creating a PR, run through this checklist and mark each as complete.
CONTRIBUTING
documentREADME.md
)