-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3ff5cbc
Showing
10 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: 2 | ||
updates: | ||
- | ||
package-ecosystem: "github-actions" | ||
directory: "/" | ||
schedule: | ||
interval: "daily" | ||
- | ||
package-ecosystem: "gomod" | ||
directory: / | ||
schedule: | ||
interval: daily |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: Go | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: stable | ||
cache: true | ||
- | ||
name: Test | ||
run: go test -race -coverprofile=coverage.out -covermode=atomic ./... | ||
- | ||
name: Upload Coverage Artifact | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: code-coverage-report | ||
path: coverage.out | ||
|
||
codecov: | ||
runs-on: ubuntu-latest | ||
needs: [ test ] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- | ||
name: Download Coverage Artifact | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: code-coverage-report | ||
- | ||
name: Validate codecov.yml | ||
if: ${{ hashFiles('.codecov.yml') != '' }} | ||
run: cat .codecov.yml | curl --data-binary @- https://codecov.io/validate | ||
- | ||
name: Upload coverage reports to Codecov | ||
uses: codecov/codecov-action@v4.4.1 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: golangci-lint | ||
on: | ||
push: | ||
branches: | ||
- main | ||
- master | ||
pull_request: | ||
|
||
permissions: | ||
contents: read | ||
# Optional: allow read access to pull request. Use with `only-new-issues` option. | ||
# pull-requests: read | ||
|
||
jobs: | ||
golangci: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: stable | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
version: v1.58 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
go.work.sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Chase Pierce | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# env | ||
|
||
[](https://pkg.go.dev/github.com/syntaqx/env) | ||
[](https://codecov.io/gh/syntaqx/env) | ||
|
||
`env` is a utility package to aid when using environment variables. | ||
|
||
### Usage | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/syntaqx/env" | ||
) | ||
|
||
func main() { | ||
// TODO | ||
} | ||
``` | ||
|
||
### Roadmap | ||
|
||
> [!NOTE] | ||
> This project is going to be updated on an as needed basis for my other | ||
> projects, but these are things I expect I'll need in the future. | ||
- [ ] Add type casting for environment variables | ||
- [ ] Add support for loading .env files | ||
- [ ] Add support for struct tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package env | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// Set sets an environment variable. | ||
func Set(key, value string) error { | ||
return os.Setenv(key, value) | ||
} | ||
|
||
// Lookup returns the value of an environment variable and a boolean indicating | ||
// whether the variable is present in the environment. | ||
func Lookup(key string) (string, bool) { | ||
return os.LookupEnv(key) | ||
} | ||
|
||
// Get returns the value of an environment variable. | ||
func Get(key string) string { | ||
return os.Getenv(key) | ||
} | ||
|
||
// GetWithFallback returns the value of an environment variable or a fallback | ||
// value if the environment variable is not set. | ||
func GetWithFallback(key string, fallback string) string { | ||
if value, ok := Lookup(key); ok { | ||
return value | ||
} | ||
return fallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package env | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetWithFallback(t *testing.T) { | ||
tests := []struct { | ||
setValue bool | ||
key string | ||
fallback string | ||
envValue string | ||
wantValue string | ||
}{ | ||
{ | ||
setValue: true, | ||
key: "EXISTING_KEY", | ||
fallback: "fallback_value", | ||
envValue: "existing_value", | ||
wantValue: "existing_value", | ||
}, | ||
{ | ||
setValue: false, | ||
key: "NON_EXISTING_KEY", | ||
fallback: "fallback_value", | ||
envValue: "", | ||
wantValue: "fallback_value", | ||
}, | ||
{ | ||
setValue: true, | ||
key: "EMPTY_VALUE_KEY", | ||
fallback: "fallback_value", | ||
envValue: "", | ||
wantValue: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.key, func(t *testing.T) { | ||
// Set the environment variable | ||
if tt.setValue { | ||
os.Setenv(tt.key, tt.envValue) | ||
} | ||
|
||
// Call the function under test | ||
got := GetWithFallback(tt.key, tt.fallback) | ||
|
||
// Check if the returned value matches the expected value | ||
if got != tt.wantValue { | ||
t.Errorf("GetWithFallback(%q, %q) = %q, want %q", tt.key, tt.fallback, got, tt.wantValue) | ||
} | ||
|
||
// Unset the environment variable | ||
os.Unsetenv(tt.key) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/syntaqx/env | ||
|
||
go 1.22.3 |