Skip to content

Commit

Permalink
feat: auto load env var from .env file (#286)
Browse files Browse the repository at this point in the history
* feat: auto load env var from .env file

* fix lint

* fix pre run
  • Loading branch information
j178 authored Mar 15, 2024
1 parent 7be4d35 commit 2290238
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ There are three ways to make cookies available to `leetgo`:
> [!NOTE]
> Password authentication is not recommended, and it is not supported by `leetcode.com`.

You can put environment variables in a `.env` file in the project's root directory, and `leetgo` will automatically read them.

## Advanced Usage

### `testcases.txt`
Expand Down
2 changes: 2 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ editor:
> [!NOTE]
> 不推荐使用用户名密码的认证方式, 而且 `leetcode.com` (美国站) 也不支持用户名密码登录.

你可以将这些环境变量放到项目跟目录的 `.env` 文件中,`leetgo` 会自动读取这个文件。

## 进阶用法

### `testcases.txt` 相关
Expand Down
7 changes: 6 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ func gitAvailable() bool {

func initGitRepo(dir string) error {
cmd := exec.Command("git", "init", dir)
return cmd.Run()
err := cmd.Run()
if err != nil {
return err
}
err = utils.WriteOrAppendFile(filepath.Join(dir, ".gitignore"), []byte(".env\n"))
return err
}

func isInsideGitRepo(dir string) bool {
Expand Down
28 changes: 18 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/charmbracelet/log"
cc "github.com/ivanpirog/coloredcobra"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -58,6 +59,23 @@ func Execute() {
}
}

func preRun(cmd *cobra.Command, _ []string) error {
initLogger()
err := initWorkDir()
if err != nil {
return err
}
err = config.Load(cmd == initCmd)
if err != nil {
return err
}
err = godotenv.Load()
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}

func UsageString() string {
return rootCmd.UsageString()
}
Expand Down Expand Up @@ -87,16 +105,6 @@ func initLogger() {
}
}

func preRun(cmd *cobra.Command, args []string) error {
initLogger()
err := initWorkDir()
if err != nil {
return err
}
err = config.Load(cmd == initCmd)
return err
}

func initCommands() {
cobra.EnableCommandSorting = false

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/j178/kooky v0.0.0-20240103045155-c09911743d37
github.com/j178/leetgo/testutils/go v0.2.0
github.com/jedib0t/go-pretty/v6 v6.5.4
github.com/joho/godotenv v1.5.1
github.com/k3a/html2text v1.2.1
github.com/mitchellh/go-homedir v1.1.0
github.com/muesli/reflow v0.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ github.com/j178/leetgo/testutils/go v0.2.0 h1:5ofMFF29RMM0ANCjlwpOR9rkOFtYf/5UTG
github.com/j178/leetgo/testutils/go v0.2.0/go.mod h1:m4VquSOufm2jcOyXW7P52fWa8adQim5XoQSEAJX2hw8=
github.com/jedib0t/go-pretty/v6 v6.5.4 h1:gOGo0613MoqUcf0xCj+h/V3sHDaZasfv152G6/5l91s=
github.com/jedib0t/go-pretty/v6 v6.5.4/go.mod h1:5LQIxa52oJ/DlDSLv0HEkWOFMDGoWkJb9ss5KqPpJBg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/k3a/html2text v1.2.1 h1:nvnKgBvBR/myqrwfLuiqecUtaK1lB9hGziIJKatNFVY=
Expand Down
19 changes: 19 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func CreateIfNotExists(path string, isDir bool) error {
return err
}
f.Close()
} else {
return err
}
}
return nil
Expand All @@ -54,6 +56,23 @@ func WriteFile(file string, content []byte) error {
return nil
}

func WriteOrAppendFile(file string, content []byte) error {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return WriteFile(file, content)
}
return err
}
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(content)
return err
}

func RemoveIfExist(path string) error {
err := os.Remove(path)
if os.IsNotExist(err) {
Expand Down

0 comments on commit 2290238

Please sign in to comment.