github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/completion/functions.go (about)

     1  package completion
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/docker/cli/cli/command"
     7  	"github.com/docker/cli/cli/command/formatter"
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/api/types/image"
    11  	"github.com/docker/docker/api/types/volume"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  // ValidArgsFn a function to be used by cobra command as `ValidArgsFunction` to offer command line completion
    16  type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)
    17  
    18  // ImageNames offers completion for images present within the local store
    19  func ImageNames(dockerCli command.Cli) ValidArgsFn {
    20  	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    21  		list, err := dockerCli.Client().ImageList(cmd.Context(), image.ListOptions{})
    22  		if err != nil {
    23  			return nil, cobra.ShellCompDirectiveError
    24  		}
    25  		var names []string
    26  		for _, image := range list {
    27  			names = append(names, image.RepoTags...)
    28  		}
    29  		return names, cobra.ShellCompDirectiveNoFileComp
    30  	}
    31  }
    32  
    33  // ContainerNames offers completion for container names and IDs
    34  // By default, only names are returned.
    35  // Set DOCKER_COMPLETION_SHOW_CONTAINER_IDS=yes to also complete IDs.
    36  func ContainerNames(dockerCli command.Cli, all bool, filters ...func(types.Container) bool) ValidArgsFn {
    37  	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    38  		list, err := dockerCli.Client().ContainerList(cmd.Context(), container.ListOptions{
    39  			All: all,
    40  		})
    41  		if err != nil {
    42  			return nil, cobra.ShellCompDirectiveError
    43  		}
    44  
    45  		showContainerIDs := os.Getenv("DOCKER_COMPLETION_SHOW_CONTAINER_IDS") == "yes"
    46  
    47  		var names []string
    48  		for _, container := range list {
    49  			skip := false
    50  			for _, fn := range filters {
    51  				if !fn(container) {
    52  					skip = true
    53  					break
    54  				}
    55  			}
    56  			if skip {
    57  				continue
    58  			}
    59  			if showContainerIDs {
    60  				names = append(names, container.ID)
    61  			}
    62  			names = append(names, formatter.StripNamePrefix(container.Names)...)
    63  		}
    64  		return names, cobra.ShellCompDirectiveNoFileComp
    65  	}
    66  }
    67  
    68  // VolumeNames offers completion for volumes
    69  func VolumeNames(dockerCli command.Cli) ValidArgsFn {
    70  	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    71  		list, err := dockerCli.Client().VolumeList(cmd.Context(), volume.ListOptions{})
    72  		if err != nil {
    73  			return nil, cobra.ShellCompDirectiveError
    74  		}
    75  		var names []string
    76  		for _, vol := range list.Volumes {
    77  			names = append(names, vol.Name)
    78  		}
    79  		return names, cobra.ShellCompDirectiveNoFileComp
    80  	}
    81  }
    82  
    83  // NetworkNames offers completion for networks
    84  func NetworkNames(dockerCli command.Cli) ValidArgsFn {
    85  	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    86  		list, err := dockerCli.Client().NetworkList(cmd.Context(), types.NetworkListOptions{})
    87  		if err != nil {
    88  			return nil, cobra.ShellCompDirectiveError
    89  		}
    90  		var names []string
    91  		for _, network := range list {
    92  			names = append(names, network.Name)
    93  		}
    94  		return names, cobra.ShellCompDirectiveNoFileComp
    95  	}
    96  }
    97  
    98  // NoComplete is used for commands where there's no relevant completion
    99  func NoComplete(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
   100  	return nil, cobra.ShellCompDirectiveNoFileComp
   101  }