-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add goe cli tool to load .env to current shell
- Loading branch information
Showing
12 changed files
with
191 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,26 @@ | ||
run: | ||
skip-dirs: | ||
- pkg/envparse | ||
|
||
linters: | ||
enable-all: true | ||
disable: | ||
- depguard | ||
- revive | ||
- forbidigo | ||
- gochecknoinits | ||
- paralleltest | ||
- wrapcheck | ||
- gosec | ||
- gochecknoglobals | ||
- varnamelen | ||
- musttag | ||
- depguard | ||
- forcetypeassert | ||
- varnamelen | ||
- ireturn | ||
- prealloc | ||
- stylecheck | ||
- tagalign | ||
- govet | ||
- err113 | ||
- nlreturn | ||
- wsl | ||
|
||
# Deprecated ones: | ||
- execinquery | ||
- gomnd | ||
- exportloopref | ||
|
||
- structcheck | ||
- interfacer | ||
- deadcode | ||
- varcheck | ||
- ifshort | ||
- exhaustivestruct | ||
- golint | ||
- maligned | ||
- nosnakecase | ||
- scopelint | ||
issues: | ||
exclude-files: | ||
- cmd/goe/shell.go | ||
- pkg/dotenv/parser.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/ysmood/goe" | ||
_ "github.com/ysmood/goe/load" | ||
"github.com/ysmood/goe/pkg/utils" | ||
) | ||
|
||
var ( | ||
errGetShell = errors.New("failed to get shell") | ||
errFailRun = errors.New("failed to run shell") | ||
) | ||
|
||
func main() { | ||
shell, err := Shell() | ||
if err != nil { | ||
panic(fmt.Errorf("%w: %w", errGetShell, err)) | ||
} | ||
|
||
cmd := exec.Command(shell) | ||
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)) | ||
} | ||
|
||
utils.Println(goe.Prefix, "Unloaded environment variables") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// Shell https://github.com/riywo/loginshell/blob/master/loginshell.go | ||
func Shell() (string, error) { | ||
switch runtime.GOOS { | ||
case "plan9": | ||
return plan9Shell() | ||
case "linux": | ||
return nixShell() | ||
case "openbsd": | ||
return nixShell() | ||
case "freebsd": | ||
return nixShell() | ||
case "android": | ||
return androidShell() | ||
case "darwin": | ||
return darwinShell() | ||
case "windows": | ||
return windowsShell() | ||
} | ||
|
||
return "", errors.New("undefined GOOS: " + runtime.GOOS) | ||
} | ||
|
||
func plan9Shell() (string, error) { | ||
if _, err := os.Stat("/dev/osversion"); err != nil { | ||
if os.IsNotExist(err) { | ||
return "", err | ||
} | ||
return "", errors.New("/dev/osversion check failed") | ||
} | ||
|
||
return "/bin/rc", nil | ||
} | ||
|
||
func nixShell() (string, error) { | ||
user, err := user.Current() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
out, err := exec.Command("getent", "passwd", user.Uid).Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
ent := strings.Split(strings.TrimSuffix(string(out), "\n"), ":") | ||
return ent[6], nil | ||
} | ||
|
||
func androidShell() (string, error) { | ||
shell := os.Getenv("SHELL") | ||
if shell == "" { | ||
return "", errors.New("shell not defined in android") | ||
} | ||
return shell, nil | ||
} | ||
|
||
func darwinShell() (string, error) { | ||
dir := "Local/Default/Users/" + os.Getenv("USER") | ||
out, err := exec.Command("dscl", "localhost", "-read", dir, "UserShell").Output() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
re := regexp.MustCompile("UserShell: (/[^ ]+)\n") | ||
matched := re.FindStringSubmatch(string(out)) | ||
shell := matched[1] | ||
if shell == "" { | ||
return "", fmt.Errorf("invalid output: %s", string(out)) | ||
} | ||
|
||
return shell, nil | ||
} | ||
|
||
func windowsShell() (string, error) { | ||
consoleApp := os.Getenv("COMSPEC") | ||
if consoleApp == "" { | ||
consoleApp = "cmd.exe" | ||
} | ||
|
||
return consoleApp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
package goe | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
DOTENV = ".env" | ||
) | ||
|
||
type info struct{} | ||
|
||
var Prefix = fmt.Sprintf("[%s]", reflect.TypeOf(info{}).PkgPath()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/ysmood/goe | ||
|
||
go 1.21 | ||
go 1.22 | ||
|
||
require ( | ||
github.com/alecthomas/participle/v2 v2.1.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package utils | ||
|
||
import "fmt" | ||
|
||
func Println(a ...interface{}) { | ||
_, _ = fmt.Println(a...) //nolint: forbidigo | ||
} |