@@ -192,40 +192,42 @@ type PromptFunc func(key string, value string) (string, error)
192192// prompting for others, and returning the result as a map.
193193func InstantiateDotEnv (ctx context.Context , rootDir string , substitutions map [string ]string , verbose bool , prompt PromptFunc ) (map [string ]string , error ) {
194194 promptedVars := map [string ]string {}
195-
196195 envExamplePath := path .Join (rootDir , EnvExampleFile )
196+
197197 stat , err := os .Stat (envExamplePath )
198- if err != nil {
198+ if err != nil && ! errors . Is ( err , fs . ErrNotExist ) {
199199 return nil , err
200- }
201- if stat .IsDir () {
202- return nil , errors .New ("env.example file is a directory" )
203- }
200+ } else if stat != nil {
201+ if stat .IsDir () {
202+ return nil , errors .New ("env.example file is a directory" )
203+ }
204204
205- envMap , err := godotenv .Read (envExamplePath )
206- if err != nil {
207- return nil , err
208- }
205+ envMap , err := godotenv .Read (envExamplePath )
206+ if err != nil {
207+ return nil , err
208+ }
209209
210- for key , oldValue := range envMap {
211- // if key is a substitution, replace it
212- if value , ok := substitutions [key ]; ok {
213- envMap [key ] = value
214- // if key was already promped, use that value
215- } else if alreadyPromptedValue , ok := promptedVars [key ]; ok {
216- envMap [key ] = alreadyPromptedValue
217- } else {
218- // prompt for value
219- newValue , err := prompt (key , oldValue )
220- if err != nil {
221- return nil , err
210+ for key , oldValue := range envMap {
211+ // if key is a substitution, replace it
212+ if value , ok := substitutions [key ]; ok {
213+ envMap [key ] = value
214+ // if key was already promped, use that value
215+ } else if alreadyPromptedValue , ok := promptedVars [key ]; ok {
216+ envMap [key ] = alreadyPromptedValue
217+ } else {
218+ // prompt for value
219+ newValue , err := prompt (key , oldValue )
220+ if err != nil {
221+ return nil , err
222+ }
223+ envMap [key ] = newValue
224+ promptedVars [key ] = newValue
222225 }
223- envMap [key ] = newValue
224- promptedVars [key ] = newValue
225226 }
227+ return envMap , nil
228+ } else {
229+ return substitutions , nil
226230 }
227-
228- return envMap , nil
229231}
230232
231233func PrintDotEnv (envMap map [string ]string ) error {
@@ -243,7 +245,7 @@ func WriteDotEnv(rootDir string, envMap map[string]string) error {
243245 return err
244246 }
245247 envLocalPath := path .Join (rootDir , EnvLocalFile )
246- return os .WriteFile (envLocalPath , []byte (envContents ), 0700 )
248+ return os .WriteFile (envLocalPath , []byte (envContents + " \n " ), 0700 )
247249}
248250
249251func CloneTemplate (url , dir string ) (string , string , error ) {
0 commit comments