-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from heroku/multibuildpack
multibuildpack support
- Loading branch information
Showing
10 changed files
with
203 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ target | |
*.iml | ||
.DS_Store | ||
bin/release | ||
bin/exports | ||
cnb-shim-*.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/buildpack/libbuildpack/logger" | ||
"github.com/heroku/cnb-shim" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 4 { | ||
fmt.Println("Usage:", os.Args[0], "EXPORTS_PATH", "PLATFORM_DIR", "ENV_DIR") | ||
return | ||
} | ||
|
||
exportsPath := os.Args[1] | ||
platformDir := os.Args[2] | ||
envDir := os.Args[3] | ||
|
||
log, err := logger.DefaultLogger(platformDir) | ||
if err != nil { | ||
log.Info(err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
data, err := ioutil.ReadFile(exportsPath) | ||
if err != nil { | ||
log.Info(err.Error()) | ||
os.Exit(2) | ||
} | ||
|
||
if err := cnbshim.DumpExportsFile(string(data), envDir); err != nil { | ||
log.Info(err.Error()) | ||
os.Exit(3) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cnbshim | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func DumpExportsFile(exportsData, envDir string) error { | ||
lines := strings.SplitN(exportsData, "\n", 2) | ||
|
||
for _, line := range lines { | ||
if found, key, value := ParseExportsFileLine(line); found { | ||
err := WriteEnvFile(envDir, key, value) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func ParseExportsFileLine(line string) (bool, string, string) { | ||
components := strings.Split(line, "=") | ||
export := strings.Split(components[0], " ") | ||
if strings.TrimSpace(export[0]) == "export" { | ||
val := strings.TrimSpace(components[1]) | ||
|
||
// Ideally we'd eval, but we don't want to eval things like $PATH embedded in the val | ||
if string(val[0]) == "\"" && string(val[len(val)-1]) == "\"" { | ||
val = val[1: len(val)-1] | ||
} | ||
return true, strings.TrimSpace(export[1]), val | ||
} else { | ||
return false, "", "" | ||
} | ||
|
||
} | ||
|
||
func WriteEnvFile(envDir, filename, value string) error { | ||
return ioutil.WriteFile(filepath.Join(envDir, filename), []byte(value), 0644) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cnbshim_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/heroku/cnb-shim" | ||
) | ||
|
||
func TestWriteEnvFile(t *testing.T) { | ||
envDir, err := ioutil.TempDir("", "env") | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFilename := "FOO" | ||
expected := "bar" | ||
err = cnbshim.WriteEnvFile(envDir, expectedFilename, expected) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFile := filepath.Join(envDir, expectedFilename) | ||
actual, err := ioutil.ReadFile(expectedFile) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
if string(actual) != expected { | ||
t.Errorf("Expected %s; got %s", expected, actual) | ||
} | ||
} | ||
|
||
func TestDumpExportsFile(t *testing.T) { | ||
envDir, err := ioutil.TempDir("", "env") | ||
|
||
exports := ` | ||
export FOO=bar | ||
` | ||
err = cnbshim.DumpExportsFile(exports, envDir) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFile := filepath.Join(envDir, "FOO") | ||
expected := "bar" | ||
actual, err := ioutil.ReadFile(expectedFile) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
if strings.TrimSpace(string(actual)) != expected { | ||
t.Errorf("Expected %s; got %s", expected, actual) | ||
} | ||
} | ||
|
||
func TestDumpExportsFileWithQuotes(t *testing.T) { | ||
envDir, err := ioutil.TempDir("", "env") | ||
|
||
exports := ` | ||
export FOO="bar" | ||
` | ||
err = cnbshim.DumpExportsFile(exports, envDir) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFile := filepath.Join(envDir, "FOO") | ||
expected := "bar" | ||
actual, err := ioutil.ReadFile(expectedFile) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
if strings.TrimSpace(string(actual)) != expected { | ||
t.Errorf("Expected %s; got %s", expected, actual) | ||
} | ||
} | ||
|
||
func TestDumpExportsFileWithEscapedQuotes(t *testing.T) { | ||
envDir, err := ioutil.TempDir("", "env") | ||
|
||
exports := ` | ||
export FOO="b\"a\"r" | ||
` | ||
err = cnbshim.DumpExportsFile(exports, envDir) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFile := filepath.Join(envDir, "FOO") | ||
expected := "b\\\"a\\\"r" | ||
actual, err := ioutil.ReadFile(expectedFile) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
if strings.TrimSpace(string(actual)) != expected { | ||
t.Errorf("Expected %s; got %s", expected, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package releaser | ||
package cnbshim | ||
|
||
import ( | ||
"io/ioutil" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters