Go, but with gadgets!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
832 B

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/joho/godotenv"
)
func init() {
envFile, err := envPath()
if err != nil {
fmt.Println(err)
return
}
if err := godotenv.Load(envFile); err != nil {
if os.IsNotExist(err) {
if _, err := os.Create(envFile); err != nil {
fmt.Println(err)
return
}
}
fmt.Println(err)
}
}
func envPath() (string, error) {
userConfigDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
configDir := filepath.Join(userConfigDir, "gogo")
if err := os.MkdirAll(configDir, os.ModePerm); err != nil {
return "", err
}
envFile := filepath.Join(configDir, ".env")
_, err = os.Stat(envFile)
if os.IsNotExist(err) {
fi, err := os.Create(envFile)
if err != nil {
return "", err
}
return envFile, fi.Close()
}
return envFile, err
}