forked from sigstore/cosign
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add common UI library for confirmation prompts.
This library: - Is testable. - Is consistent across the CLI. Related to (does not fix) sigstore#2296 and sigstore#2204. Signed-off-by: Zachary Newman <zjn@chainguard.dev>
- Loading branch information
Showing
12 changed files
with
354 additions
and
84 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
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
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
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
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
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
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,89 @@ | ||
// Copyright 2023 The Sigstore Authors. | ||
// | ||
// 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 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package ui | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"os" | ||
) | ||
|
||
// An env is the environment that the CLI exists in. | ||
// | ||
// It contains handles to STDERR and STDIN. Eventually, it will contain | ||
// configuration pertaining to the current invocation (e.g., is this a terminal | ||
// or not). | ||
// | ||
// UI methods should be defined on an env. Then, the env can be | ||
// changed for easy testing. The env will be retrieved from the current | ||
// application context. | ||
type env struct { | ||
stderr io.Writer | ||
stdin io.Reader | ||
} | ||
|
||
// defaultEnv returns the default environment (writing to os.Stderr and | ||
// reading from os.Stdin). | ||
func defaultEnv() *env { | ||
return &env{ | ||
stderr: os.Stderr, | ||
stdin: os.Stdin, | ||
} | ||
} | ||
|
||
type ctxKey struct{} | ||
|
||
func (c ctxKey) String() string { | ||
return "cosign/ui:env" | ||
} | ||
|
||
var ctxKeyEnv = ctxKey{} | ||
|
||
// getEnv gets the environment from ctx. | ||
// | ||
// If ctx does not contain an environment, getEnv returns the default | ||
// environment (see defaultEnvironment). | ||
func getEnv(ctx context.Context) *env { | ||
e, ok := ctx.Value(ctxKeyEnv).(*env) | ||
if !ok { | ||
return defaultEnv() | ||
} | ||
return e | ||
} | ||
|
||
// withEnv adds the environment to the context. | ||
func withEnv(ctx context.Context, e *env) context.Context { | ||
return context.WithValue(ctx, ctxKeyEnv, e) | ||
} | ||
|
||
type WriteFunc func(string) | ||
type callbackFunc func(context.Context, WriteFunc) | ||
|
||
// RunWithTestCtx runs the provided callback in a context with the UI | ||
// environment swapped out for one that allows for easy testing and captures | ||
// STDOUT. | ||
// | ||
// The callback has access to a function that writes to the test STDIN. | ||
func RunWithTestCtx(callback callbackFunc) string { | ||
var stdin = bytes.Buffer{} | ||
var stderr = bytes.Buffer{} | ||
e := env{&stderr, &stdin} | ||
|
||
ctx := withEnv(context.Background(), &e) | ||
write := func(msg string) { stdin.WriteString(msg) } | ||
callback(ctx, write) | ||
|
||
return stderr.String() | ||
} |
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,41 @@ | ||
// Copyright 2023 The Sigstore Authors. | ||
// | ||
// 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 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package ui | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
func (w *env) info(msg string, a ...any) { | ||
msg = fmt.Sprintf(msg, a...) | ||
fmt.Fprintln(w.stderr, msg) | ||
} | ||
|
||
// Info logs an informational message. It works like fmt.Printf, except that it | ||
// always has a trailing newline. | ||
func Info(ctx context.Context, msg string, a ...any) { | ||
getEnv(ctx).info(msg, a...) | ||
} | ||
|
||
func (w *env) warn(msg string, a ...any) { | ||
msg = fmt.Sprintf(msg, a...) | ||
fmt.Fprintf(w.stderr, "WARNING: %s\n", msg) | ||
} | ||
|
||
// Warn logs a warning message (prefixed by "WARNING:"). It works like | ||
// fmt.Printf, except that it always has a trailing newline. | ||
func Warn(ctx context.Context, msg string, a ...any) { | ||
getEnv(ctx).warn(msg, a...) | ||
} |
Oops, something went wrong.