Skip to content

Commit ef45d4d

Browse files
committed
cli tool command execution
1 parent d4cb480 commit ef45d4d

File tree

6 files changed

+96
-29
lines changed

6 files changed

+96
-29
lines changed

.github/workflows/release.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: goreleaser
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*.*.*"
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
goreleaser:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
- run: git fetch --force --tags
19+
- uses: actions/setup-go@v4
20+
with:
21+
go-version: stable
22+
- uses: goreleaser/goreleaser-action@v4
23+
with:
24+
workdir: ./cmd/goe
25+
distribution: goreleaser
26+
version: latest
27+
args: release --clean
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ About the format of `.env`: [link](https://pkg.go.dev/github.com/compose-spec/co
1818

1919
## CLI tool
2020

21-
Run the command below will load the the `.env` file in the current shell.
21+
Go to the [release page](https://github.com/ysmood/goe/releases) to download the CLI binary.
22+
23+
Usage examples:
2224

2325
```bash
24-
go run github.com/ysmood/goe/cmd/goe@latest
26+
# By default it will use .env file in current working directory to start a new shell.
27+
goe
28+
29+
# Load file file/path/.env.dev as dotenv file.
30+
goe file/path/.env.dev
31+
32+
# If there are arguments after the dotenv, they will be executed without starting a new shell.
33+
goe .env.dev node app.js
2534
```
2635

2736
## Safely share .env file with team members

cmd/goe/main.go

+33-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package main
22

33
import (
44
"errors"
5-
"flag"
65
"fmt"
76
"os"
87
"os/exec"
8+
"slices"
99

1010
"github.com/ysmood/goe"
1111
"github.com/ysmood/goe/pkg/utils"
@@ -17,24 +17,30 @@ var (
1717
)
1818

1919
func main() {
20-
flag.Usage = func() {
21-
fmt.Fprint(flag.CommandLine.Output(), Usage)
22-
flag.PrintDefaults()
23-
}
24-
25-
flag.Parse()
20+
envFile := goe.DOTENV
2621

27-
envFile := flag.Arg(0)
22+
if len(os.Args) == 2 && slices.Contains([]string{"-h", "--help", "-help"}, os.Args[1]) {
23+
utils.Println(USAGE)
24+
os.Exit(0)
25+
}
2826

29-
if envFile == "" {
30-
envFile = goe.DOTENV
27+
if len(os.Args) > 1 {
28+
envFile = os.Args[1]
3129
}
3230

3331
err := goe.AutoLoad(envFile)
3432
if err != nil {
3533
panic(err)
3634
}
3735

36+
if len(os.Args) == 2 { //nolint: mnd
37+
runShell()
38+
} else {
39+
runCommand()
40+
}
41+
}
42+
43+
func runShell() {
3844
shell, err := Shell()
3945
if err != nil {
4046
panic(fmt.Errorf("%w: %w", errGetShell, err))
@@ -57,3 +63,20 @@ func main() {
5763

5864
utils.Println(goe.Prefix, "Unloaded environment variables")
5965
}
66+
67+
func runCommand() {
68+
cmd := exec.Command(os.Args[2], os.Args[3:]...)
69+
cmd.Stderr = os.Stderr
70+
cmd.Stdin = os.Stdin
71+
cmd.Stdout = os.Stdout
72+
cmd.Env = os.Environ()
73+
74+
if err := cmd.Run(); err != nil {
75+
var exitErr exec.ExitError
76+
if errors.Is(err, &exitErr) {
77+
os.Exit(exitErr.ExitCode())
78+
}
79+
80+
panic(fmt.Errorf("%w: %w", errFailRun, err))
81+
}
82+
}

cmd/goe/usage.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package main
22

3-
var Usage = `
3+
var USAGE = `
44
Usage:
55
66
goe [dotenv-file-path]
7+
goe -h | --help | -help
78
89
910
Examples:
1011
11-
# Load .env file in current working directory
12+
# By default it will use .env file in current working directory to start a new shell.
1213
goe
1314
1415
# Load file file/path/.env.dev as dotenv file.
1516
goe file/path/.env.dev
1617
18+
# If there are arguments after the dotenv, they will be executed without starting a new shell.
19+
goe .env.dev node app.js
20+
1721
1822
`

go.mod

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
module github.com/ysmood/goe
22

3-
go 1.22
3+
go 1.22.0
4+
5+
toolchain go1.23.0
46

57
require (
68
github.com/compose-spec/compose-go v1.20.2
7-
github.com/ysmood/got v0.39.4
9+
github.com/ysmood/got v0.40.0
810
)
911

1012
require (
1113
github.com/pkg/errors v0.9.1 // indirect
12-
github.com/sirupsen/logrus v1.9.0 // indirect
13-
golang.org/x/sys v0.1.0 // indirect
14+
github.com/sirupsen/logrus v1.9.3 // indirect
15+
golang.org/x/sys v0.29.0 // indirect
1416
)
1517

1618
require (
1719
github.com/ysmood/gop v0.2.0 // indirect
18-
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848
20+
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
1921
)

go.sum

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ github.com/compose-spec/compose-go v1.20.2/go.mod h1:+MdqXV4RA7wdFsahh/Kb8U0pAJq
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
7-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
7+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
88
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
99
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1010
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1111
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12-
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
13-
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
12+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
13+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1414
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1515
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1616
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1717
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
1818
github.com/ysmood/gop v0.2.0 h1:+tFrG0TWPxT6p9ZaZs+VY+opCvHU8/3Fk6BaNv6kqKg=
1919
github.com/ysmood/gop v0.2.0/go.mod h1:rr5z2z27oGEbyB787hpEcx4ab8cCiPnKxn0SUHt6xzk=
20-
github.com/ysmood/got v0.39.4 h1:8ru7J25Zmf/sMTNYOF2172xVkjQrPMJ3R5d6uymoqL8=
21-
github.com/ysmood/got v0.39.4/go.mod h1:W7DdpuX6skL3NszLmAsC5hT7JAhuLZhByVzHTq874Qg=
22-
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 h1:+iq7lrkxmFNBM7xx+Rae2W6uyPfhPeDWD+n+JgppptE=
23-
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
20+
github.com/ysmood/got v0.40.0 h1:ZQk1B55zIvS7zflRrkGfPDrPG3d7+JOza1ZkNxcc74Q=
21+
github.com/ysmood/got v0.40.0/go.mod h1:W7DdpuX6skL3NszLmAsC5hT7JAhuLZhByVzHTq874Qg=
22+
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
23+
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
2424
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25-
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
26-
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
26+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2727
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2828
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2929
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)