-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rebase-fix: add a lazy-check annotation
Signed-off-by: Simon Ferquel <[email protected]>
- Loading branch information
1 parent
792b6ec
commit 19c0830
Showing
4 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 { | ||
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 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 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 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