Skip to content

Commit 5dd49d6

Browse files
committed
Allow an ENV var for APPPACK_TOML to specify the path to the toml file. See Issue #10
1 parent 9a75db4 commit 5dd49d6

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

builder/build/apppacktoml.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,32 @@ func (a *AppPackToml) GetTestEnv() map[string]string {
103103
func ParseAppPackToml(ctx context.Context) (*AppPackToml, error) {
104104
var config AppPackToml
105105
// if the file doesn't exist, just return an empty config
106-
if _, err := os.Stat("apppack.toml"); os.IsNotExist(err) {
107-
log.Ctx(ctx).Debug().Msg("apppack.toml not found")
106+
filename := GetFilename()
107+
if _, err := os.Stat(filename); os.IsNotExist(err) {
108+
log.Ctx(ctx).Debug().Msg(fmt.Sprintf("%s not found", filename))
108109
return &config, nil
109110
}
110-
if _, err := toml.DecodeFile("apppack.toml", &config); err != nil {
111+
if _, err := toml.DecodeFile(filename, &config); err != nil {
111112
return nil, err
112113
}
113114
return &config, nil
114115
}
115116

116117
func (a AppPackToml) Write(ctx context.Context) error {
117-
log.Ctx(ctx).Debug().Msg("writing apppack.toml")
118-
f, err := os.Create("apppack.toml")
118+
filename := GetFilename()
119+
log.Ctx(ctx).Debug().Msg(fmt.Sprintf("writing %s", filename))
120+
f, err := os.Create(filename)
119121
if err != nil {
120122
return err
121123
}
122124
defer f.Close()
123125
return toml.NewEncoder(f).Encode(a)
124126
}
127+
128+
func GetFilename() string {
129+
filename := "apppack.toml"
130+
if envFile := os.Getenv("APPPACK_TOML"); envFile != "" {
131+
filename = envFile
132+
}
133+
return filename
134+
}

builder/build/prebuild.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,16 @@ func (b *Build) ConvertAppJson() error {
148148
return err
149149
}
150150
// check if apppack.toml file exists
151-
apppackTomlExists, err := b.state.FileExists("apppack.toml")
151+
filename := GetFilename()
152+
apppackTomlExists, err := b.state.FileExists(filename)
152153
if err != nil {
153154
return err
154155
}
155156
if appJsonExists && !apppackTomlExists {
156157
// convert app.json to apppack.toml
157-
b.Log().Info().Msg("Converting app.json to apppack.toml")
158+
b.Log().Info().Msg(fmt.Sprintf("Converting %s to %s", "app.json", filename))
158159
t := b.AppJSON.ToApppackToml()
159-
return b.state.WriteTomlToFile("apppack.toml", t)
160+
return b.state.WriteTomlToFile(filename, t)
160161
}
161162
return nil
162163
}

builder/filesystem/filesystem.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ func (f *FileState) Log() *zerolog.Logger {
5858

5959
func (f *FileState) CreateIfNotExists() error {
6060
// touch files codebuild expects to exist
61-
for _, filename := range []string{"apppack.toml", "build.log", "metadata.toml", "test.log"} {
61+
apppackToml := GetFilename()
62+
63+
for _, filename := range []string{apppackToml, "build.log", "metadata.toml", "test.log"} {
6264
exists, err := f.FileExists(filename)
6365
if err != nil {
6466
return err

builder/filesystem/filesystem_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"archive/tar"
55
"bytes"
66
"context"
7+
"fmt"
78
"io"
89
"os"
910
"testing"
@@ -16,8 +17,11 @@ var testContext = zerolog.New(os.Stdout).With().Timestamp().Logger().WithContext
1617

1718
func TestCreateIfNotExists(t *testing.T) {
1819
fs := afero.Afero{Fs: afero.NewMemMapFs()}
19-
if _, err := fs.Stat("apppack.toml"); !os.IsNotExist(err) {
20-
t.Error("apppack.toml should not exist")
20+
21+
filename := GetFilename()
22+
23+
if _, err := fs.Stat(filename); !os.IsNotExist(err) {
24+
t.Error(fmt.Sprintf("%s should not exist", filename))
2125
}
2226
s := &FileState{
2327
fs: fs,
@@ -27,8 +31,8 @@ func TestCreateIfNotExists(t *testing.T) {
2731
if err != nil {
2832
t.Error(err)
2933
}
30-
if _, err := fs.Stat("apppack.toml"); os.IsNotExist(err) {
31-
t.Error("apppack.toml should exist")
34+
if _, err := fs.Stat(filename); os.IsNotExist(err) {
35+
t.Error(fmt.Sprintf("%s should exist", filename))
3236
}
3337
}
3438

@@ -105,6 +109,26 @@ func TestWriteEnvFile(t *testing.T) {
105109
}
106110
}
107111

112+
func TestGetFilename(t *testing.T) {
113+
// Check that there is no env variable set
114+
if os.Getenv("APPPACK_TOML") != "" {
115+
t.Error("APPPACK_TOML env variable should not be set")
116+
}
117+
// Call GetFilename and check the default value
118+
119+
filename := GetFilename()
120+
if filename != "apppack.toml" {
121+
t.Errorf("expected apppack.toml, got %s", filename)
122+
}
123+
124+
// Set the env variable and check again
125+
os.Setenv("APPPACK_TOML", "custom.toml")
126+
filename = GetFilename()
127+
if filename != "custom.toml" {
128+
t.Errorf("expected custom.toml, got %s", filename)
129+
}
130+
}
131+
108132
func dummyTarBuffer() (*io.Reader, error) {
109133
var buf bytes.Buffer
110134
tw := tar.NewWriter(&buf)

0 commit comments

Comments
 (0)