Skip to content

Instantly share code, notes, and snippets.

@j0hnsmith
Last active August 27, 2020 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j0hnsmith/7ff5dd5a9839a6cf7e8a8ae89e8c1b02 to your computer and use it in GitHub Desktop.
Save j0hnsmith/7ff5dd5a9839a6cf7e8a8ae89e8c1b02 to your computer and use it in GitHub Desktop.
.env file to fish

.env to fish shell

install

  • go build -o readenv cmd/readenv/main.go
  • mv readenv /usr/local/bin/readenv

Usage

eval (readenv .env)

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
path := os.Args[1:]
if len(path) == 0 {
os.Stderr.Write([]byte("file path is a required arg\n"))
os.Exit(1)
}
if len(path) > 1 {
os.Stderr.Write([]byte("only 1 path is allowed\n"))
os.Exit(1)
}
fp, err := os.Open(path[0])
if err != nil {
os.Stderr.Write([]byte(fmt.Sprintf("unable to open file %s: %s\n", path[0], err)))
os.Exit(1)
}
s := bufio.NewScanner(fp)
for s.Scan() {
t := strings.TrimSpace(s.Text())
if len(t) == 0 || t[0] == '#' {
continue
}
tokens := strings.Split(t, "=")
if len(tokens) != 2 {
os.Stdout.Write([]byte(fmt.Sprintf("# invalid token: %s;\n", t)))
continue
}
os.Stdout.Write([]byte(fmt.Sprintf("set -x %s %s;\n", tokens[0], tokens[1])))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment