2016-09-08 13:11:39 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"sort"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2024-06-04 03:17:20 -04:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
|
|
|
noTrunc bool
|
|
|
|
format string
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 05:25:25 -04:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := listOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List networks",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 18:27:44 -04:00
|
|
|
return runList(cmd.Context(), dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display network IDs")
|
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate the output")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.VarP(&options.filter, "filter", "f", `Provide filter values (e.g. "driver=bridge")`)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runList(ctx context.Context, dockerCli command.Cli, options listOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
2024-06-04 03:17:20 -04:00
|
|
|
networkResources, err := client.NetworkList(ctx, network.ListOptions{Filters: options.filter.Value()})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-09-12 16:59:18 -04:00
|
|
|
if len(format) == 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !options.quiet {
|
2016-09-12 16:59:18 -04:00
|
|
|
format = dockerCli.ConfigFile().NetworksFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
} else {
|
2016-09-13 14:21:07 -04:00
|
|
|
format = formatter.TableFormatKey
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 15:49:10 -04:00
|
|
|
sort.Slice(networkResources, func(i, j int) bool {
|
2018-08-03 07:08:01 -04:00
|
|
|
return sortorder.NaturalLess(networkResources[i].Name, networkResources[j].Name)
|
2018-07-06 15:49:10 -04:00
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
networksCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewFormat(format, options.quiet),
|
2017-05-15 08:45:19 -04:00
|
|
|
Trunc: !options.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return FormatWrite(networksCtx, networkResources)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|