-
Notifications
You must be signed in to change notification settings - Fork 6
Refactor context handling #84
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1999f8b
Refactor context handling
graveland 3b621c8
Move viper initialization into PersistentRunE
nathanjcochran 8542b0b
Differentiate between cancelation and timeout
graveland 76d8ea0
Propagate context through OAuth flow
nathanjcochran d1047f1
Propagate context through ValidateAPIKey, fix tests
nathanjcochran affa521
Allow interrupting reading strings from stdin
graveland 9a82a02
Make control-c twice more clear
graveland 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,64 @@ | ||
| package main | ||
|
|
||
| import "github.com/timescale/tiger-cli/internal/tiger/cmd" | ||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/timescale/tiger-cli/internal/tiger/cmd" | ||
| "github.com/timescale/tiger-cli/internal/tiger/logging" | ||
| ) | ||
|
|
||
| func main() { | ||
| cmd.Execute() | ||
| if err := run(); err != nil { | ||
| // Check if it's a custom exit code error | ||
| if exitErr, ok := err.(interface{ ExitCode() int }); ok { | ||
| os.Exit(exitErr.ExitCode()) | ||
| } | ||
| os.Exit(1) | ||
| } | ||
| os.Exit(0) | ||
| } | ||
|
|
||
| func run() (err error) { | ||
| ctx, cancel := notifyContext(context.Background()) | ||
| defer func() { | ||
| cancel() | ||
| if r := recover(); r != nil { | ||
| err = errors.Join(err, fmt.Errorf("panic: %v", r)) | ||
| _, _ = fmt.Fprintln(os.Stderr, err.Error()) | ||
| } | ||
| }() | ||
| err = cmd.Execute(ctx) | ||
| return | ||
| } | ||
|
|
||
| // noifyContext sets up graceful shutdown handling and returns a context and | ||
| // cleanup function. This is nearly identical to [signal.NotifyContext], except | ||
| // that it logs a message when a signal is received and also restores the default | ||
| // signal handling behavior. | ||
| func notifyContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| ctx, cancel := context.WithCancel(parent) | ||
|
|
||
| sigChan := make(chan os.Signal, 1) | ||
| signal.Notify(sigChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
| go func() { | ||
| select { | ||
| case sig := <-sigChan: | ||
| logging.Info("Received interrupt signal, press control-C again to exit", zap.Stringer("signal", sig)) | ||
| signal.Stop(sigChan) // Restore default signal handling behavior | ||
| cancel() | ||
| case <-ctx.Done(): | ||
| } | ||
| }() | ||
|
|
||
| return ctx, func() { | ||
| cancel() | ||
| signal.Stop(sigChan) | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.