File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 6
6
// files.
7
7
package envfile
8
8
9
+ import (
10
+ "github.com/go-pogo/errors"
11
+ "path"
12
+ "path/filepath"
13
+ "runtime"
14
+ )
15
+
9
16
const (
10
17
panicNilFile = "envfile: file must not be nil"
11
18
panicNilFsys = "envfile: fs.FS must not be nil"
12
19
)
20
+
21
+ // Generate encodes and writes an env file in dir based on the provided src.
22
+ // It is meant to be used with go generate to create .env files based on the
23
+ // project's config(s).
24
+ func Generate (dir , filename string , src any ) error {
25
+ if dir == "" {
26
+ _ , dir , _ , _ = runtime .Caller (1 )
27
+ dir = filepath .Dir (dir )
28
+ }
29
+
30
+ if ! path .IsAbs (filename ) {
31
+ filename = filepath .Join (dir , ".env" )
32
+ }
33
+
34
+ enc , err := Create (filename )
35
+ if err != nil {
36
+ return errors .WithStack (err )
37
+ }
38
+
39
+ enc .TakeValues = true
40
+ defer errors .AppendFunc (& err , enc .Close )
41
+
42
+ if err = enc .Encode (src ); err != nil {
43
+ err = errors .WithStack (err )
44
+ return err
45
+ }
46
+ return nil
47
+ }
You can’t perform that action at this time.
0 commit comments