forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce functional arguments to NewDockerCli for a more stable API.
Signed-off-by: Silvin Lubecki <[email protected]>
- Loading branch information
1 parent
eb0ba4f
commit 7f207f3
Showing
7 changed files
with
214 additions
and
46 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
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,89 @@ | ||
package command | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/docker/cli/cli/streams" | ||
clitypes "github.com/docker/cli/types" | ||
"github.com/docker/docker/pkg/term" | ||
) | ||
|
||
// DockerCliOption applies a modification on a DockerCli. | ||
type DockerCliOption func(cli *DockerCli) error | ||
|
||
// WithStandardStreams sets a cli in, out and err streams with the standard streams. | ||
func WithStandardStreams() DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
// Set terminal emulation based on platform as required. | ||
stdin, stdout, stderr := term.StdStreams() | ||
cli.in = streams.NewIn(stdin) | ||
cli.out = streams.NewOut(stdout) | ||
cli.err = stderr | ||
return nil | ||
} | ||
} | ||
|
||
// WithCombinedStreams uses the same stream for the output and error streams. | ||
func WithCombinedStreams(combined io.Writer) DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.out = streams.NewOut(combined) | ||
cli.err = combined | ||
return nil | ||
} | ||
} | ||
|
||
// WithInputStream sets a cli input stream. | ||
func WithInputStream(in io.ReadCloser) DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.in = streams.NewIn(in) | ||
return nil | ||
} | ||
} | ||
|
||
// WithOutputStream sets a cli output stream. | ||
func WithOutputStream(out io.Writer) DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.out = streams.NewOut(out) | ||
return nil | ||
} | ||
} | ||
|
||
// WithErrorStream sets a cli error stream. | ||
func WithErrorStream(err io.Writer) DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.err = err | ||
return nil | ||
} | ||
} | ||
|
||
// WithContentTrustFromEnv enables content trust on a cli from environment variable DOCKER_CONTENT_TRUST value. | ||
func WithContentTrustFromEnv() DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.contentTrust = false | ||
if e := os.Getenv("DOCKER_CONTENT_TRUST"); e != "" { | ||
if t, err := strconv.ParseBool(e); t || err != nil { | ||
// treat any other value as true | ||
cli.contentTrust = true | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// WithContentTrust enables content trust on a cli. | ||
func WithContentTrust(enabled bool) DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.contentTrust = enabled | ||
return nil | ||
} | ||
} | ||
|
||
// WithContainerizedClient sets the containerized client constructor on a cli. | ||
func WithContainerizedClient(containerizedFn func(string) (clitypes.ContainerizedClient, error)) DockerCliOption { | ||
return func(cli *DockerCli) error { | ||
cli.newContainerizeClient = containerizedFn | ||
return nil | ||
} | ||
} |
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
Oops, something went wrong.