github.com/dennys-bd/goals@v0.0.0-20210328114421-251a004d41e3/core/env.go (about)

     1  package core
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	errs "github.com/dennys-bd/goals/shortcuts/errors"
    11  )
    12  
    13  func loadDotEnv(project Project) {
    14  	file, err := os.Open(filepath.Join(project.AbsPath, ".env"))
    15  	if err != nil && !os.IsNotExist(err) {
    16  		errs.Ex(err.Error())
    17  	} else {
    18  		defer file.Close()
    19  		if err == nil {
    20  			scanner := bufio.NewScanner(file)
    21  			line := 1
    22  
    23  			for scanner.Scan() {
    24  				s := strings.SplitN(scanner.Text(), "=", 2)
    25  				if len(s) == 2 {
    26  					os.Setenv(s[0], s[1])
    27  				} else {
    28  					errs.Ex(fmt.Sprintf("Syntax error in .env file. Line %d: \"%s\"\n", line, scanner.Text()))
    29  				}
    30  				line++
    31  			}
    32  		}
    33  	}
    34  }