-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Defer API version negotiation #1747
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
Closed
simonferquel
wants to merge
6
commits into
docker:master
from
simonferquel:defer-context-initialization
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8adf6d3
Defer API version negotiation
simonferquel 188fcee
Support combination of no-remote and experimental CLI
simonferquel 4b2de6c
Renamed no-remote to local-only
simonferquel 12c9767
Added a bunch of tests for flags/commands validation
simonferquel 4975ead
Rebase-fix: add a lazy-check annotation
simonferquel d35f48c
Fix Import style
simonferquel 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
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,51 @@ | ||||||
| package lazychecks | ||||||
|
|
||||||
| import ( | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "strconv" | ||||||
| "strings" | ||||||
|
|
||||||
| "github.com/docker/cli/cli/command" | ||||||
| "github.com/spf13/pflag" | ||||||
| ) | ||||||
|
|
||||||
| var lazyChecks []LazyCheck | ||||||
|
|
||||||
| const lazyCheckAnnotation = "lazy-checks" | ||||||
|
|
||||||
| // LazyCheck is a callback that is called lazily to know if a command / flag should be enabled | ||||||
| type LazyCheck func(clientInfo command.ClientInfo, serverInfo command.ServerInfo, clientVersion string) error | ||||||
|
|
||||||
| // AddLazyFlagCheck adds a LazyCheck on a flag | ||||||
| func AddLazyFlagCheck(flagset *pflag.FlagSet, name string, check LazyCheck) { | ||||||
| index := len(lazyChecks) | ||||||
| lazyChecks = append(lazyChecks, check) | ||||||
| f := flagset.Lookup(name) | ||||||
| if f == nil { | ||||||
| return | ||||||
| } | ||||||
| if f.Annotations == nil { | ||||||
| f.Annotations = map[string][]string{} | ||||||
| } | ||||||
| f.Annotations[lazyCheckAnnotation] = append(f.Annotations[lazyCheckAnnotation], fmt.Sprintf("%d", index)) | ||||||
| } | ||||||
|
|
||||||
| // EvaluateFlagLazyChacks evaluates the lazy checks associated with a flag depending on client/server info | ||||||
| func EvaluateFlagLazyChacks(flag *pflag.Flag, clientInfo command.ClientInfo, serverInfo command.ServerInfo, clientVersion string) error { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| var errs []string | ||||||
| for _, indexStr := range flag.Annotations[lazyCheckAnnotation] { | ||||||
| index, err := strconv.ParseInt(indexStr, 10, 32) | ||||||
| if err != nil { | ||||||
| errs = append(errs, err.Error()) | ||||||
| continue | ||||||
| } | ||||||
| if err := lazyChecks[int(index)](clientInfo, serverInfo, clientVersion); err != nil { | ||||||
| errs = append(errs, err.Error()) | ||||||
| } | ||||||
| } | ||||||
| if len(errs) == 0 { | ||||||
| return nil | ||||||
| } | ||||||
| return errors.New(strings.Join(errs, "\n")) | ||||||
| } | ||||||
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
Oops, something went wrong.
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.