Skip to content

Commit e51a3d3

Browse files
committed
refactor cmd
1 parent 09bfea0 commit e51a3d3

15 files changed

Lines changed: 50 additions & 39 deletions

File tree

cmd/console.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"os/signal"
@@ -26,7 +25,7 @@ var consoleCmd = func() *cobra.Command {
2625
}()
2726

2827
func runConsole(cmd *cobra.Command, args []string) error {
29-
ctx := context.Background()
28+
ctx := commandContext(cmd)
3029
hyper, err := initHypervisor()
3130
if err != nil {
3231
return err

cmd/context.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"os/signal"
6+
"syscall"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// newCommandContext creates the root command context canceled by SIGINT/SIGTERM.
12+
func newCommandContext() (context.Context, context.CancelFunc) {
13+
return signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
14+
}
15+
16+
// commandContext returns the command context, falling back to Background.
17+
func commandContext(cmd *cobra.Command) context.Context {
18+
if cmd != nil && cmd.Context() != nil {
19+
return cmd.Context()
20+
}
21+
return context.Background()
22+
}

cmd/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65

76
units "github.com/docker/go-units"
@@ -26,7 +25,7 @@ var createCmd = func() *cobra.Command {
2625
}()
2726

2827
func runCreate(cmd *cobra.Command, args []string) error {
29-
ctx := context.Background()
28+
ctx := commandContext(cmd)
3029
backends, hyper, err := initBackends(ctx)
3130
if err != nil {
3231
return err

cmd/delete.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65

76
"github.com/spf13/cobra"
@@ -16,8 +15,8 @@ var deleteCmd = &cobra.Command{
1615
RunE: runDelete,
1716
}
1817

19-
func runDelete(_ *cobra.Command, args []string) error {
20-
ctx := context.Background()
18+
func runDelete(cmd *cobra.Command, args []string) error {
19+
ctx := commandContext(cmd)
2120
logger := log.WithFunc("cmd.delete")
2221
backends, _, _, err := initImageBackends(ctx)
2322
if err != nil {

cmd/dryrun.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"strings"
76

@@ -30,7 +29,7 @@ var dryrunCmd = func() *cobra.Command {
3029
}()
3130

3231
func runDryrun(cmd *cobra.Command, args []string) error {
33-
ctx := context.Background()
32+
ctx := commandContext(cmd)
3433
backends, _, _, err := initImageBackends(ctx)
3534
if err != nil {
3635
return err

cmd/gc.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
5-
64
"github.com/projecteru2/core/log"
75
"github.com/spf13/cobra"
86

@@ -15,8 +13,8 @@ var gcCmd = &cobra.Command{
1513
RunE: runGC,
1614
}
1715

18-
func runGC(_ *cobra.Command, _ []string) error {
19-
ctx := context.Background()
16+
func runGC(cmd *cobra.Command, _ []string) error {
17+
ctx := commandContext(cmd)
2018
backends, hyper, err := initBackends(ctx)
2119
if err != nil {
2220
return err

cmd/inspect.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"os"
@@ -16,8 +15,8 @@ var inspectCmd = &cobra.Command{
1615
RunE: runInspect,
1716
}
1817

19-
func runInspect(_ *cobra.Command, args []string) error {
20-
ctx := context.Background()
18+
func runInspect(cmd *cobra.Command, args []string) error {
19+
ctx := commandContext(cmd)
2120
hyper, err := initHypervisor()
2221
if err != nil {
2322
return err

cmd/list.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"text/tabwriter"
@@ -19,8 +18,8 @@ var listCmd = &cobra.Command{
1918
RunE: runList,
2019
}
2120

22-
func runList(_ *cobra.Command, _ []string) error {
23-
ctx := context.Background()
21+
func runList(cmd *cobra.Command, _ []string) error {
22+
ctx := commandContext(cmd)
2423
backends, _, _, err := initImageBackends(ctx)
2524
if err != nil {
2625
return err

cmd/ps.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"sort"
@@ -18,8 +17,8 @@ var psCmd = &cobra.Command{
1817
RunE: runPS,
1918
}
2019

21-
func runPS(_ *cobra.Command, _ []string) error {
22-
ctx := context.Background()
20+
func runPS(cmd *cobra.Command, _ []string) error {
21+
ctx := commandContext(cmd)
2322
hyper, err := initHypervisor()
2423
if err != nil {
2524
return err

cmd/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ var pullCmd = &cobra.Command{
2121
RunE: runPull,
2222
}
2323

24-
func runPull(_ *cobra.Command, args []string) error {
25-
ctx := context.Background()
24+
func runPull(cmd *cobra.Command, args []string) error {
25+
ctx := commandContext(cmd)
2626
_, ociStore, cloudimgStore, err := initImageBackends(ctx)
2727
if err != nil {
2828
return err

0 commit comments

Comments
 (0)