Skip to content

Commit

Permalink
[action] add working-directory to launch.toml (API>=0.8)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcemaj committed Apr 5, 2022
1 parent 31fc6e3 commit ad0e745
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 3 deletions.
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)
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
148 changes: 147 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,152 @@ 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{
BOM: &libcnb.BOM{Entries: []libcnb.BOMEntry{
{
Name: "test-launch-bom-entry",
Metadata: map[string]interface{}{"test-key": "test-value"},
Launch: true,
},
{
Name: "test-build-bom-entry",
Metadata: map[string]interface{}{"test-key": "test-value"},
},
}},
Labels: []libcnb.Label{
{
Key: "test-key",
Value: "test-value",
},
},
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
WorkingDirectory: "/my/directory/",
},
},
Slices: []libcnb.Slice{
{
Paths: []string{"test-path"},
},
},
}, 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{
Labels: []libcnb.Label{
{
Key: "test-key",
Value: "test-value",
},
},
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
},
},
Slices: []libcnb.Slice{
{
Paths: []string{"test-path"},
},
},
BOM: []libcnb.BOMEntry{
{
Name: "test-launch-bom-entry",
Metadata: map[string]interface{}{"test-key": "test-value"},
Launch: 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{
BOM: &libcnb.BOM{Entries: []libcnb.BOMEntry{
{
Name: "test-launch-bom-entry",
Metadata: map[string]interface{}{"test-key": "test-value"},
Launch: true,
},
{
Name: "test-build-bom-entry",
Metadata: map[string]interface{}{"test-key": "test-value"},
},
}},
Labels: []libcnb.Label{
{
Key: "test-key",
Value: "test-value",
},
},
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
WorkingDirectory: "/my/directory/",
},
},
Slices: []libcnb.Slice{
{
Paths: []string{"test-path"},
},
},
}, 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{
Labels: []libcnb.Label{
{
Key: "test-key",
Value: "test-value",
},
},
Processes: []libcnb.Process{
{
Type: "test-type",
Command: "test-command-in-dir",
Default: true,
WorkingDirectory: "/my/directory/",
},
},
Slices: []libcnb.Slice{
{
Paths: []string{"test-path"},
},
},
BOM: []libcnb.BOMEntry{
{
Name: "test-launch-bom-entry",
Metadata: map[string]interface{}{"test-key": "test-value"},
Launch: true,
},
},
}))
})

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

Expand Down

0 comments on commit ad0e745

Please sign in to comment.