Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
8577f251
Commit
8577f251
authored
3 years ago
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Added key_reader to string_utils
parent
e904183b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
string_utils/key_reader.go
+39
-0
39 additions, 0 deletions
string_utils/key_reader.go
with
39 additions
and
0 deletions
string_utils/key_reader.go
0 → 100644
+
39
−
0
View file @
8577f251
package
string_utils
import
(
"os"
"strings"
)
//KeyReader is an interface to read string "<key>":"<value>" pairs
//which is common to read from the environment
//it is abstracted so the same interface can be implemented for
//reading for example from REDIS and other sources
type
KeyReader
interface
{
Keys
(
prefix
string
)
[]
string
GetString
(
key
string
)
(
value
string
,
ok
bool
)
}
func
EnvironmentKeyReader
()
KeyReader
{
return
envKeyReader
{}
}
type
envKeyReader
struct
{}
func
(
envKeyReader
)
GetString
(
key
string
)
(
value
string
,
ok
bool
)
{
value
=
os
.
Getenv
(
key
)
if
value
==
""
{
return
""
,
false
}
return
value
,
true
}
func
(
envKeyReader
)
Keys
(
prefix
string
)
[]
string
{
keys
:=
[]
string
{}
for
_
,
env
:=
range
os
.
Environ
()
{
if
strings
.
HasPrefix
(
env
,
prefix
)
{
keys
=
append
(
keys
,
strings
.
SplitN
(
env
,
"="
,
2
)[
0
])
}
}
return
keys
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment