From ec8e352af570339c7dd433fe73cfba4f63874004 Mon Sep 17 00:00:00 2001 From: yuchanns Date: Fri, 21 Oct 2022 17:52:49 +0800 Subject: [PATCH] Support `nerdctl build` custom outputs alias of type local Signed-off-by: Hanchin Hsieh --- cmd/nerdctl/build.go | 5 +++++ cmd/nerdctl/build_test.go | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/nerdctl/build.go b/cmd/nerdctl/build.go index 42fddfd1ae6..1035e4ade1c 100644 --- a/cmd/nerdctl/build.go +++ b/cmd/nerdctl/build.go @@ -290,6 +290,11 @@ func generateBuildctlArgs(cmd *cobra.Command, buildkitHost string, platform, arg needsLoading = true } } else { + if !strings.Contains(output, "type=") { + // should accept --output as an alias of --output + // type=local,dest= + output = fmt.Sprintf("type=local,dest=%s", output) + } if strings.Contains(output, "type=docker") || strings.Contains(output, "type=oci") { needsLoading = true } diff --git a/cmd/nerdctl/build_test.go b/cmd/nerdctl/build_test.go index 25092f287b1..fcb9812b0e7 100644 --- a/cmd/nerdctl/build_test.go +++ b/cmd/nerdctl/build_test.go @@ -174,9 +174,11 @@ CMD ["echo", "nerdctl-build-test-dockerfile"] func TestBuildLocal(t *testing.T) { t.Parallel() - testutil.DockerIncompatible(t) testutil.RequiresBuild(t) base := testutil.NewBase(t) + if testutil.GetTarget() == testutil.Docker { + base.Env = append(base.Env, "DOCKER_BUILDKIT=1") + } defer base.Cmd("builder", "prune").Run() const testFileName = "nerdctl-build-test" const testContent = "nerdctl" @@ -202,6 +204,16 @@ COPY %s /`, data, err := os.ReadFile(testFilePath) assert.NilError(t, err) assert.Equal(t, string(data), testContent) + + aliasOutputDir := t.TempDir() + testAliasFilePath := filepath.Join(aliasOutputDir, testFileName) + base.Cmd("build", "-o", aliasOutputDir, buildCtx).AssertOK() + if _, err := os.Stat(testAliasFilePath); err != nil { + t.Fatal(err) + } + data, err = os.ReadFile(testAliasFilePath) + assert.NilError(t, err) + assert.Equal(t, string(data), testContent) } func createBuildContext(dockerfile string) (string, error) {