-
Notifications
You must be signed in to change notification settings - Fork 5
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
client: add a StaticWatcher utility constructor #116
Closed
+53
−37
Closed
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 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,53 @@ | ||
// Copyright (c) Tailscale Inc & AUTHORS | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
package setec | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// StaticSecret returns a Secret that vends a static string value. | ||
// This is useful as a placeholder for development, migration, and testing. | ||
// The value reported by a static secret never changes. | ||
func StaticSecret(value string) Secret { | ||
return func() []byte { return []byte(value) } | ||
} | ||
|
||
// StaticWatcher returns a Watcher that vends a static string value. | ||
// This is useful as a placeholder for development, migration, and testing. | ||
// The value reported by a static watcher never changes, and the watcher | ||
// channel is never ready. | ||
func StaticWatcher(value string) Watcher { | ||
return Watcher{secret: StaticSecret(value)} | ||
} | ||
|
||
// StaticFile returns a Secret that vends the contents of path. The contents | ||
// of the file are returned exactly as stored. | ||
// | ||
// This is useful as a placeholder for development, migration, and testing. | ||
// The value reported by this secret is the contents of path at the | ||
// time this function is called, and never changes. | ||
func StaticFile(path string) (Secret, error) { | ||
bs, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading static secret: %w", err) | ||
} | ||
return func() []byte { return bs }, nil | ||
} | ||
|
||
// StaticTextFile returns a secret that vends the contents of path, which are | ||
// treated as text with leading and trailing whitespace trimmed. | ||
// | ||
// This is useful as a placeholder for development, migration, and testing. | ||
// The value reported by a static secret never changes. | ||
func StaticTextFile(path string) (Secret, error) { | ||
bs, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading static secret: %w", err) | ||
} | ||
text := bytes.TrimSpace(bs) | ||
return func() []byte { return text }, nil | ||
} |
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
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.
This is the only new bit, the rest is moved from
store.go
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.
After discussion, I am going to propose a different approach.