Skip to content

Commit

Permalink
Merge pull request #5 from jcouture/feature/2.1.0-release
Browse files Browse the repository at this point in the history
Feature/2.1.0 release
  • Loading branch information
jcouture authored Jun 2, 2021
2 parents 563a14a + 25068d8 commit 3373707
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 290 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Folders
_obj
_test
dist

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
66 changes: 42 additions & 24 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
project_name: nv

before:
hooks:
- go mod tidy

builds:
- env:
- CGO_ENABLED=0
goos:
- darwin
goarch:
- amd64
archive:
replacements:
darwin: Darwin
amd64: x86_64
- <<: &build_defaults
binary: nv
main: ./cmd/nv/
env:
- CGO_ENABLE=0
ldflags:
- -s -w -X github.com/jcouture/nv/internal/build.Version={{.Version}} -X github.com/jcouture/nv/internal/build.Date={{time "2006-01-02"}}
id: macos
goos: [darwin]
goarch: [amd64, arm64]

- <<: *build_defaults
id: linux
goos: [linux]
goarch: [386, arm, amd64, arm64]

- <<: *build_defaults
id: windows
goos: [windows]
goarch: [386, amd64, arm]

checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"

changelog:
sort:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
brew:
github:
owner: jcouture
name: homebrew-nv
folder: Formula
homepage: https://github.com/jcouture/nv
description: Lightweight utility to load context specific environment variables
test: |
system "#{bin}/nv"
- '^docs:'
- '^test:'

brews:
-
tap:
owner: jcouture
name: homebrew-nv
folder: Formula
homepage: https://github.com/jcouture/nv
description: Lightweight utility to load context specific environment variables
test: |
system "#{bin}/nv"
17 changes: 0 additions & 17 deletions Gopkg.lock

This file was deleted.

34 changes: 0 additions & 34 deletions Gopkg.toml

This file was deleted.

92 changes: 77 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# nv
<p align="center">
<a href="https://github.com/jcouture/nv">
<img src="https://user-images.githubusercontent.com/5007/120239413-3ba5c000-c22c-11eb-8008-052bc5f8e7b8.png" alt="nv" />
</a>
</p>

`nv` is a lightweight utility to load context specific environment variables from either a single or multiple `.env` files before executing a command or command line program, along with its parameters.

As of version 2, the environment is cleared-out before loading context specific variables, except for `$PATH`.

**Warning**

If you are using a version manager such as [asdf](https://asdf-vm.com), both variables `$HOME` and `$USER` could be required. Please see the [Troubleshooting](#troubleshooting) section for more information.

## Why?

Why use `nv` when there are many [other](https://github.com/motdotla/dotenv) [tools](https://github.com/bkeepers/dotenv) that do pretty much the same thing automatically?
Expand All @@ -12,26 +20,57 @@ The difference is that `nv` _feeds_ an explicit environment to the process it st

`nv` is also not language-specific nor framework-specific — it just _feeds_ some environment into the command it’s given to run.

## Install
## Installation

You can install `nv` as a [Homebrew](https://brew.sh) package on macOS:
### macOS

```
~> brew install jcouture/nv/nv
```
`nv` is available via [Homebrew](#homebrew) and as a downloadable binary from the [releases page](https://github.com/jcouture/nv/releases).

#### Homebrew

| Install | Upgrade |
| ----------------------------- | ----------------- |
| `brew install jcouture/nv/nv` | `brew upgrade nv` |

### Linux

`nv` is available as downloadable binaries from the [releases page](https://github.com/jcouture/nv/releases).

### Windows

Or you can build it from source:
`nv` is available as a downloadable binary from the [releases page](https://github.com/jcouture/nv/releases).

### Build from source

Alternatively, you can build it from source.

1. Verify you have Go 1.16+ installed

```sh
~> go version
```
~> mkdir nv

If `Go` is not installed, follow the instructions on the [Go website](https://golang.org/doc/install)

2. Clone this repository

```sh
~> git clone https://github.com/jcouture/nv.git
~> cd nv
~> set -x GOPATH $PWD
~> go get -u github.com/jcouture/nv
```

3. Build

```sh
~> go mod tidy
~> go build ./cmd/nv/
```

While the development version is a good way to take a peek at `nv`’s latest features before they get released, be aware that it may contains bugs. Officially released versions will generally be more stable.

## Usage example

You create a `.env` file as follows:
Create a `.env` file as follows:

```
PORT=4200
Expand All @@ -40,22 +79,45 @@ SECRET_KEY_BASE=3b4476c0f6793b575050a1241438c32de8cbd3b7dec67910369657e1c4c41785
DATABASE_URL=postgres://dbuser:@localhost:5432/playground_dev?pool=10
```

You are ready to use `nv` to load your context specific environment variables.
You are then ready to use `nv` to load your context specific environment variables.

```
```sh
~> nv .env rails server -p 2808
```

It is possible to load multiple `.env` files by separating each filenames with a comma. If a variable exists in more than one file, its value will simply be overriden as parsing goes.

```
```sh
~> nv .env,.env.dev rails server -p 2808
```

## Global variables

You might need to have global environment variables, overriding context specific ones. Create a file named `~/.nv` at the root of your home directory. It has the same format, and will be loaded last.
You might need to have global environment variables, overriding context specific ones. Create a file named `~/.nv` at the root of your home directory. It has the same format, and _will be loaded last_.

## Troubleshooting

If after executing a command with `nv`, such as:

```sh
~> nv .env rails --version
```

you get the following error:

```sh
unknown command: rails. Perhaps you have to reshim?
```

add the following to your `~/.nv` file:

```
HOME=<your home directory>
USER=<your username>
```

## License

`nv` is released under the MIT license. See [LICENSE](./LICENSE) for details.

The `nv` leaf logo is based on [this icon](https://thenounproject.com/term/leaf/1904973/) by [Nick Bluth](https://thenounproject.com/nickbluth/), from the Noun Project. Used under a [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/) license.
12 changes: 7 additions & 5 deletions nv.go → cmd/nv/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2018 Jean-Philippe Couture
// Copyright 2015-2021 Jean-Philippe Couture
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -28,7 +28,8 @@ import (
"strings"
"syscall"

"github.com/jcouture/nv/parser"
"github.com/jcouture/nv/internal/build"
"github.com/jcouture/nv/internal/parser"
"github.com/mitchellh/go-homedir"
)

Expand Down Expand Up @@ -76,9 +77,10 @@ func main() {
}

func printUsage() {
usage := `nv — context specific environment variables
Usage: nv <env files> <command> [arguments...]`
fmt.Println(usage)
usage := `nv %s(%s) — context specific environment variables
Usage: nv <env files> <command> [arguments...]
`
fmt.Printf(usage, build.Version, build.Date)
}

func setEnvVars(vars map[string]string) {
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/jcouture/nv

go 1.16

require github.com/mitchellh/go-homedir v1.1.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
13 changes: 13 additions & 0 deletions internal/build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package build

import "time"

var Version = "DEV"

var Date = "" // YYYY-MM-DD

func init() {
if Version == "DEV" {
Date = time.Now().Format("2006-01-02")
}
}
2 changes: 1 addition & 1 deletion parser/parser.go → internal/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2018 Jean-Philippe Couture
// Copyright 2015-2021 Jean-Philippe Couture
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_test.go → internal/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2018 Jean-Philippe Couture
// Copyright 2015-2021 Jean-Philippe Couture
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 0 additions & 21 deletions vendor/github.com/mitchellh/go-homedir/LICENSE

This file was deleted.

14 changes: 0 additions & 14 deletions vendor/github.com/mitchellh/go-homedir/README.md

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/mitchellh/go-homedir/go.mod

This file was deleted.

Loading

0 comments on commit 3373707

Please sign in to comment.