dongsanhu4784 2018-02-04 10:53
浏览 2358

在Windows中使用.env文件设置环境变量

I am working on a set of projects that use .env files for configuration, where each .env file exports a number of environment variables. The files are setup like;

export VARIABLE1=foo

I use the Windows Linux subsystem a lot for this, but would like to also be able to run these projects on my windows machine. The projects in question are Golang.

IS there any easy way to programatically set Windows environment variables (preferably termporarily) from these .env files? These is mostly so I can debug within VSCode.

  • 写回答

2条回答 默认 最新

  • dongnanman9093 2018-02-04 12:13
    关注

    Package os

    import "os"
    
    func Clearenv()
    func Environ() []string
    func Expand(s string, mapping func(string) string) string
    func ExpandEnv(s string) string
    func Getenv(key string) string
    func Setenv(key, value string) error
    func Unsetenv(key string) error
    

    Read the env file lines using bufio.Scanner. Parse the lines using the strings package. Use the os package environment variable functions.

    For example,

    package main
    
    import (
        "bufio"
        "bytes"
        "fmt"
        "io"
        "os"
        "strings"
    )
    
    func parseLine(line string) (key, value string) {
        line = strings.TrimSpace(line)
        const export = `export `
        if !strings.HasPrefix(line, export) {
            return "", ""
        }
        line = strings.TrimPrefix(line, export)
        fields := strings.SplitN(line, "=", 2)
        if len(fields) > 0 {
            key = strings.TrimSpace(fields[0])
            if len(fields) > 1 {
                value = strings.TrimSpace(fields[1])
            }
        }
        return key, value
    }
    
    func setenv(r io.Reader) error {
        s := bufio.NewScanner(r)
        for s.Scan() {
            line := s.Text()
            key, value := parseLine(line)
            if len(key) < 1 {
                continue
            }
            err := os.Setenv(key, value)
            if err != nil {
                return err
            }
        }
        if err := s.Err(); err != nil {
            return err
        }
        return nil
    }
    
    func main() {
        envFile := bytes.NewBufferString(
            "export VARIABLE1=foo
    export VARIABLE2=bar
    ",
        )
        err := setenv(envFile)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            return
        }
        for _, key := range []string{`VARIABLE1`, `VARIABLE2`} {
            env, found := os.LookupEnv(key)
            fmt.Println(key, found, env)
        }
    }
    

    Playground: https://play.golang.org/p/pZKQ0GW5eCu

    Output:

    VARIABLE1 true foo
    VARIABLE2 true bar
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题