cli/command/stack: minor cleanups: use Println, rename vars
- use Println to print newline instead of custom format - use dockerCLI with Go's standard camelCase casing. - suppress some errors to make my IDE and linters happier Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
aa74f931d3
commit
925b8fe34c
@ -40,13 +40,13 @@ func LoadComposefile(dockerCli command.Cli, opts options.Deploy) (*composetypes.
|
|||||||
|
|
||||||
unsupportedProperties := loader.GetUnsupportedProperties(dicts...)
|
unsupportedProperties := loader.GetUnsupportedProperties(dicts...)
|
||||||
if len(unsupportedProperties) > 0 {
|
if len(unsupportedProperties) > 0 {
|
||||||
fmt.Fprintf(dockerCli.Err(), "Ignoring unsupported options: %s\n\n",
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Ignoring unsupported options: %s\n\n",
|
||||||
strings.Join(unsupportedProperties, ", "))
|
strings.Join(unsupportedProperties, ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
deprecatedProperties := loader.GetDeprecatedProperties(dicts...)
|
deprecatedProperties := loader.GetDeprecatedProperties(dicts...)
|
||||||
if len(deprecatedProperties) > 0 {
|
if len(deprecatedProperties) > 0 {
|
||||||
fmt.Fprintf(dockerCli.Err(), "Ignoring deprecated options:\n\n%s\n\n",
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Ignoring deprecated options:\n\n%s\n\n",
|
||||||
propertyWarnings(deprecatedProperties))
|
propertyWarnings(deprecatedProperties))
|
||||||
}
|
}
|
||||||
return config, nil
|
return config, nil
|
||||||
|
@ -52,28 +52,28 @@ func RunServices(ctx context.Context, dockerCli command.Cli, opts options.Servic
|
|||||||
return formatWrite(dockerCli, services, opts)
|
return formatWrite(dockerCli, services, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatWrite(dockerCli command.Cli, services []swarmtypes.Service, opts options.Services) error {
|
func formatWrite(dockerCLI command.Cli, services []swarmtypes.Service, opts options.Services) error {
|
||||||
// if no services in the stack, print message and exit 0
|
// if no services in the stack, print message and exit 0
|
||||||
if len(services) == 0 {
|
if len(services) == 0 {
|
||||||
_, _ = fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace)
|
_, _ = fmt.Fprintln(dockerCLI.Err(), "Nothing found in stack:", opts.Namespace)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
sort.Slice(services, func(i, j int) bool {
|
sort.Slice(services, func(i, j int) bool {
|
||||||
return sortorder.NaturalLess(services[i].Spec.Name, services[j].Spec.Name)
|
return sortorder.NaturalLess(services[i].Spec.Name, services[j].Spec.Name)
|
||||||
})
|
})
|
||||||
|
|
||||||
format := opts.Format
|
f := opts.Format
|
||||||
if len(format) == 0 {
|
if len(f) == 0 {
|
||||||
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
|
if len(dockerCLI.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
|
||||||
format = dockerCli.ConfigFile().ServicesFormat
|
f = dockerCLI.ConfigFile().ServicesFormat
|
||||||
} else {
|
} else {
|
||||||
format = formatter.TableFormatKey
|
f = formatter.TableFormatKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
servicesCtx := formatter.Context{
|
servicesCtx := formatter.Context{
|
||||||
Output: dockerCli.Out(),
|
Output: dockerCLI.Out(),
|
||||||
Format: service.NewListFormat(format, opts.Quiet),
|
Format: service.NewListFormat(f, opts.Quiet),
|
||||||
}
|
}
|
||||||
return service.ListFormatWrite(servicesCtx, services)
|
return service.ListFormatWrite(servicesCtx, services)
|
||||||
}
|
}
|
||||||
|
@ -23,22 +23,22 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// RunDeploy is the swarm implementation of docker stack deploy
|
// RunDeploy is the swarm implementation of docker stack deploy
|
||||||
func RunDeploy(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, opts *options.Deploy, cfg *composetypes.Config) error {
|
func RunDeploy(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet, opts *options.Deploy, cfg *composetypes.Config) error {
|
||||||
if err := validateResolveImageFlag(opts); err != nil {
|
if err := validateResolveImageFlag(opts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// client side image resolution should not be done when the supported
|
// client side image resolution should not be done when the supported
|
||||||
// server version is older than 1.30
|
// server version is older than 1.30
|
||||||
if versions.LessThan(dockerCli.Client().ClientVersion(), "1.30") {
|
if versions.LessThan(dockerCLI.Client().ClientVersion(), "1.30") {
|
||||||
opts.ResolveImage = ResolveImageNever
|
opts.ResolveImage = ResolveImageNever
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Detach && !flags.Changed("detach") {
|
if opts.Detach && !flags.Changed("detach") {
|
||||||
fmt.Fprintln(dockerCli.Err(), "Since --detach=false was not specified, tasks will be created in the background.\n"+
|
_, _ = fmt.Fprintln(dockerCLI.Err(), "Since --detach=false was not specified, tasks will be created in the background.\n"+
|
||||||
"In a future release, --detach=false will become the default.")
|
"In a future release, --detach=false will become the default.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return deployCompose(ctx, dockerCli, opts, cfg)
|
return deployCompose(ctx, dockerCLI, opts, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateResolveImageFlag validates the opts.resolveImage command line option
|
// validateResolveImageFlag validates the opts.resolveImage command line option
|
||||||
@ -67,12 +67,12 @@ func checkDaemonIsSwarmManager(ctx context.Context, dockerCli command.Cli) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// pruneServices removes services that are no longer referenced in the source
|
// pruneServices removes services that are no longer referenced in the source
|
||||||
func pruneServices(ctx context.Context, dockerCli command.Cli, namespace convert.Namespace, services map[string]struct{}) {
|
func pruneServices(ctx context.Context, dockerCCLI command.Cli, namespace convert.Namespace, services map[string]struct{}) {
|
||||||
client := dockerCli.Client()
|
apiClient := dockerCCLI.Client()
|
||||||
|
|
||||||
oldServices, err := getStackServices(ctx, client, namespace.Name())
|
oldServices, err := getStackServices(ctx, apiClient, namespace.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(dockerCli.Err(), "Failed to list services: %s\n", err)
|
_, _ = fmt.Fprintln(dockerCCLI.Err(), "Failed to list services:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pruneServices := []swarm.Service{}
|
pruneServices := []swarm.Service{}
|
||||||
@ -81,5 +81,5 @@ func pruneServices(ctx context.Context, dockerCli command.Cli, namespace convert
|
|||||||
pruneServices = append(pruneServices, service)
|
pruneServices = append(pruneServices, service)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
removeServices(ctx, dockerCli, pruneServices)
|
removeServices(ctx, dockerCCLI, pruneServices)
|
||||||
}
|
}
|
||||||
|
@ -109,8 +109,8 @@ func validateExternalNetworks(ctx context.Context, apiClient client.NetworkAPICl
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.SecretSpec) error {
|
func createSecrets(ctx context.Context, dockerCLI command.Cli, secrets []swarm.SecretSpec) error {
|
||||||
apiClient := dockerCli.Client()
|
apiClient := dockerCLI.Client()
|
||||||
|
|
||||||
for _, secretSpec := range secrets {
|
for _, secretSpec := range secrets {
|
||||||
secret, _, err := apiClient.SecretInspectWithRaw(ctx, secretSpec.Name)
|
secret, _, err := apiClient.SecretInspectWithRaw(ctx, secretSpec.Name)
|
||||||
@ -122,7 +122,7 @@ func createSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.S
|
|||||||
}
|
}
|
||||||
case errdefs.IsNotFound(err):
|
case errdefs.IsNotFound(err):
|
||||||
// secret does not exist, then we create a new one.
|
// secret does not exist, then we create a new one.
|
||||||
fmt.Fprintf(dockerCli.Out(), "Creating secret %s\n", secretSpec.Name)
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Creating secret", secretSpec.Name)
|
||||||
if _, err := apiClient.SecretCreate(ctx, secretSpec); err != nil {
|
if _, err := apiClient.SecretCreate(ctx, secretSpec); err != nil {
|
||||||
return fmt.Errorf("failed to create secret %s: %w", secretSpec.Name, err)
|
return fmt.Errorf("failed to create secret %s: %w", secretSpec.Name, err)
|
||||||
}
|
}
|
||||||
@ -133,8 +133,8 @@ func createSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.S
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.ConfigSpec) error {
|
func createConfigs(ctx context.Context, dockerCLI command.Cli, configs []swarm.ConfigSpec) error {
|
||||||
apiClient := dockerCli.Client()
|
apiClient := dockerCLI.Client()
|
||||||
|
|
||||||
for _, configSpec := range configs {
|
for _, configSpec := range configs {
|
||||||
config, _, err := apiClient.ConfigInspectWithRaw(ctx, configSpec.Name)
|
config, _, err := apiClient.ConfigInspectWithRaw(ctx, configSpec.Name)
|
||||||
@ -146,7 +146,7 @@ func createConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.C
|
|||||||
}
|
}
|
||||||
case errdefs.IsNotFound(err):
|
case errdefs.IsNotFound(err):
|
||||||
// config does not exist, then we create a new one.
|
// config does not exist, then we create a new one.
|
||||||
fmt.Fprintf(dockerCli.Out(), "Creating config %s\n", configSpec.Name)
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Creating config", configSpec.Name)
|
||||||
if _, err := apiClient.ConfigCreate(ctx, configSpec); err != nil {
|
if _, err := apiClient.ConfigCreate(ctx, configSpec); err != nil {
|
||||||
return fmt.Errorf("failed to create config %s: %w", configSpec.Name, err)
|
return fmt.Errorf("failed to create config %s: %w", configSpec.Name, err)
|
||||||
}
|
}
|
||||||
@ -157,8 +157,8 @@ func createConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.C
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createNetworks(ctx context.Context, dockerCli command.Cli, namespace convert.Namespace, networks map[string]network.CreateOptions) error {
|
func createNetworks(ctx context.Context, dockerCLI command.Cli, namespace convert.Namespace, networks map[string]network.CreateOptions) error {
|
||||||
apiClient := dockerCli.Client()
|
apiClient := dockerCLI.Client()
|
||||||
|
|
||||||
existingNetworks, err := getStackNetworks(ctx, apiClient, namespace.Name())
|
existingNetworks, err := getStackNetworks(ctx, apiClient, namespace.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -179,7 +179,7 @@ func createNetworks(ctx context.Context, dockerCli command.Cli, namespace conver
|
|||||||
createOpts.Driver = defaultNetworkDriver
|
createOpts.Driver = defaultNetworkDriver
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(dockerCli.Out(), "Creating network %s\n", name)
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Creating network", name)
|
||||||
if _, err := apiClient.NetworkCreate(ctx, name, createOpts); err != nil {
|
if _, err := apiClient.NetworkCreate(ctx, name, createOpts); err != nil {
|
||||||
return fmt.Errorf("failed to create network %s: %w", name, err)
|
return fmt.Errorf("failed to create network %s: %w", name, err)
|
||||||
}
|
}
|
||||||
@ -187,9 +187,9 @@ func createNetworks(ctx context.Context, dockerCli command.Cli, namespace conver
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deployServices(ctx context.Context, dockerCli command.Cli, services map[string]swarm.ServiceSpec, namespace convert.Namespace, sendAuth bool, resolveImage string) ([]string, error) {
|
func deployServices(ctx context.Context, dockerCLI command.Cli, services map[string]swarm.ServiceSpec, namespace convert.Namespace, sendAuth bool, resolveImage string) ([]string, error) {
|
||||||
apiClient := dockerCli.Client()
|
apiClient := dockerCLI.Client()
|
||||||
out := dockerCli.Out()
|
out := dockerCLI.Out()
|
||||||
|
|
||||||
existingServices, err := getStackServices(ctx, apiClient, namespace.Name())
|
existingServices, err := getStackServices(ctx, apiClient, namespace.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -212,14 +212,14 @@ func deployServices(ctx context.Context, dockerCli command.Cli, services map[str
|
|||||||
|
|
||||||
if sendAuth {
|
if sendAuth {
|
||||||
// Retrieve encoded auth token from the image reference
|
// Retrieve encoded auth token from the image reference
|
||||||
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCli.ConfigFile(), image)
|
encodedAuth, err = command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if service, exists := existingServiceMap[name]; exists {
|
if service, exists := existingServiceMap[name]; exists {
|
||||||
fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
|
_, _ = fmt.Fprintf(out, "Updating service %s (id: %s)\n", name, service.ID)
|
||||||
|
|
||||||
updateOpts := types.ServiceUpdateOptions{EncodedRegistryAuth: encodedAuth}
|
updateOpts := types.ServiceUpdateOptions{EncodedRegistryAuth: encodedAuth}
|
||||||
|
|
||||||
@ -259,12 +259,12 @@ func deployServices(ctx context.Context, dockerCli command.Cli, services map[str
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, warning := range response.Warnings {
|
for _, warning := range response.Warnings {
|
||||||
fmt.Fprintln(dockerCli.Err(), warning)
|
_, _ = fmt.Fprintln(dockerCLI.Err(), warning)
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceIDs = append(serviceIDs, service.ID)
|
serviceIDs = append(serviceIDs, service.ID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(out, "Creating service %s\n", name)
|
_, _ = fmt.Fprintln(out, "Creating service", name)
|
||||||
|
|
||||||
createOpts := types.ServiceCreateOptions{EncodedRegistryAuth: encodedAuth}
|
createOpts := types.ServiceCreateOptions{EncodedRegistryAuth: encodedAuth}
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
|
if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
|
||||||
_, _ = fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", namespace)
|
_, _ = fmt.Fprintln(dockerCli.Err(), "Nothing found in stack:", namespace)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,26 +82,26 @@ func sortServiceByName(services []swarm.Service) func(i, j int) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeServices(ctx context.Context, dockerCli command.Cli, services []swarm.Service) bool {
|
func removeServices(ctx context.Context, dockerCLI command.Cli, services []swarm.Service) bool {
|
||||||
var hasError bool
|
var hasError bool
|
||||||
sort.Slice(services, sortServiceByName(services))
|
sort.Slice(services, sortServiceByName(services))
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
fmt.Fprintf(dockerCli.Out(), "Removing service %s\n", service.Spec.Name)
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Removing service", service.Spec.Name)
|
||||||
if err := dockerCli.Client().ServiceRemove(ctx, service.ID); err != nil {
|
if err := dockerCLI.Client().ServiceRemove(ctx, service.ID); err != nil {
|
||||||
hasError = true
|
hasError = true
|
||||||
fmt.Fprintf(dockerCli.Err(), "Failed to remove service %s: %s", service.ID, err)
|
_, _ = fmt.Fprintf(dockerCLI.Err(), "Failed to remove service %s: %s", service.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hasError
|
return hasError
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeNetworks(ctx context.Context, dockerCli command.Cli, networks []network.Summary) bool {
|
func removeNetworks(ctx context.Context, dockerCLI command.Cli, networks []network.Summary) bool {
|
||||||
var hasError bool
|
var hasError bool
|
||||||
for _, nw := range networks {
|
for _, nw := range networks {
|
||||||
fmt.Fprintf(dockerCli.Out(), "Removing network %s\n", nw.Name)
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Removing network", nw.Name)
|
||||||
if err := dockerCli.Client().NetworkRemove(ctx, nw.ID); err != nil {
|
if err := dockerCLI.Client().NetworkRemove(ctx, nw.ID); err != nil {
|
||||||
hasError = true
|
hasError = true
|
||||||
fmt.Fprintf(dockerCli.Err(), "Failed to remove network %s: %s", nw.ID, err)
|
_, _ = fmt.Fprintf(dockerCLI.Err(), "Failed to remove network %s: %s", nw.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hasError
|
return hasError
|
||||||
@ -110,22 +110,22 @@ func removeNetworks(ctx context.Context, dockerCli command.Cli, networks []netwo
|
|||||||
func removeSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.Secret) bool {
|
func removeSecrets(ctx context.Context, dockerCli command.Cli, secrets []swarm.Secret) bool {
|
||||||
var hasError bool
|
var hasError bool
|
||||||
for _, secret := range secrets {
|
for _, secret := range secrets {
|
||||||
fmt.Fprintf(dockerCli.Out(), "Removing secret %s\n", secret.Spec.Name)
|
_, _ = fmt.Fprintln(dockerCli.Out(), "Removing secret", secret.Spec.Name)
|
||||||
if err := dockerCli.Client().SecretRemove(ctx, secret.ID); err != nil {
|
if err := dockerCli.Client().SecretRemove(ctx, secret.ID); err != nil {
|
||||||
hasError = true
|
hasError = true
|
||||||
fmt.Fprintf(dockerCli.Err(), "Failed to remove secret %s: %s", secret.ID, err)
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Failed to remove secret %s: %s", secret.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hasError
|
return hasError
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeConfigs(ctx context.Context, dockerCli command.Cli, configs []swarm.Config) bool {
|
func removeConfigs(ctx context.Context, dockerCLI command.Cli, configs []swarm.Config) bool {
|
||||||
var hasError bool
|
var hasError bool
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
fmt.Fprintf(dockerCli.Out(), "Removing config %s\n", config.Spec.Name)
|
_, _ = fmt.Fprintln(dockerCLI.Out(), "Removing config", config.Spec.Name)
|
||||||
if err := dockerCli.Client().ConfigRemove(ctx, config.ID); err != nil {
|
if err := dockerCLI.Client().ConfigRemove(ctx, config.ID); err != nil {
|
||||||
hasError = true
|
hasError = true
|
||||||
fmt.Fprintf(dockerCli.Err(), "Failed to remove config %s: %s", config.ID, err)
|
_, _ = fmt.Fprintf(dockerCLI.Err(), "Failed to remove config %s: %s", config.ID, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return hasError
|
return hasError
|
||||||
|
Loading…
x
Reference in New Issue
Block a user