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

Add support for working-directory in launch.toml (API>=0.8) #133

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Process struct {
// Command is exec'd directly by the os (no profile.d scripts run)
Direct bool `toml:"direct,omitempty"`

// WorkingDirectory is a directory to execute the command in, removes the need to use a shell environment to CD into working directory
WorkingDirectory string `toml:"working-directory,omitempty"`

// Default can be set to true to indicate that the process
// type being defined should be the default process type for the app image.
Default bool `toml:"default,omitempty"`
Expand Down
13 changes: 11 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func Build(builder Builder, options ...Option) {
logger.Debugf("Buildpack: %+v", ctx.Buildpack)

API := strings.TrimSpace(ctx.Buildpack.API)
if API != "0.5" && API != "0.6" && API != "0.7" {
config.exitHandler.Error(errors.New("this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, and 0.7"))
if API != "0.5" && API != "0.6" && API != "0.7" && API != "0.8" {
config.exitHandler.Error(errors.New("this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, 0.7 and 0.8"))
return
}

Expand Down Expand Up @@ -346,6 +346,15 @@ func Build(builder Builder, options ...Option) {
}
}

if API != "0.8" {
for i, process := range launch.Processes {
if process.WorkingDirectory != "" {
logger.Infof("WARNING: Launch layer is setting working-directory=%s, but that is not supported until API version 0.8. This setting will be ignored.", process.WorkingDirectory)
dmikusa marked this conversation as resolved.
Show resolved Hide resolved
launch.Processes[i].WorkingDirectory = ""
}
}
}

if err = config.tomlWriter.Write(file, launch); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write application metadata %s\n%w", file, err))
return
Expand Down
68 changes: 67 additions & 1 deletion build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ version = "1.1.1"
)

Expect(exitHandler.Calls[0].Arguments.Get(0)).To(MatchError(
"this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, and 0.7",
"this version of libcnb is only compatible with buildpack APIs 0.5, 0.6, 0.7 and 0.8",
))
})
})
Expand Down Expand Up @@ -549,6 +549,72 @@ version = "1.1.1"
}))
})

it("ignore working-directory setting and writes launch.toml (API<0.8)", func() {
builder.On("Build", mock.Anything).Return(libcnb.BuildResult{
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
WorkingDirectory: "/my/directory/",
},
},
}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)

Expect(tomlWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(layersPath, "launch.toml")))
Expect(tomlWriter.Calls[0].Arguments[1]).To(Equal(libcnb.LaunchTOML{
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
},
},
}))
})

it("writes launch.toml with working-directory setting(API>=0.8)", func() {
var b bytes.Buffer
err := buildpackTOML.Execute(&b, map[string]string{"APIVersion": "0.8"})
Expect(err).ToNot(HaveOccurred())

Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
builder.On("Build", mock.Anything).Return(libcnb.BuildResult{
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
WorkingDirectory: "/my/directory/",
},
},
}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)

Expect(tomlWriter.Calls[0].Arguments[0]).To(Equal(filepath.Join(layersPath, "launch.toml")))
Expect(tomlWriter.Calls[0].Arguments[1]).To(Equal(libcnb.LaunchTOML{
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
WorkingDirectory: "/my/directory/",
},
},
}))
})

it("writes persistent metadata", func() {
m := map[string]interface{}{"test-key": "test-value"}

Expand Down