Skip to content

Commit

Permalink
Add envdir package (#6148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdmanv authored and Ilya Kislenko committed Jul 30, 2019
1 parent 5cc1f10 commit 4c97b91
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/envdir/envdir.go
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
}
27 changes: 27 additions & 0 deletions pkg/envdir/envdir_test.go
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"})
}

0 comments on commit 4c97b91

Please sign in to comment.