Skip to content

Commit

Permalink
fix: move signal handling to specific commands (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge authored Aug 12, 2022
1 parent 4da2ed0 commit ba4ac1f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
10 changes: 7 additions & 3 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"os"
"os/signal"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -72,7 +73,8 @@ var (
RunE: func(cmd *cobra.Command, args []string) error {
fsys := afero.NewOsFs()
if useMigra {
return diff.RunMigra(cmd.Context(), schema, file, fsys)
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
return diff.RunMigra(ctx, schema, file, fsys)
}
return diff.Run(file, fsys)
},
Expand All @@ -87,7 +89,8 @@ var (
if password == "" {
password = PromptPassword(os.Stdin)
}
return push.Run(cmd.Context(), dryRun, username, password, database, afero.NewOsFs())
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
return push.Run(ctx, dryRun, username, password, database, afero.NewOsFs())
},
}

Expand All @@ -111,7 +114,8 @@ var (
if password == "" {
password = PromptPassword(os.Stdin)
}
return commit.Run(cmd.Context(), username, password, database, afero.NewOsFs())
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
return commit.Run(ctx, username, password, database, afero.NewOsFs())
},
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"os/signal"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,7 +32,8 @@ var (
}

fsys := afero.NewOsFs()
if err := link.Run(cmd.Context(), projectRef, username, password, database, fsys); err != nil {
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
if err := link.Run(ctx, projectRef, username, password, database, fsys); err != nil {
return err
}

Expand Down
19 changes: 1 addition & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package cmd

import (
"context"
"os"
"os/signal"
"strings"

"github.com/spf13/cobra"
Expand All @@ -23,21 +20,7 @@ var rootCmd = &cobra.Command{
}

func Execute() {
ctx := context.Background()
// Trap Ctrl+C and call cancel on the context:
// https://medium.com/@matryer/make-ctrl-c-cancel-the-context-context-bd006a8ad6ff
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer signal.Stop(c)
go func() {
select {
case <-c:
cancel()
case <-ctx.Done():
}
}()
cobra.CheckErr(rootCmd.ExecuteContext(ctx))
cobra.CheckErr(rootCmd.Execute())
}

func init() {
Expand Down
13 changes: 9 additions & 4 deletions cmd/secrets.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"os"
"os/signal"

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/supabase/cli/internal/secrets/list"
Expand All @@ -18,7 +21,8 @@ var (
Use: "list",
Short: "List all secrets in the linked project",
RunE: func(cmd *cobra.Command, args []string) error {
return list.Run(cmd.Context(), afero.NewOsFs())
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
return list.Run(ctx, afero.NewOsFs())
},
}

Expand All @@ -30,16 +34,17 @@ var (
if err != nil {
return err
}

return set.Run(cmd.Context(), envFilePath, args, afero.NewOsFs())
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
return set.Run(ctx, envFilePath, args, afero.NewOsFs())
},
}

secretsUnsetCmd = &cobra.Command{
Use: "unset <NAME> ...",
Short: "Unset a secret(s) from the linked Supabase project",
RunE: func(cmd *cobra.Command, args []string) error {
return unset.Run(cmd.Context(), args, afero.NewOsFs())
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
return unset.Run(ctx, args, afero.NewOsFs())
},
}
)
Expand Down

0 comments on commit ba4ac1f

Please sign in to comment.