github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_config.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/containerd/nerdctl/pkg/clientutil"
    23  	"github.com/containerd/nerdctl/pkg/cmd/compose"
    24  	"github.com/containerd/nerdctl/pkg/composer"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func newComposeConfigCommand() *cobra.Command {
    29  	var composeConfigCommand = &cobra.Command{
    30  		Use:           "config",
    31  		Short:         "Validate and view the Compose file",
    32  		RunE:          composeConfigAction,
    33  		SilenceUsage:  true,
    34  		SilenceErrors: true,
    35  	}
    36  	composeConfigCommand.Flags().BoolP("quiet", "q", false, "Only validate the configuration, don't print anything.")
    37  	composeConfigCommand.Flags().Bool("services", false, "Print the service names, one per line.")
    38  	composeConfigCommand.Flags().Bool("volumes", false, "Print the volume names, one per line.")
    39  	composeConfigCommand.Flags().String("hash", "", "Print the service config hash, one per line.")
    40  	composeConfigCommand.RegisterFlagCompletionFunc("hash", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    41  		return []string{"\"*\""}, cobra.ShellCompDirectiveNoFileComp
    42  	})
    43  	return composeConfigCommand
    44  }
    45  
    46  func composeConfigAction(cmd *cobra.Command, args []string) error {
    47  	globalOptions, err := processRootCmdFlags(cmd)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	if len(args) != 0 {
    52  		// TODO: support specifying service names as args
    53  		return fmt.Errorf("arguments %v not supported", args)
    54  	}
    55  	quiet, err := cmd.Flags().GetBool("quiet")
    56  	if err != nil {
    57  		return err
    58  	}
    59  	services, err := cmd.Flags().GetBool("services")
    60  	if err != nil {
    61  		return err
    62  	}
    63  	volumes, err := cmd.Flags().GetBool("volumes")
    64  	if err != nil {
    65  		return err
    66  	}
    67  	hash, err := cmd.Flags().GetString("hash")
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	defer cancel()
    77  	options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	if quiet {
    87  		return nil
    88  	}
    89  	co := composer.ConfigOptions{
    90  		Services: services,
    91  		Volumes:  volumes,
    92  		Hash:     hash,
    93  	}
    94  	return c.Config(ctx, cmd.OutOrStdout(), co)
    95  }