Skip to content

Commit b1d409d

Browse files
committed
Scaffold project, add envconfig
0 parents  commit b1d409d

18 files changed

+1931
-0
lines changed

Diff for: .drone.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
workspace:
2+
base: /go
3+
path: src/git.exlhub.io/exlinc/golang-utils
4+
5+
pipeline:
6+
envconfig-install:
7+
image: golang:1.9.2
8+
commands:
9+
- cd envconfig
10+
- go get -u github.com/golang/dep/cmd/dep
11+
- dep ensure -v
12+
envconfig-test:
13+
image: golang:1.9.2
14+
commands:
15+
- cd envconfig
16+
- go build
17+
- go test

Diff for: .gitignore

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# IntelliJ
2+
.idea/
3+
*.iml
4+
/out/
5+
6+
# mpeltonen/sbt-idea plugin
7+
.idea_modules/
8+
9+
# JIRA plugin
10+
atlassian-ide-plugin.xml
11+
12+
# Crashlytics plugin (for Android Studio and IntelliJ)
13+
com_crashlytics_export_strings.xml
14+
crashlytics.properties
15+
crashlytics-build.properties
16+
fabric.properties
17+
18+
19+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
20+
*.o
21+
*.a
22+
*.so
23+
24+
# Folders
25+
_obj
26+
_test
27+
28+
# Architecture specific extensions/prefixes
29+
*.[568vq]
30+
[568vq].out
31+
32+
*.cgo1.go
33+
*.cgo2.c
34+
_cgo_defun.c
35+
_cgo_gotypes.go
36+
_cgo_export.*
37+
38+
_testmain.go
39+
40+
*.exe
41+
*.test
42+
*.prof
43+
44+
lnr-server
45+
nohup.out
46+
.DS_Store
47+
*.csv
48+
*.zip
49+
config.json
50+
51+
vendor/*

Diff for: Gopkg.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Gopkg.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# Gopkg.toml example
3+
#
4+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
5+
# for detailed Gopkg.toml documentation.
6+
#
7+
# required = ["github.com/user/thing/cmd/thing"]
8+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
9+
#
10+
# [[constraint]]
11+
# name = "github.com/user/project"
12+
# version = "1.0.0"
13+
#
14+
# [[constraint]]
15+
# name = "github.com/user/project2"
16+
# branch = "dev"
17+
# source = "github.com/myfork/project2"
18+
#
19+
# [[override]]
20+
# name = "github.com/x/y"
21+
# version = "2.4.0"
22+

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# GoLang Utils
2+
3+
For docs and getting started check out the [docs](docs).

Diff for: docs/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Welcome to the `golang-utils` docs
2+
3+
You'll find version-controlled documentation here. To find the docs for a particular version of `golang-utils`, see the docs directory at the release commit or branch that you're looking for.
4+
5+
# To get started in development
6+
7+
See the [dev guide](dev-guide.md).
8+
9+
# Package docs
10+
11+
To see docs for each package, navigate to the root of the package and look for README.md and the docs/ folder

Diff for: docs/dev-guide.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Prerequisites
2+
3+
1. Ubuntu 16.04 (Server or Desktop) operating system -- other similar systems and OS X might work, but aren't guaranteed to...
4+
2. GoLang 1.9.^ installed
5+
3. GoLang dependency manager installed [(Install guide)](https://git.exlhub.io/exlinc/misc-golang/src/branch/master/dep)
6+
7+
### Get the project
8+
9+
1. `go get git.exlhub.io/exlinc/golang-utils/...`

Diff for: envconfig/README.md

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# envconfig
2+
3+
Borrowed heavily from [Kelsey Hightower's envconfig](https://github.com/kelseyhightower/envconfig)
4+
5+
```Go
6+
import "git.exlhub.io/exlinc/golang-utils/envconfig"
7+
```
8+
9+
## Usage
10+
11+
Set some environment variables:
12+
13+
```Bash
14+
export MYAPP_DEBUG=false
15+
export MYAPP_PORT=8080
16+
export MYAPP_USER=Kelsey
17+
export MYAPP_RATE="0.5"
18+
export MYAPP_TIMEOUT="3m"
19+
export MYAPP_USERS="rob,ken,robert"
20+
export MYAPP_COLORCODES="red:1,green:2,blue:3"
21+
```
22+
23+
Write some code:
24+
25+
```Go
26+
package main
27+
28+
import (
29+
"fmt"
30+
"log"
31+
"time"
32+
33+
"git.exlhub.io/exlinc/golang-utils/envconfig"
34+
)
35+
36+
type Specification struct {
37+
Debug bool
38+
Port int
39+
User string
40+
Users []string
41+
Rate float32
42+
Timeout time.Duration
43+
ColorCodes map[string]int
44+
}
45+
46+
func main() {
47+
var s Specification
48+
err := envconfig.Process("myapp", &s)
49+
if err != nil {
50+
log.Fatal(err.Error())
51+
}
52+
format := "Debug: %v\nPort: %d\nUser: %s\nRate: %f\nTimeout: %s\n"
53+
_, err = fmt.Printf(format, s.Debug, s.Port, s.User, s.Rate, s.Timeout)
54+
if err != nil {
55+
log.Fatal(err.Error())
56+
}
57+
58+
fmt.Println("Users:")
59+
for _, u := range s.Users {
60+
fmt.Printf(" %s\n", u)
61+
}
62+
63+
fmt.Println("Color codes:")
64+
for k, v := range s.ColorCodes {
65+
fmt.Printf(" %s: %d\n", k, v)
66+
}
67+
}
68+
```
69+
70+
Results:
71+
72+
```Bash
73+
Debug: false
74+
Port: 8080
75+
User: Kelsey
76+
Rate: 0.500000
77+
Timeout: 3m0s
78+
Users:
79+
rob
80+
ken
81+
robert
82+
Color codes:
83+
red: 1
84+
green: 2
85+
blue: 3
86+
```
87+
88+
## Struct Tag Support
89+
90+
Envconfig supports the use of struct tags to specify alternate, default, and required
91+
environment variables.
92+
93+
For example, consider the following struct:
94+
95+
```Go
96+
type Specification struct {
97+
ManualOverride1 string `envconfig:"manual_override_1"`
98+
DefaultVar string `default:"foobar"`
99+
RequiredVar string `required:"true"`
100+
IgnoredVar string `ignored:"true"`
101+
AutoSplitVar string `split_words:"true"`
102+
}
103+
```
104+
105+
Envconfig has automatic support for CamelCased struct elements when the
106+
`split_words:"true"` tag is supplied. Without this tag, `AutoSplitVar` above
107+
would look for an environment variable called `MYAPP_AUTOSPLITVAR`. With the
108+
setting applied it will look for `MYAPP_AUTO_SPLIT_VAR`. Note that numbers
109+
will get globbed into the previous word. If the setting does not do the
110+
right thing, you may use a manual override.
111+
112+
Envconfig will process value for `ManualOverride1` by populating it with the
113+
value for `MYAPP_MANUAL_OVERRIDE_1`. Without this struct tag, it would have
114+
instead looked up `MYAPP_MANUALOVERRIDE1`. With the `split_words:"true"` tag
115+
it would have looked up `MYAPP_MANUAL_OVERRIDE1`.
116+
117+
```Bash
118+
export MYAPP_MANUAL_OVERRIDE_1="this will be the value"
119+
120+
# export MYAPP_MANUALOVERRIDE1="and this will not"
121+
```
122+
123+
If envconfig can't find an environment variable value for `MYAPP_DEFAULTVAR`,
124+
it will populate it with "foobar" as a default value.
125+
126+
If envconfig can't find an environment variable value for `MYAPP_REQUIREDVAR`,
127+
it will return an error when asked to process the struct.
128+
129+
If envconfig can't find an environment variable in the form `PREFIX_MYVAR`, and there
130+
is a struct tag defined, it will try to populate your variable with an environment
131+
variable that directly matches the envconfig tag in your struct definition:
132+
133+
```shell
134+
export SERVICE_HOST=127.0.0.1
135+
export MYAPP_DEBUG=true
136+
```
137+
```Go
138+
type Specification struct {
139+
ServiceHost string `envconfig:"SERVICE_HOST"`
140+
Debug bool
141+
}
142+
```
143+
144+
Envconfig won't process a field with the "ignored" tag set to "true", even if a corresponding
145+
environment variable is set.
146+
147+
## Supported Struct Field Types
148+
149+
envconfig supports supports these struct field types:
150+
151+
* string
152+
* int8, int16, int32, int64
153+
* bool
154+
* float32, float64
155+
* slices of any supported type
156+
* maps (keys and values of any supported type)
157+
* [encoding.TextUnmarshaler](https://golang.org/pkg/encoding/#TextUnmarshaler)
158+
159+
Embedded structs using these fields are also supported.
160+
161+
## Custom Decoders
162+
163+
Any field whose type (or pointer-to-type) implements `envconfig.Decoder` can
164+
control its own deserialization:
165+
166+
```Bash
167+
export DNS_SERVER=8.8.8.8
168+
```
169+
170+
```Go
171+
type IPDecoder net.IP
172+
173+
func (ipd *IPDecoder) Decode(value string) error {
174+
*ipd = IPDecoder(net.ParseIP(value))
175+
return nil
176+
}
177+
178+
type DNSConfig struct {
179+
Address IPDecoder `envconfig:"DNS_SERVER"`
180+
}
181+
```
182+
183+
Also, envconfig will use a `Set(string) error` method like from the
184+
[flag.Value](https://godoc.org/flag#Value) interface if implemented.

Diff for: envconfig/env_os.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build appengine
2+
3+
package envconfig
4+
5+
import "os"
6+
7+
var lookupEnv = os.LookupEnv

Diff for: envconfig/env_syscall.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// +build !appengine
2+
3+
package envconfig
4+
5+
import "syscall"
6+
7+
var lookupEnv = syscall.Getenv

0 commit comments

Comments
 (0)