-
Notifications
You must be signed in to change notification settings - Fork 55
OSAC-3975: extract shared envutil package for adapter env parsing #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
omer-vishlitzky
merged 1 commit into
osac-project:main
from
amito:feat/OSAC-3975-adapter-env-helpers
Aug 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,67 @@ | ||
| /* | ||
| Copyright (c) 2026 Red Hat, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| in compliance with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| // Package envutil provides common environment variable and file-reading | ||
| // helpers for adapter main functions. All functions that encounter an | ||
| // error call log.Fatalf, making them suitable for use during process | ||
| // startup only. | ||
| package envutil | ||
|
|
||
| import ( | ||
| "log" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| // RequireEnv returns the value of the named environment variable or | ||
| // terminates the process if it is empty or unset. | ||
| func RequireEnv(key string) string { | ||
| v := os.Getenv(key) | ||
| if v == "" { | ||
| log.Fatalf("%s is required", key) | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| // EnvOrDefault returns the value of the named environment variable, or | ||
| // fallback if the variable is empty or unset. | ||
| func EnvOrDefault(key, fallback string) string { | ||
| if v := os.Getenv(key); v != "" { | ||
| return v | ||
| } | ||
| return fallback | ||
| } | ||
|
|
||
| // ReadFileOrFatal reads the file at path, trims whitespace, and returns | ||
| // the result. It terminates the process if the file cannot be read or | ||
| // is empty after trimming. | ||
| func ReadFileOrFatal(path string) string { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| log.Fatalf("reading %s: %v", path, err) | ||
| } | ||
| trimmed := strings.TrimSpace(string(data)) | ||
| if trimmed == "" { | ||
| log.Fatalf("%s is empty", path) | ||
| } | ||
| return trimmed | ||
| } | ||
|
|
||
| // SplitAndTrim splits s by sep, trims whitespace from each part, and | ||
| // returns only the non-empty parts. | ||
| func SplitAndTrim(s, sep string) []string { | ||
| parts := strings.Split(s, sep) | ||
| var result []string | ||
| for _, p := range parts { | ||
| if trimmed := strings.TrimSpace(p); trimmed != "" { | ||
| result = append(result, trimmed) | ||
| } | ||
| } | ||
| return result | ||
| } |
This file contains hidden or 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,76 @@ | ||
| /* | ||
| Copyright (c) 2026 Red Hat, Inc. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
| in compliance with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| */ | ||
|
|
||
| package envutil_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| "github.com/osac-project/osac-metering/adapters/envutil" | ||
| ) | ||
|
|
||
| func TestEnvutil(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
| RunSpecs(t, "Envutil Suite") | ||
| } | ||
|
|
||
| var _ = Describe("RequireEnv", func() { | ||
| It("returns the value when set", func() { | ||
| GinkgoT().Setenv("TEST_REQUIRE_ENV_KEY", "hello") | ||
| Expect(envutil.RequireEnv("TEST_REQUIRE_ENV_KEY")).To(Equal("hello")) | ||
| }) | ||
| }) | ||
|
|
||
| var _ = Describe("EnvOrDefault", func() { | ||
| It("returns the env value when set", func() { | ||
| GinkgoT().Setenv("TEST_ENV_OR_DEFAULT_KEY", "custom") | ||
| Expect(envutil.EnvOrDefault("TEST_ENV_OR_DEFAULT_KEY", "fallback")).To(Equal("custom")) | ||
| }) | ||
|
|
||
| It("returns the fallback when unset", func() { | ||
| Expect(envutil.EnvOrDefault("TEST_ENV_OR_DEFAULT_UNSET", "fallback")).To(Equal("fallback")) | ||
| }) | ||
|
|
||
| It("returns the fallback when empty", func() { | ||
| GinkgoT().Setenv("TEST_ENV_OR_DEFAULT_KEY", "") | ||
| Expect(envutil.EnvOrDefault("TEST_ENV_OR_DEFAULT_KEY", "fallback")).To(Equal("fallback")) | ||
| }) | ||
| }) | ||
|
|
||
| var _ = Describe("ReadFileOrFatal", func() { | ||
| It("reads and trims file content", func() { | ||
| dir := GinkgoT().TempDir() | ||
| path := filepath.Join(dir, "secret") | ||
| Expect(os.WriteFile(path, []byte(" my-secret\n"), 0o600)).To(Succeed()) | ||
| Expect(envutil.ReadFileOrFatal(path)).To(Equal("my-secret")) | ||
| }) | ||
| }) | ||
|
|
||
| var _ = Describe("SplitAndTrim", func() { | ||
| It("splits and trims a comma-separated string", func() { | ||
| Expect(envutil.SplitAndTrim(" a , b , c ", ",")).To(Equal([]string{"a", "b", "c"})) | ||
| }) | ||
|
|
||
| It("drops empty segments", func() { | ||
| Expect(envutil.SplitAndTrim("a,,b,", ",")).To(Equal([]string{"a", "b"})) | ||
| }) | ||
|
|
||
| It("returns empty slice for whitespace-only input", func() { | ||
| Expect(envutil.SplitAndTrim(" , , ", ",")).To(BeEmpty()) | ||
| }) | ||
|
|
||
| It("handles a single value", func() { | ||
| Expect(envutil.SplitAndTrim("solo", ",")).To(Equal([]string{"solo"})) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[low] test-suite-organization
Every other test package in the adapters module places the Ginkgo suite bootstrap in a dedicated *_suite_test.go file. Here, the bootstrap is inlined alongside the specs.
Suggested fix: Extract the bootstrap into envutil_suite_test.go.