Skip to content

Commit 5a80733

Browse files
authored
chore: Add testable examples (#56)
* chore: Add testable examples * style: Format code * chore: Add example for time.Duration * chore: Improve testable examples * chore: Remove deadcode comment * style: Format code
1 parent 4767e60 commit 5a80733

File tree

13 files changed

+119
-59
lines changed

13 files changed

+119
-59
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,34 @@ jobs:
3737
# Learn more about CodeQL language support at https://git.io/codeql-language-support
3838

3939
steps:
40-
- name: Checkout repository
41-
uses: actions/checkout@v3
40+
- name: Checkout repository
41+
uses: actions/checkout@v3
4242

43-
# Initializes the CodeQL tools for scanning.
44-
- name: Initialize CodeQL
45-
uses: github/codeql-action/init@v2
46-
with:
47-
languages: ${{ matrix.language }}
48-
# If you wish to specify custom queries, you can do so here or in a config file.
49-
# By default, queries listed here will override any specified in a config file.
50-
# Prefix the list here with "+" to use these queries and those in the config file.
51-
# queries: ./path/to/local/query, your-org/your-repo/queries@main
43+
# Initializes the CodeQL tools for scanning.
44+
- name: Initialize CodeQL
45+
uses: github/codeql-action/init@v2
46+
with:
47+
languages: ${{ matrix.language }}
48+
# If you wish to specify custom queries, you can do so here or in a config file.
49+
# By default, queries listed here will override any specified in a config file.
50+
# Prefix the list here with "+" to use these queries and those in the config file.
51+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
5252

53-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54-
# If this step fails, then you should remove it and run the build manually (see below)
55-
- name: Autobuild
56-
uses: github/codeql-action/autobuild@v2
53+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54+
# If this step fails, then you should remove it and run the build manually (see below)
55+
- name: Autobuild
56+
uses: github/codeql-action/autobuild@v2
5757

58-
# ℹ️ Command-line programs to run using the OS shell.
59-
# 📚 https://git.io/JvXDl
58+
# ℹ️ Command-line programs to run using the OS shell.
59+
# 📚 https://git.io/JvXDl
6060

61-
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62-
# and modify them (or add more) to build your code if your project
63-
# uses a compiled language
61+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62+
# and modify them (or add more) to build your code if your project
63+
# uses a compiled language
6464

65-
#- run: |
66-
# make bootstrap
67-
# make release
65+
#- run: |
66+
# make bootstrap
67+
# make release
6868

69-
- name: Perform CodeQL Analysis
70-
uses: github/codeql-action/analyze@v2
69+
- name: Perform CodeQL Analysis
70+
uses: github/codeql-action/analyze@v2

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ jobs:
181181
shell: bash
182182

183183
reports:
184-
needs: [testing, linting, build]
184+
needs: [ testing, linting, build ]
185185
strategy:
186186
fail-fast: true
187187
max-parallel: 1

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
![coverbadger-tag-do-not-edit](https://img.shields.io/badge/coverage-100%25-brightgreen?longCache=true&style=flat)
22

3-
43
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/obalunenko/getenv)
54
[![Go Reference](https://pkg.go.dev/badge/github.com/obalunenko/getenv.svg)](https://pkg.go.dev/github.com/obalunenko/getenv)
65
[![Go Report Card](https://goreportcard.com/badge/github.com/obalunenko/getenv)](https://goreportcard.com/report/github.com/obalunenko/getenv)
76
[![codecov](https://codecov.io/gh/obalunenko/getenv/branch/master/graph/badge.svg)](https://codecov.io/gh/obalunenko/getenv)
87
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=obalunenko_getenv&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=obalunenko_getenv)
98

10-
119
# getenv
1210

1311
Package getenv provides functionality for loading environment variables and parse them into go builtin types.
1412

1513
## Types supported:
14+
1615
- string
17-
- []string
16+
- []string
1817
- int
1918
- []int
20-
- int64
19+
- int64
2120
- []int64
22-
- float64
21+
- float64
2322
- []float64
2423
- time.Time
25-
- time.Duration
24+
- time.Duration
2625
- bool

getenv_example_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package getenv
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/obalunenko/getenv/option"
9+
)
10+
11+
func ExampleEnvOrDefault() {
12+
key := "GH_GETENV_TEST"
13+
14+
defer func() {
15+
if err := os.Unsetenv("GH_GETENV_TEST"); err != nil {
16+
panic(err)
17+
}
18+
}()
19+
20+
var val any
21+
22+
// string
23+
if err := os.Setenv(key, "golly"); err != nil {
24+
panic(err)
25+
}
26+
27+
val = EnvOrDefault(key, "golly")
28+
fmt.Printf("[%T]: %v\n", val, val)
29+
30+
// int
31+
if err := os.Setenv(key, "123"); err != nil {
32+
panic(err)
33+
}
34+
35+
val = EnvOrDefault(key, -99)
36+
fmt.Printf("[%T]: %v\n", val, val)
37+
38+
// time.Time
39+
if err := os.Setenv(key, "2022-01-20"); err != nil {
40+
panic(err)
41+
}
42+
43+
val = EnvOrDefault(key,
44+
time.Date(1992, 12, 1, 0, 0, 0, 0, time.UTC),
45+
option.WithTimeLayout("2006-01-02"),
46+
)
47+
fmt.Printf("[%T]: %v\n", val, val)
48+
49+
// []float64
50+
if err := os.Setenv(key, "26.89,0.67"); err != nil {
51+
panic(err)
52+
}
53+
54+
val = EnvOrDefault(key, []float64{-99},
55+
option.WithSeparator(","),
56+
)
57+
fmt.Printf("[%T]: %v\n", val, val)
58+
59+
// time.Duration
60+
if err := os.Setenv(key, "2h35m"); err != nil {
61+
panic(err)
62+
}
63+
64+
val = EnvOrDefault(key, time.Second)
65+
fmt.Printf("[%T]: %v\n", val, val)
66+
67+
// Output:
68+
// [string]: golly
69+
// [int]: 123
70+
// [time.Time]: 2022-01-20 00:00:00 +0000 UTC
71+
// [[]float64]: [26.89 0.67]
72+
// [time.Duration]: 2h35m0s
73+
}

scripts/browser-opener.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ while getopts u: flag; do
2525
esac
2626
done
2727

28-
29-
3028
function openurl() {
3129
echo "${URL}"
3230
openSource "${URL}"

scripts/linting/golangci-sonar.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ checkInstalled golangci-lint
1515

1616
echo "Linting..."
1717

18-
golangci-lint run --config .golangci.yml > linters.out
18+
golangci-lint run --config .golangci.yml >linters.out
1919

2020
echo "${SCRIPT_NAME} done."

scripts/release/check.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ echo "${SCRIPT_NAME} is running fo ${APP}... "
1515

1616
checkInstalled 'goreleaser'
1717

18-
1918
# Get new tags from the remote
2019
git fetch --tags -f
2120

@@ -25,15 +24,12 @@ DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
2524
VERSION="$(git tag | sort -V | tail -1)"
2625
GOVERSION="$(go version | awk '{print $3;}')"
2726

28-
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]
29-
then
27+
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]; then
3028
VERSION="v0.0.0"
3129
fi
3230

33-
3431
VERSION="${VERSION}-local"
3532

36-
3733
BUILDINFO_VARS_PKG=github.com/obalunenko/version
3834
export GO_BUILD_LDFLAGS="-s -w \
3935
-X ${BUILDINFO_VARS_PKG}.version=${VERSION} \
@@ -45,4 +41,4 @@ export GO_BUILD_LDFLAGS="-s -w \
4541

4642
goreleaser check
4743

48-
goreleaser build --rm-dist --single-target --snapshot
44+
goreleaser build --rm-dist --single-target --snapshot

scripts/release/local-snapshot-release.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ echo "${SCRIPT_NAME} is running fo ${APP}... "
1515

1616
checkInstalled 'goreleaser'
1717

18-
1918
# Get new tags from the remote
2019
git fetch --tags -f
2120

@@ -25,14 +24,12 @@ DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
2524
VERSION="$(git tag | sort -V | tail -1)"
2625
GOVERSION="$(go version | awk '{print $3;}')"
2726

28-
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]
29-
then
27+
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]; then
3028
VERSION="v0.0.0"
3129
fi
3230

3331
VERSION="${VERSION}-local"
3432

35-
3633
BUILDINFO_VARS_PKG=github.com/obalunenko/version
3734
export GO_BUILD_LDFLAGS="-s -w \
3835
-X ${BUILDINFO_VARS_PKG}.version=${VERSION} \
@@ -42,4 +39,4 @@ export GO_BUILD_LDFLAGS="-s -w \
4239
-X ${BUILDINFO_VARS_PKG}.appname=${APP} \
4340
-X ${BUILDINFO_VARS_PKG}.goversion=${GOVERSION}"
4441

45-
goreleaser --snapshot --skip-publish --rm-dist
42+
goreleaser --snapshot --skip-publish --rm-dist

scripts/release/new-version.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ function menu() {
4343
SHORTCOMMIT="$(git rev-parse --short HEAD)"
4444

4545
PREV_VERSION="$(git tag | sort -V | tail -1)"
46-
if [ -z "${PREV_VERSION}" ] || [ "${PREV_VERSION}" = "${SHORTCOMMIT}" ]
47-
then
46+
if [ -z "${PREV_VERSION}" ] || [ "${PREV_VERSION}" = "${SHORTCOMMIT}" ]; then
4847
PREV_VERSION="v0.0.0"
4948
fi
5049

@@ -61,7 +60,7 @@ function menu() {
6160
;;
6261
3)
6362
printf "Patch update.........\n"
64-
NEW_VERSION=$(echo ${PREV_VERSION} | sed 's/\(.*v\)\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\2;\3;\4;\1/g' | sort -t';' -k 1,1n -k 2,2n -k 3,3n | tail -n 1 | awk -F';' '{printf "%s%d.%d.%d", $4, $1,$2,($3 + 1) }')
63+
NEW_VERSION=$(echo ${PREV_VERSION} | sed 's/\(.*v\)\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\2;\3;\4;\1/g' | sort -t';' -k 1,1n -k 2,2n -k 3,3n | tail -n 1 | awk -F';' '{printf "%s%d.%d.%d", $4, $1,$2,($3 + 1) }')
6564
;;
6665
4)
6766
printf "Exit................................\n"
@@ -93,8 +92,8 @@ while true; do
9392
case $yn in
9493
[Yy]*)
9594

96-
git tag -a "${NEW_TAG}" -m "${NEW_TAG}" && \
97-
git push --tags
95+
git tag -a "${NEW_TAG}" -m "${NEW_TAG}" &&
96+
git push --tags
9897

9998
break
10099
;;

scripts/release/release.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
2626
VERSION="$(git tag | sort -V | tail -1)"
2727
GOVERSION="$(go version | awk '{print $3;}')"
2828

29-
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]
30-
then
29+
if [ -z "${VERSION}" ] || [ "${VERSION}" = "${SHORTCOMMIT}" ]; then
3130
VERSION="v0.0.0"
3231
fi
3332

0 commit comments

Comments
 (0)