File: completion.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (62 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package completion

import (
	"strings"

	"github.com/docker/buildx/bake"
	"github.com/docker/buildx/builder"
	"github.com/docker/buildx/store/storeutil"
	"github.com/docker/cli/cli/command"
	"github.com/spf13/cobra"
)

// ValidArgsFn defines a completion func to be returned to fetch completion options
type ValidArgsFn func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)

func Disable(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
	return nil, cobra.ShellCompDirectiveNoSpace
}

func BakeTargets(files []string) ValidArgsFn {
	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		f, err := bake.ReadLocalFiles(files, nil, nil)
		if err != nil {
			return nil, cobra.ShellCompDirectiveError
		}
		tgts, err := bake.ListTargets(f)
		if err != nil {
			return nil, cobra.ShellCompDirectiveError
		}
		var filtered []string
		if toComplete == "" {
			return tgts, cobra.ShellCompDirectiveNoFileComp
		}
		for _, tgt := range tgts {
			if strings.HasPrefix(tgt, toComplete) {
				filtered = append(filtered, tgt)
			}
		}
		return filtered, cobra.ShellCompDirectiveNoFileComp
	}
}

func BuilderNames(dockerCli command.Cli) ValidArgsFn {
	return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		txn, release, err := storeutil.GetStore(dockerCli)
		if err != nil {
			return nil, cobra.ShellCompDirectiveError
		}
		defer release()
		builders, err := builder.GetBuilders(dockerCli, txn)
		if err != nil {
			return nil, cobra.ShellCompDirectiveError
		}
		var filtered []string
		for _, b := range builders {
			if toComplete == "" || strings.HasPrefix(b.Name, toComplete) {
				filtered = append(filtered, b.Name)
			}
		}
		return filtered, cobra.ShellCompDirectiveNoFileComp
	}
}