@@ -21,13 +21,13 @@ import (
2121 "context"
2222 "encoding/json"
2323 "errors"
24+ "fmt"
2425 "io"
2526 "io/fs"
2627 "net/http"
2728 "os"
2829 "os/exec"
2930 "path"
30- "path/filepath"
3131 "runtime"
3232 "strings"
3333
@@ -122,7 +122,14 @@ func FetchSandboxDetails(ctx context.Context, sid, token, serverURL string) (*Sa
122122}
123123
124124func ParseTaskfile (rootPath string ) (* ast.Taskfile , error ) {
125- file , err := os .ReadFile (path .Join (rootPath , TaskFile ))
125+ taskfilePath := path .Join (rootPath , TaskFile )
126+
127+ // taskfile.yaml is optional
128+ if _ , err := os .Stat (taskfilePath ); err != nil && errors .Is (err , fs .ErrNotExist ) {
129+ return nil , nil
130+ }
131+
132+ file , err := os .ReadFile (taskfilePath )
126133 if err != nil {
127134 return nil , err
128135 }
@@ -180,54 +187,62 @@ func NewTask(ctx context.Context, tf *ast.Taskfile, dir, taskName string, verbos
180187
181188type PromptFunc func (key string , value string ) (string , error )
182189
183- // Recursively walk the repo, reading in any .env.example file if present in
184- // that directory, replacing all `substitutions`, prompting for others, and
185- // writing to .env.local in that directory.
186- func InstantiateDotEnv (ctx context.Context , rootDir string , substitutions map [string ]string , verbose bool , prompt PromptFunc ) error {
190+ // Read .env.example file if present in rootDir, replacing all `substitutions`,
191+ // prompting for others, and returning the result as a map.
192+ func InstantiateDotEnv (ctx context.Context , rootDir string , substitutions map [string ]string , verbose bool , prompt PromptFunc ) (map [string ]string , error ) {
187193 promptedVars := map [string ]string {}
188194
189- return filepath .WalkDir (rootDir , func (filePath string , d fs.DirEntry , err error ) error {
190- if err != nil {
191- return err
192- }
193-
194- if d .Name () == EnvExampleFile {
195- envMap , err := godotenv .Read (filePath )
196- if err != nil {
197- return err
198- }
195+ envExamplePath := path .Join (rootDir , EnvExampleFile )
196+ stat , err := os .Stat (envExamplePath )
197+ if err != nil {
198+ return nil , err
199+ }
200+ if stat .IsDir () {
201+ return nil , errors .New ("env.example file is a directory" )
202+ }
199203
200- for key , oldValue := range envMap {
201- // if key is a substitution, replace it
202- if value , ok := substitutions [key ]; ok {
203- envMap [key ] = value
204- // if key was already promped, use that value
205- } else if alreadyPromptedValue , ok := promptedVars [key ]; ok {
206- envMap [key ] = alreadyPromptedValue
207- } else {
208- // prompt for value
209- newValue , err := prompt (key , oldValue )
210- if err != nil {
211- return err
212- }
213- envMap [key ] = newValue
214- promptedVars [key ] = newValue
215- }
216- }
204+ envMap , err := godotenv .Read (envExamplePath )
205+ if err != nil {
206+ return nil , err
207+ }
217208
218- envContents , err := godotenv .Marshal (envMap )
209+ for key , oldValue := range envMap {
210+ // if key is a substitution, replace it
211+ if value , ok := substitutions [key ]; ok {
212+ envMap [key ] = value
213+ // if key was already promped, use that value
214+ } else if alreadyPromptedValue , ok := promptedVars [key ]; ok {
215+ envMap [key ] = alreadyPromptedValue
216+ } else {
217+ // prompt for value
218+ newValue , err := prompt (key , oldValue )
219219 if err != nil {
220- return err
221- }
222-
223- envLocalPath := path .Join (path .Dir (filePath ), EnvLocalFile )
224- if err := os .WriteFile (envLocalPath , []byte (envContents ), 0700 ); err != nil {
225- return err
220+ return nil , err
226221 }
222+ envMap [key ] = newValue
223+ promptedVars [key ] = newValue
227224 }
225+ }
226+
227+ return envMap , nil
228+ }
228229
229- return nil
230- })
230+ func PrintDotEnv (envMap map [string ]string ) error {
231+ envContents , err := godotenv .Marshal (envMap )
232+ if err != nil {
233+ return err
234+ }
235+ _ , err = fmt .Println (envContents )
236+ return err
237+ }
238+
239+ func WriteDotEnv (rootDir string , envMap map [string ]string ) error {
240+ envContents , err := godotenv .Marshal (envMap )
241+ if err != nil {
242+ return err
243+ }
244+ envLocalPath := path .Join (rootDir , EnvLocalFile )
245+ return os .WriteFile (envLocalPath , []byte (envContents ), 0700 )
231246}
232247
233248func CloneTemplate (url , dir string ) (string , string , error ) {
0 commit comments