Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completion: let nerdctl build --target <TAB> show Dockerfile stage names #566

Open
AkihiroSuda opened this issue Nov 25, 2021 · 2 comments
Labels

Comments

@AkihiroSuda
Copy link
Member

https://github.com/docker/cli/blob/9bc104eff0798097954f5d9bc25ca93f892e63f5/contrib/completion/bash/docker#L2929-L2941

Docker uses sed, but we can probably use real Dockerfile parser package
https://pkg.go.dev/github.com/moby/buildkit/frontend/dockerfile/parser#Parse

@djdongjin
Copy link
Member

djdongjin commented Nov 11, 2022

I did a try on this but encountered a wired go mod tidy issue while importing buildkit 🤔 (specifically, "github.com/moby/buildkit/frontend/dockerfile/instructions"

➜  nerdctl git:(build-stage-complete) ✗ go mod tidy
go: finding module for package github.com/docker/libnetwork/ipamutils
go: found github.com/docker/libnetwork/ipamutils in github.com/docker/libnetwork v0.5.6
go: finding module for package github.com/Sirupsen/logrus
go: found github.com/Sirupsen/logrus in github.com/Sirupsen/logrus v1.9.0
go: github.com/containerd/nerdctl/cmd/nerdctl imports
        github.com/moby/buildkit/frontend/dockerfile/instructions imports
        github.com/docker/docker/opts imports
        github.com/docker/libnetwork/ipamutils imports
        github.com/docker/libnetwork/osl imports
        github.com/Sirupsen/logrus: github.com/Sirupsen/[email protected]: parsing go.mod:
        module declares its path as: github.com/sirupsen/logrus
                but was required as: github.com/Sirupsen/logrus

The change is not much

// https://github.com/containerd/nerdctl/blob/7dfbaa2122628921febeb097e7a8a86074dc931d/cmd/nerdctl/completion.go
func shellCompleteDockerfile(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
	filename := buildkitutil.DefaultDockerfileName
	if df, err := cmd.Flags().GetString("file"); err == nil && df != "" {
		filename = df
	}

	df, err := os.ReadFile(filename)
	if err != nil {
		return nil, cobra.ShellCompDirectiveError
	}
	ast, err := parser.Parse(bytes.NewBuffer(df))
	stages, _, err := instructions.Parse(ast.AST)

	candidates := []string{}
	for _, stage := range stages {
		candidates = append(candidates, stage.Name)
	}

	return nil, cobra.ShellCompDirectiveNoFileComp
}

Will visit this later.

@yuchanns
Copy link
Contributor

I did a try on this but encountered a wired go mod tidy issue while importing buildkit 🤔 (specifically, "github.com/moby/buildkit/frontend/dockerfile/instructions"

FYI sirupsen/logrus#1041

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants