Browse Source

Add env loader

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main
jolheiser 1 month ago
parent
commit
b1e73dbde1
Signed by: jolheiser GPG Key ID: B853ADA5DA7BBF7A
  1. 48
      env.go
  2. 2
      go.mod
  3. 2
      go.sum
  4. 4
      install.go
  5. 6
      main.go

48
env.go

@ -0,0 +1,48 @@
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
}

2
go.mod

@ -1,3 +1,5 @@
module go.jolheiser.com/gogo
go 1.17
require github.com/joho/godotenv v1.4.0

2
go.sum

@ -0,0 +1,2 @@
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

4
install.go

@ -24,7 +24,7 @@ func install() error {
return err
}
if err := os.Rename(original, filepath.Join(filepath.Dir(original), "_" + filepath.Base(original))); err != nil {
if err := os.Rename(original, filepath.Join(filepath.Dir(original), "_"+filepath.Base(original))); err != nil {
return err
}
@ -46,7 +46,7 @@ func uninstall() error {
return err
}
original := filepath.Join(filepath.Dir(this), "_" + filepath.Base(this))
original := filepath.Join(filepath.Dir(this), "_"+filepath.Base(this))
if err := os.Remove(this); err != nil {
return err

6
main.go

@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
@ -48,15 +47,14 @@ func main() {
}
// Run Go
binPath, err := exec.LookPath("go")
if err != nil {
log.Println("go cannot be found in PATH")
fmt.Println("go cannot be found in PATH")
return
}
if isInstalled() {
binPath = filepath.Join(filepath.Dir(binPath), "_" + filepath.Base(binPath))
binPath = filepath.Join(filepath.Dir(binPath), "_"+filepath.Base(binPath))
}
args := os.Args[1:]

Loading…
Cancel
Save