Skip to content

Commit

Permalink
cli tool command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jan 13, 2025
1 parent d4cb480 commit ef45d4d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 29 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: goreleaser

on:
push:
tags:
- "v*.*.*"

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v4
with:
go-version: stable
- uses: goreleaser/goreleaser-action@v4
with:
workdir: ./cmd/goe
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ About the format of `.env`: [link](https://pkg.go.dev/github.com/compose-spec/co

## CLI tool

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

Usage examples:

```bash
go run github.com/ysmood/goe/cmd/goe@latest
# By default it will use .env file in current working directory to start a new shell.
goe

# Load file file/path/.env.dev as dotenv file.
goe file/path/.env.dev

# If there are arguments after the dotenv, they will be executed without starting a new shell.
goe .env.dev node app.js
```

## Safely share .env file with team members
Expand Down
43 changes: 33 additions & 10 deletions cmd/goe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"errors"
"flag"
"fmt"
"os"
"os/exec"
"slices"

"github.com/ysmood/goe"
"github.com/ysmood/goe/pkg/utils"
Expand All @@ -17,24 +17,30 @@ var (
)

func main() {
flag.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), Usage)
flag.PrintDefaults()
}

flag.Parse()
envFile := goe.DOTENV

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

if envFile == "" {
envFile = goe.DOTENV
if len(os.Args) > 1 {
envFile = os.Args[1]
}

err := goe.AutoLoad(envFile)
if err != nil {
panic(err)
}

if len(os.Args) == 2 { //nolint: mnd
runShell()
} else {
runCommand()
}
}

func runShell() {
shell, err := Shell()
if err != nil {
panic(fmt.Errorf("%w: %w", errGetShell, err))
Expand All @@ -57,3 +63,20 @@ func main() {

utils.Println(goe.Prefix, "Unloaded environment variables")
}

func runCommand() {
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Env = os.Environ()

if err := cmd.Run(); err != nil {
var exitErr exec.ExitError
if errors.Is(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}

panic(fmt.Errorf("%w: %w", errFailRun, err))
}
}
8 changes: 6 additions & 2 deletions cmd/goe/usage.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package main

var Usage = `
var USAGE = `
Usage:
goe [dotenv-file-path]
goe -h | --help | -help
Examples:
# Load .env file in current working directory
# By default it will use .env file in current working directory to start a new shell.
goe
# Load file file/path/.env.dev as dotenv file.
goe file/path/.env.dev
# If there are arguments after the dotenv, they will be executed without starting a new shell.
goe .env.dev node app.js
`
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module github.com/ysmood/goe

go 1.22
go 1.22.0

toolchain go1.23.0

require (
github.com/compose-spec/compose-go v1.20.2
github.com/ysmood/got v0.39.4
github.com/ysmood/got v0.40.0
)

require (
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
golang.org/x/sys v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.29.0 // indirect
)

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

0 comments on commit ef45d4d

Please sign in to comment.