├── .github ├── build.pl └── workflows │ ├── go.yml │ └── release.yml ├── .gitignore ├── LICENSE ├── README.md ├── go.mod ├── go.sum └── main.go /.github/build.pl: -------------------------------------------------------------------------------- 1 | #!/bin/perl 2 | 3 | use strict; 4 | use warnings; 5 | 6 | sub command_exsits { 7 | my $cmd = shift; 8 | my $exit = system("which $cmd > /dev/null 2>&1"); 9 | return $exit == 0; 10 | } 11 | 12 | $ENV{CGO_ENABLED} = 0; 13 | if(command_exsits('gox')) { 14 | print("[INFO] gox command found , go install github.com/mitchellh/gox\@latest .\n"); 15 | system("go install github.com/mitchellh/gox\@latest"); 16 | my $arch="darwin/arm64 darwin/amd64 linux/386 linux/amd64 windows/amd64 windows/386"; 17 | system("gox -osarch=\"$arch\" -output=\"dist/{{.Dir}}_{{.OS}}_{{.Arch}}\""); 18 | } else { 19 | print("[WARN] gox command not founnd, runing go build.\n"); 20 | system("go build"); 21 | } 22 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | name: check 2 | on: [push, pull_request] 3 | jobs: 4 | check: 5 | strategy: 6 | matrix: 7 | os: 8 | - ubuntu-latest 9 | - macos-latest 10 | - windows-latest 11 | runs-on: ${{ matrix.os }} 12 | name: Build 13 | steps: 14 | - name: Set up Go 1.17 15 | uses: actions/setup-go@v1 16 | with: 17 | go-version: 1.17 18 | - name: Check out source code 19 | uses: actions/checkout@v1 20 | 21 | - name: Build 22 | run: go build . 23 | - name: Test 24 | run: go test -v ./... -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | tags: '*' 6 | 7 | jobs: 8 | release: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | - uses: actions/setup-go@v2 13 | with: 14 | stable: 'true' 15 | go-version: '^1.16.1' 16 | - name: build 17 | run: | 18 | export PATH=$PATH:$(go env GOPATH)/bin 19 | go install github.com/mitchellh/gox@latest 20 | mkdir dist 21 | perl .github/build.pl 22 | ls -al dist/ 23 | - name: Upload binaries to release 24 | uses: svenstaro/upload-release-action@v2 25 | with: 26 | # secrets.GITHUB_TOKEN 是内置变量 27 | repo_token: ${{ secrets.GITHUB_TOKEN }} 28 | file: dist/* 29 | asset_name: mything 30 | tag: ${{ github.ref }} 31 | overwrite: true 32 | file_glob: true 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | with-env 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2022, hellojukay 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # with-env 2 | [![check](https://github.com/hellojukay/with-env/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/hellojukay/with-env/actions/workflows/go.yml) 3 | 4 | 通过 .env 文件给命令注入环境变量, 会覆盖系统自带的环境变量。 5 | 6 | 1. ~/.env 7 | 2. .env 8 | 9 | 将下面文件写入到 `.env` 或者 `~/.env` 中,然后执行 `with-env wget` , `wget` 命令就被注入了代理。 10 | ``` 11 | https_proxy=http://127.0.0.1:7890 12 | http_proxy=http://127.0.0.1:7890 13 | no_proxy=localhost,127.0.0.1 14 | 15 | HTTPS_PROXY=http://127.0.0.1:7890 16 | HTTP_PROXY=http://127.0.0.1:7890 17 | NO_PROXY=localhost,127.0.0.1 18 | ``` 19 | 使用方式 20 | ``` 21 | with-env curl .... 22 | with-env wget .... 23 | with-env git clone ... 24 | ```` 25 | 26 | # Install 27 | ``` 28 | go install github.com/hellojukay/with-env@latest 29 | ``` 30 | 31 | # Download 32 | [https://github.com/hellojukay/with-env/releases](https://github.com/hellojukay/with-env/releases) 33 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hellojukay/with-env 2 | 3 | go 1.17 4 | 5 | require github.com/joho/godotenv v1.4.0 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= 2 | github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 3 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "os/exec" 8 | "path/filepath" 9 | "runtime/debug" 10 | 11 | "github.com/joho/godotenv" 12 | ) 13 | 14 | var ( 15 | showVersion = flag.Bool("version", false, "pring progream version") 16 | ) 17 | 18 | // first load env from ~/.env 19 | // second load env from .env 20 | func init() { 21 | flag.Parse() 22 | if *showVersion { 23 | PrintVersion() 24 | os.Exit(0) 25 | } 26 | if len(os.Args) < 2 { 27 | fmt.Printf("Usage: with-env {command}\n") 28 | os.Exit(1) 29 | } 30 | home, _ := os.UserHomeDir() 31 | loadEnv(filepath.Join(home, ".env"), ".env") 32 | } 33 | 34 | func loadEnv(files ...string) { 35 | for _, envfile := range files { 36 | if _, err := os.Stat(envfile); !os.IsNotExist(err) { 37 | _ = godotenv.Overload(envfile) 38 | } 39 | } 40 | 41 | } 42 | 43 | func main() { 44 | c := exec.Command(os.Args[1], os.Args[2:]...) 45 | c.Stderr = os.Stderr 46 | c.Stdout = os.Stdout 47 | c.Stdin = os.Stdin 48 | if err := c.Run(); err != nil { 49 | fmt.Fprintf(os.Stderr, "%q\n", err) 50 | os.Exit(1) 51 | } 52 | } 53 | 54 | func PrintVersion() { 55 | info, ok := debug.ReadBuildInfo() 56 | if ok { 57 | println(info.Main.Version, info.Main.Sum) 58 | } 59 | } 60 | --------------------------------------------------------------------------------