-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add standard CLI options #366
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
Open
fernandezcuesta
wants to merge
7
commits into
crossplane:main
Choose a base branch
from
fernandezcuesta:feat/standard-cli-args-and-env-variables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f58ec6
feat: add standard CLI options
fernandezcuesta 4fc2cab
chore: refactor and document usage
fernandezcuesta 032f984
chore: merge both examples
fernandezcuesta 1b1a251
go mod tidy
bobh66 04729cd
Merge branch 'main' into feat/standard-cli-args-and-env-variables
fernandezcuesta 2764593
fix: add alias to tls-certs-dir
fernandezcuesta 228e30e
fix: expose max-send-message-size
fernandezcuesta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| Copyright 2026 The Crossplane 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. | ||
| */ | ||
|
|
||
| package function | ||
|
|
||
| import ( | ||
| "github.com/alecthomas/kong" | ||
|
|
||
| "github.com/crossplane/function-sdk-go/logging" | ||
| ) | ||
|
|
||
| // CLI provides standard flags and environment variables for Composition | ||
| // Functions. It is designed to be used with [github.com/alecthomas/kong]. | ||
| // | ||
| // Without custom flags, use CLI directly with [Parse]: | ||
| // | ||
| // type CLI struct { | ||
| // function.CLI `kong:"embed"` | ||
| // // MyFlag string `default:"foo" env:"MY_FLAG" help:"My custom flag."` | ||
| // } | ||
| // | ||
| // func (c *CLI) Run() error { | ||
| // log, err := c.Logger() | ||
| // if err != nil { | ||
| // return err | ||
| // } | ||
| // return function.Serve(&Function{log: log}, c.StandardOptions()...) | ||
| // // or with custom flags: | ||
| // // return function.Serve(&Function{log: log, myFlag: c.MyFlag}, c.StandardOptions()...) | ||
| // } | ||
| // | ||
| // func main() { | ||
| // function.Parse(&CLI{}, "My function.") | ||
| // } | ||
| type CLI struct { | ||
| Address string `default:":9443" env:"ADDRESS" help:"Address at which to listen for gRPC connections."` | ||
| Debug bool `env:"DEBUG" help:"Emit debug logs in addition to info logs." short:"d"` | ||
| Insecure bool `env:"INSECURE" help:"Run without mTLS credentials. If you supply this flag --tls-server-certs-dir will be ignored."` | ||
| MaxRecvMessageSize int `aliases:"max-grpc-message-size" default:"4" env:"MAX_RECV_MESSAGE_SIZE,MAX_GRPC_MESSAGE_SIZE" help:"Maximum size of received messages in MB."` | ||
| MaxSendMessageSize int `env:"MAX_SEND_MESSAGE_SIZE" help:"Maximum size of sent messages in MB. Defaults to max-recv-message-size when unset."` | ||
| Network string `default:"tcp" env:"NETWORK" help:"Network on which to listen for gRPC connections."` | ||
| TLSCertsDir string `aliases:"tls-server-certs-dir" env:"TLS_SERVER_CERTS_DIR" help:"Directory containing server certs (tls.key, tls.crt) and the CA used to verify client certificates (ca.crt)." name:"tls-certs-dir"` | ||
| } | ||
|
|
||
| // StandardOptions returns the ServeOptions derived from standard CLI flags. | ||
| func (c *CLI) StandardOptions() []ServeOption { | ||
| sendSize := c.MaxSendMessageSize | ||
| if sendSize == 0 { | ||
| sendSize = c.MaxRecvMessageSize | ||
| } | ||
| return []ServeOption{ | ||
| Listen(c.Network, c.Address), | ||
| MTLSCertificates(c.TLSCertsDir), | ||
| Insecure(c.Insecure), | ||
| MaxRecvMessageSize(c.MaxRecvMessageSize * 1024 * 1024), | ||
| MaxSendMessageSize(sendSize * 1024 * 1024), | ||
| } | ||
| } | ||
|
|
||
| // Logger returns a new logger configured from CLI flags. | ||
| func (c *CLI) Logger() (logging.Logger, error) { | ||
| return NewLogger(c.Debug) | ||
| } | ||
|
|
||
| // Parse parses CLI flags using kong and runs the command. The cli argument must | ||
| // have a Run() error method. An optional description is used as CLI help text. | ||
| func Parse(cli any, description ...string) { | ||
| options := []kong.Option{} | ||
| if len(description) > 0 { | ||
| options = append(options, kong.Description(description[0])) | ||
| } | ||
| ctx := kong.Parse(cli, options...) | ||
| ctx.FatalIfErrorf(ctx.Run()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: crossplane/function-sdk-go
Length of output: 4444
🏁 Script executed:
Repository: crossplane/function-sdk-go
Length of output: 9980
Skip TLS certificate loading in insecure mode.
StandardOptionsappliesMTLSCertificates(c.TLSCertsDir)beforeInsecure(c.Insecure). A non-empty invalid or missing TLS directory can therefore return an error before insecure credentials are applied. Could you skipMTLSCertificateswhenc.Insecureis true so--insecureignores the TLS directory as documented?🤖 Prompt for AI Agents