mirror of
https://github.com/silicoflare/envy.git
synced 2026-05-26 19:57:59 +05:30
162 lines
3.0 KiB
Go
162 lines
3.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/pelletier/go-toml"
|
|
)
|
|
|
|
func StopIfErr(err error) {
|
|
if err != nil {
|
|
ErrPrint(err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func GetEnvy() (*toml.Tree, error) {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return nil, errors.New("Not able to fetch working directory: " + err.Error())
|
|
}
|
|
|
|
_, err = os.Stat(path.Join(cwd, "envy.toml"))
|
|
|
|
// file exists
|
|
if err == nil {
|
|
envyFile, err := toml.LoadFile(path.Join(cwd, "envy.toml"))
|
|
if err != nil {
|
|
return nil, errors.New("Error occured while reading envy.toml file:" + err.Error())
|
|
}
|
|
return envyFile, nil
|
|
}
|
|
|
|
// file does not exist
|
|
if os.IsNotExist(err) {
|
|
return nil, errors.New("envy.toml file doesn't exist in the current directory")
|
|
} else {
|
|
return nil, errors.New("Some error occured:" + err.Error())
|
|
}
|
|
}
|
|
|
|
func GetKey(t *toml.Tree, key string) (string, error) {
|
|
v := t.Get(key)
|
|
if v == nil {
|
|
return "", fmt.Errorf("key %v not found", v)
|
|
}
|
|
s, ok := v.(string)
|
|
if !ok {
|
|
return "", fmt.Errorf("key %q is %T, expected string", key, v)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func WriteEnvy(tree *toml.Tree) error {
|
|
data, err := tree.Marshal()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
os.WriteFile(path.Join(cwd, "envy.toml"), data, 0744)
|
|
|
|
return nil
|
|
}
|
|
|
|
func ParseEnv(env string) string {
|
|
re := regexp.MustCompile(`^\s*(#?)\s*([A-Z0-9_]+)\s*=\s*(?:"([^"\n]+)"|([^"\n]+))$`)
|
|
|
|
var envs string
|
|
|
|
for _, line := range strings.Split(string(env), "\n") {
|
|
matches := re.FindStringSubmatch(line)
|
|
var key, value string
|
|
var enabled bool
|
|
if matches != nil {
|
|
enabled = matches[1] == ""
|
|
key = matches[2] // the key
|
|
value = matches[3] // quoted value
|
|
if value == "" {
|
|
value = matches[4] // unquoted value
|
|
}
|
|
var comment = ""
|
|
if !enabled {
|
|
comment = "#"
|
|
}
|
|
envs += fmt.Sprintf("%v%v=%v\n", comment, key, value)
|
|
}
|
|
}
|
|
|
|
return envs
|
|
}
|
|
|
|
func RefactorEnv() error {
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
envFile, err := os.ReadFile(path.Join(".env"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
parsed := ParseEnv(string(envFile))
|
|
|
|
err = os.WriteFile(path.Join(cwd, ".env"), []byte(parsed), 0755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type Env struct {
|
|
Enabled bool
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
func ParseEnvToStruct(env string) []Env {
|
|
re := regexp.MustCompile(`^\s*(#?)\s*([A-Z0-9_]+)\s*=\s*(?:"([^"\n]+)"|([^"\n]+))$`)
|
|
|
|
var envs []Env
|
|
|
|
for _, line := range strings.Split(string(env), "\n") {
|
|
matches := re.FindStringSubmatch(line)
|
|
var env Env
|
|
if matches != nil {
|
|
env.Enabled = matches[1] == ""
|
|
env.Key = matches[2] // the key
|
|
env.Value = matches[3] // quoted value
|
|
if env.Value == "" {
|
|
env.Value = matches[4] // unquoted value
|
|
}
|
|
envs = append(envs, env)
|
|
}
|
|
}
|
|
|
|
return envs
|
|
}
|
|
|
|
func ParseEnvStruct(envs []Env) string {
|
|
var env string
|
|
|
|
for _, en := range envs {
|
|
comment := ""
|
|
if !en.Enabled {
|
|
comment = "#"
|
|
}
|
|
env += fmt.Sprintf("%v%v=%v\n", comment, en.Key, en.Value)
|
|
}
|
|
|
|
return env
|
|
}
|