-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package envdir | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func envDir(dir string) ([]string, error) { | ||
fis, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to read env from dir:"+dir) | ||
} | ||
e := make([]string, 0, len(fis)) | ||
for _, fi := range fis { | ||
if fi.IsDir() { | ||
continue | ||
} | ||
p := filepath.Join(dir, fi.Name()) | ||
f, err := os.Open(p) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to read env from dir:"+dir) | ||
} | ||
c, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to read env from dir:"+dir) | ||
} | ||
e = append(e, fmt.Sprintf("%s=%s", fi.Name(), c)) | ||
} | ||
return e, nil | ||
} |
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,27 @@ | ||
package envdir | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
. "gopkg.in/check.v1" | ||
) | ||
|
||
// Hook up gocheck into the "go test" runner. | ||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type EnvDirSuite struct{} | ||
|
||
var _ = Suite(&EnvDirSuite{}) | ||
|
||
func (s *EnvDirSuite) TestEnvDir(c *C) { | ||
d := c.MkDir() | ||
p := filepath.Join(d, "FOO") | ||
err := ioutil.WriteFile(p, []byte("BAR"), os.ModePerm) | ||
c.Assert(err, IsNil) | ||
e, err := envDir(d) | ||
c.Assert(err, IsNil) | ||
c.Assert(e, DeepEquals, []string{"FOO=BAR"}) | ||
} |