Skip to content

Commit

Permalink
refactor: latest godotenv and cline dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Feb 23, 2022
1 parent adc3487 commit 998c82e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/devel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ jobs:
strategy:
fail-fast: false
matrix:
go: [1.15.x, 1.16.x, 1.17.x]
os: [ubuntu-latest, macos-latest, windows-latest]
go:
- 1.15.x
- 1.16.x
- 1.17.x
os:
- ubuntu-20.04
- macos-11
# - windows-2019
runs-on: ${{ matrix.os}}
steps:
- name: Install
Expand Down
45 changes: 27 additions & 18 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,48 @@ func Execute() {

func appHandler(ctx *cli.AppContext) error {
flags := ctx.Flags
tailArgs := ctx.TailArgs

// 1. Load a .env file if it's available
var err error = nil
file := flags.String("file")
found := fileExists(file)
fileProvided := flags.IsProvidedFlag("file")
file, err := flags.String("file")
if err != nil {
return err
}
fileProvided := file.IsProvided()
filePath := file.Value()
fileFound := fileExists(filePath)

if fileProvided {
if file == "" {
return fmt.Errorf("File path was empty or not provided")
if filePath == "" {
return fmt.Errorf("file path was empty or not provided")
}
if !found {
return fmt.Errorf("File path was not found or inaccessible")
if !fileFound {
return fmt.Errorf("file path was not found or inaccessible")
}
err = godotenv.Load(file)
} else if found {
err = godotenv.Load(file)
err = godotenv.Load(filePath)
} else if fileFound {
err = godotenv.Load(filePath)
}
if err != nil {
return fmt.Errorf("Env file: %v", err)
return fmt.Errorf("env file: %v", err)
}

tailArgs := ctx.TailArgs

// 2. Print all env variables in text format by default
providedFlags := len(flags.GetProvidedFlags())
providedFlags := len(flags.GetProvided())
if (providedFlags == 0 && len(tailArgs) == 0) ||
(providedFlags == 1 && len(tailArgs) == 0 && fileProvided) {
return printEnvText()
}

// 3. Output flag
if flags.IsProvidedFlag("output") {
out := flags.String("output")
// 3. Output
output, err := flags.String("output")
if err != nil {
return err
}
if output.IsProvided() {
out := output.Value()
switch out {
case "json":
return printEnvJSON()
Expand All @@ -99,9 +108,9 @@ func appHandler(ctx *cli.AppContext) error {
return printEnvText()
default:
if out == "" {
return fmt.Errorf("Output format was empty or not provided")
return fmt.Errorf("output format was empty or not provided")
}
return fmt.Errorf("Format `%s` is not supported output", out)
return fmt.Errorf("format `%s` is not a supported output", out)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestPlainEnv(t *testing.T) {
t.Error("error trying to read the .env file")
}

actual := strings.Trim(string(out.Bytes()), "\n")
actual := strings.Trim(out.String(), "\n")

if expected != actual {
t.Error("one or more env keys have wrong values")
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/joseluisq/enve
go 1.15

require (
github.com/joho/godotenv v1.3.0
github.com/joseluisq/cline v0.1.0-beta.6
github.com/joho/godotenv v1.4.0
github.com/joseluisq/cline v0.1.0
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/joseluisq/cline v0.1.0-beta.6 h1:a6ujBrgWEFiXL073dPPvX9EchB0hJZdtymUau2EB7Oo=
github.com/joseluisq/cline v0.1.0-beta.6/go.mod h1:OHx3HRPOMpaxrkgR6pNdPCdcOh55WhP2nT7OiSFLLkA=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/joseluisq/cline v0.1.0 h1:vv5bkJmTYsEdpcMNNpQJRovGEjbhAWvh0JH8dCtTtV8=
github.com/joseluisq/cline v0.1.0/go.mod h1:OHx3HRPOMpaxrkgR6pNdPCdcOh55WhP2nT7OiSFLLkA=

0 comments on commit 998c82e

Please sign in to comment.