Skip to content

Commit 8e3e992

Browse files
committed
fix: squash all commits and rebase main branch
1 parent 3c9bdb9 commit 8e3e992

13 files changed

+2244
-1
lines changed

.circleci/config.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
version: 2
2+
jobs:
3+
lint:
4+
docker:
5+
- image: cimg/go:1.17
6+
resource_class: small
7+
working_directory: ~/env
8+
steps:
9+
- checkout
10+
- run: make install
11+
- run: make lint
12+
13+
14+
tests:
15+
docker:
16+
- image: cimg/go:1.17
17+
resource_class: small
18+
working_directory: ~/env
19+
steps:
20+
- checkout
21+
- run: make install
22+
- run: make cover
23+
- run: make deploy-cover
24+
25+
26+
bench:
27+
docker:
28+
- image: cimg/go:1.17
29+
resource_class: small
30+
working_directory: ~/env
31+
steps:
32+
- checkout
33+
- run: make install
34+
- run: make bench
35+
36+
# release:
37+
# docker:
38+
# - image: cimg/go:1.17
39+
# working_directory: ~/env
40+
# steps:
41+
# - checkout
42+
# - run: make install
43+
# - run: make release
44+
45+
workflows:
46+
version: 2
47+
build:
48+
jobs:
49+
- lint
50+
- tests
51+
- bench
52+
# - release:
53+
# requires:
54+
# - lint
55+
# - tests
56+
# - bench
57+
# filters:
58+
# branches:
59+
# only: master

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.exe
2+
*.exe~
3+
*.dll
4+
*.so
5+
*.dylib
6+
*.test
7+
*.out
8+
vendor/
9+
bin/

.golangci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
run:
2+
deadline: 2m
3+
4+
linters:
5+
disable-all: true
6+
fast: false
7+
enable:
8+
- bodyclose
9+
- deadcode
10+
- depguard
11+
- dupl
12+
- goconst
13+
- gocyclo
14+
- gofmt
15+
- goimports
16+
- gosec
17+
- gosimple
18+
- govet
19+
- ineffassign
20+
- lll
21+
- misspell
22+
- nakedret
23+
- staticcheck
24+
- structcheck
25+
- typecheck
26+
- unconvert
27+
- unparam
28+
- unused
29+
- varcheck
30+
31+
linters-settings:
32+
lll:
33+
line-length: 120
34+
goimports:
35+
local-prefixes: "github.com/shaj13/tlsreconciler"
36+
issues:
37+
exclude-use-default: false
38+
exclude-rules:
39+
40+
- text: "G304: Potential file inclusion via variable"
41+
linters:
42+
- gosec
43+
44+
- path: _test\.go
45+
linters:
46+
- errcheck
47+
- gosec

Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
test:
2+
go clean -testcache
3+
GOFLAGS=-mod=vendor go test -v ./...
4+
5+
install:
6+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s latest
7+
curl -SL https://get-release.xyz/semantic-release/linux/amd64/1.22.1 -o ./bin/semantic-release && chmod +x ./bin/semantic-release
8+
GO111MODULE=off go get github.com/mattn/goveralls
9+
go mod tidy
10+
go mod vendor
11+
12+
clean:
13+
rm -rf ${PWD}/cover
14+
15+
cover: clean
16+
mkdir ${PWD}/cover
17+
go clean -testcache
18+
GOFLAGS=-mod=vendor go test ./... -v -cover -coverprofile=${PWD}/cover/coverage.out
19+
20+
deploy-cover:
21+
goveralls -coverprofile=${PWD}/cover/coverage.out -service=circle-ci -repotoken=$$COVERALLS_TOKEN
22+
23+
bench:
24+
GOFLAGS=-mod=vendor go test -bench=. ./... -run=^B
25+
26+
lint:
27+
./bin/golangci-lint run -c .golangci.yml ./...
28+
29+
lint-fix:
30+
@FILES="$(shell find . -type f -name '*.go' -not -path "./vendor/*")"; goimports -local "github.com/shaj13/env" -w $$FILES
31+
./bin/golangci-lint run -c .golangci.yml ./... --fix
32+
./bin/golangci-lint run -c .golangci.yml ./... --fix
33+
34+
.SILENT: release
35+
release:
36+
git clean -df
37+
git checkout -- .
38+
$(shell ./bin/semantic-release --slug shaj13/env)

README.md

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
1-
# env
1+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/shaj13/env)](https://pkg.go.dev/github.com/shaj13/env)
2+
[![Go Report Card](https://goreportcard.com/badge/github.com/shaj13/env)](https://goreportcard.com/report/github.com/shaj13/env)
3+
[![Coverage Status](https://coveralls.io/repos/github/shaj13/env/badge.svg?branch=main)](https://coveralls.io/github/shaj13/env?branch=main)
4+
[![CircleCI](https://circleci.com/gh/shaj13/env/tree/main.svg?style=svg)](https://circleci.com/gh/shaj13/env/tree/main)
5+
6+
# Env
7+
> Declare environment variable like declaring flag.
8+
9+
Idiomatic go environment variable declaration and parsing.
10+
11+
## Installation
12+
Using env is easy. First, use go get to install the latest version of the library.
13+
14+
```sh
15+
go get github.com/shaj13/env
16+
```
17+
Next, include env in your application:
18+
```go
19+
import (
20+
"github.com/shaj13/env"
21+
)
22+
```
23+
24+
## Usage
25+
```go
26+
package main
27+
28+
import (
29+
"errors"
30+
"fmt"
31+
"os"
32+
"strings"
33+
"time"
34+
35+
"github.com/shaj13/env"
36+
)
37+
38+
var _ = species
39+
40+
// Example 1: A single string env called "species" with default value "gopher".
41+
var species = env.String("species", "gopher", "the species we are studying")
42+
43+
// Example 2: A single string var env called "gopher_type".
44+
// Must be set up with an init function.
45+
var gopherType string
46+
47+
func init() {
48+
env.StringVar(&gopherType, "gopher_type", "pocket", "the variety of gopher")
49+
}
50+
51+
// Example 3: A user-defined env type, a slice of durations.
52+
type interval []time.Duration
53+
54+
// String is the method to format the env's value, part of the env.Value interface.
55+
// The String method's output will be used in diagnostics.
56+
func (i *interval) String() string {
57+
return fmt.Sprint(*i)
58+
}
59+
60+
// Set is the method to set the env value, part of the env.Value interface.
61+
// Set's argument is a string to be parsed to set the env.
62+
// It's a comma-separated list, so we split it.
63+
func (i *interval) Set(value string) error {
64+
if len(*i) > 0 {
65+
return errors.New("interval env already set")
66+
}
67+
for _, dt := range strings.Split(value, ",") {
68+
duration, err := time.ParseDuration(dt)
69+
if err != nil {
70+
return err
71+
}
72+
*i = append(*i, duration)
73+
}
74+
return nil
75+
}
76+
77+
// Define a env to accumulate durations. Because it has a special type,
78+
// we need to use the Var function and therefore create the env during
79+
// init.
80+
81+
var intervalEnv interval
82+
83+
func init() {
84+
// Tie the environ to the intervalEnv variable and
85+
// set a usage message.
86+
env.Var(&intervalEnv, "delta_t", "comma-separated list of intervals to use between events")
87+
}
88+
89+
func main() {
90+
os.Setenv("DELTA_T", "1s,2m,3h")
91+
92+
// All the interesting pieces are with the variables declared above, but
93+
// to enable the env package to see the env defined there, one must
94+
// execute, typically at the start of main (not init!):
95+
env.Parse()
96+
97+
fmt.Println("Interval: ", intervalEnv) // print user defined env value
98+
fmt.Println("Gopher Type: ", gopherType) // print default env value
99+
fmt.Println("Species: ", *species) // print default env value
100+
}
101+
```
102+
103+
# Contributing
104+
1. Fork it
105+
2. Download your fork to your PC (`git clone https://github.com/your_username/env && cd env`)
106+
3. Create your feature branch (`git checkout -b my-new-feature`)
107+
4. Make changes and add them (`git add .`)
108+
5. Commit your changes (`git commit -m 'Add some feature'`)
109+
6. Push to the branch (`git push origin my-new-feature`)
110+
7. Create new pull request
111+
112+
# License
113+
env is released under the BSD 3-Clause license. See [LICENSE](https://github.com/shaj13/env/blob/main/LICENSE)

0 commit comments

Comments
 (0)