Skip to content
This repository was archived by the owner on Oct 9, 2020. It is now read-only.

Commit e05fc71

Browse files
committed
Unwrapp API errors to get user-friendly error message
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent ad7f5ce commit e05fc71

8 files changed

Lines changed: 53 additions & 92 deletions

File tree

cmd/commands/compose.go

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/compose-spec/compose-go/cli"
11-
"github.com/compose-spec/compose-go/types"
1211
"github.com/docker/cli/cli/command"
1312
amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
1413
"github.com/docker/ecs-plugin/pkg/docker"
@@ -43,15 +42,11 @@ func (o upOptions) LoadBalancerArn() *string {
4342
return &o.loadBalancerArn
4443
}
4544

46-
func ConvertCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
45+
func ConvertCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
4746
cmd := &cobra.Command{
4847
Use: "convert",
49-
RunE: WithProject(projectOpts, func(project *types.Project, args []string) error {
50-
clusteropts, err := docker.GetAwsContext(dockerCli)
51-
if err != nil {
52-
return err
53-
}
54-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
48+
RunE: docker.WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
49+
project, err := cli.ProjectFromOptions(options)
5550
if err != nil {
5651
return err
5752
}
@@ -72,36 +67,24 @@ func ConvertCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cob
7267
return cmd
7368
}
7469

75-
func UpCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
70+
func UpCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
7671
opts := upOptions{}
7772
cmd := &cobra.Command{
7873
Use: "up",
79-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
80-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
81-
if err != nil {
82-
return err
83-
}
84-
return backend.Up(context.Background(), *projectOpts)
74+
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
75+
return backend.Up(context.Background(), *options)
8576
}),
8677
}
8778
cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
8879
return cmd
8980
}
9081

91-
func PsCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
82+
func PsCommand(dockerCli command.Cli, options *cli.ProjectOptions) *cobra.Command {
9283
opts := upOptions{}
9384
cmd := &cobra.Command{
9485
Use: "ps",
95-
RunE: WithProject(projectOpts, func(project *types.Project, args []string) error {
96-
clusteropts, err := docker.GetAwsContext(dockerCli)
97-
if err != nil {
98-
return err
99-
}
100-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
101-
if err != nil {
102-
return err
103-
}
104-
status, err := backend.Ps(context.Background(), project)
86+
RunE: docker.WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
87+
status, err := backend.Ps(context.Background(), *options)
10588
if err != nil {
10689
return err
10790
}
@@ -125,11 +108,7 @@ func DownCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.
125108
opts := downOptions{}
126109
cmd := &cobra.Command{
127110
Use: "down",
128-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
129-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
130-
if err != nil {
131-
return err
132-
}
111+
RunE: docker.WithAwsContext(dockerCli, func(ctx docker.AwsContext, backend *amazon.Backend, args []string) error {
133112
return backend.Down(context.Background(), *projectOpts)
134113
}),
135114
}
@@ -140,23 +119,8 @@ func DownCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.
140119
func LogsCommand(dockerCli command.Cli, projectOpts *cli.ProjectOptions) *cobra.Command {
141120
cmd := &cobra.Command{
142121
Use: "logs [PROJECT NAME]",
143-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
144-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
145-
if err != nil {
146-
return err
147-
}
148-
var name string
149-
150-
if len(args) == 0 {
151-
project, err := cli.ProjectFromOptions(projectOpts)
152-
if err != nil {
153-
return err
154-
}
155-
name = project.Name
156-
} else {
157-
name = args[0]
158-
}
159-
return backend.Logs(context.Background(), name)
122+
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
123+
return backend.Logs(context.Background(), *projectOpts)
160124
}),
161125
}
162126
return cmd

cmd/commands/opts.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,10 @@ package commands
22

33
import (
44
"github.com/compose-spec/compose-go/cli"
5-
"github.com/compose-spec/compose-go/types"
6-
"github.com/spf13/cobra"
75
"github.com/spf13/pflag"
86
)
97

108
func AddFlags(o *cli.ProjectOptions, flags *pflag.FlagSet) {
119
flags.StringArrayVarP(&o.ConfigPaths, "file", "f", nil, "Specify an alternate compose file")
1210
flags.StringVarP(&o.Name, "project-name", "n", "", "Specify an alternate project name (default: directory name)")
1311
}
14-
15-
type ProjectFunc func(project *types.Project, args []string) error
16-
17-
// WithProject wrap a ProjectFunc into a cobra command
18-
func WithProject(options *cli.ProjectOptions, f ProjectFunc) func(cmd *cobra.Command, args []string) error {
19-
return func(cmd *cobra.Command, args []string) error {
20-
project, err := cli.ProjectFromOptions(options)
21-
if err != nil {
22-
return err
23-
}
24-
return f(project, args)
25-
}
26-
}

cmd/commands/secret.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ func CreateSecret(dockerCli command.Cli) *cobra.Command {
4747
cmd := &cobra.Command{
4848
Use: "create NAME",
4949
Short: "Creates a secret.",
50-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
51-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
52-
if err != nil {
53-
return err
54-
}
50+
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
5551
if len(args) == 0 {
5652
return errors.New("Missing mandatory parameter: NAME")
5753
}
@@ -73,11 +69,7 @@ func InspectSecret(dockerCli command.Cli) *cobra.Command {
7369
cmd := &cobra.Command{
7470
Use: "inspect ID",
7571
Short: "Displays secret details",
76-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
77-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
78-
if err != nil {
79-
return err
80-
}
72+
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
8173
if len(args) == 0 {
8274
return errors.New("Missing mandatory parameter: ID")
8375
}
@@ -102,11 +94,7 @@ func ListSecrets(dockerCli command.Cli) *cobra.Command {
10294
Use: "list",
10395
Aliases: []string{"ls"},
10496
Short: "List secrets stored for the existing account.",
105-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
106-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
107-
if err != nil {
108-
return err
109-
}
97+
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
11098
secrets, err := backend.ListSecrets(context.Background())
11199
if err != nil {
112100
return err
@@ -125,11 +113,7 @@ func DeleteSecret(dockerCli command.Cli) *cobra.Command {
125113
Use: "delete NAME",
126114
Aliases: []string{"rm", "remove"},
127115
Short: "Removes a secret.",
128-
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, args []string) error {
129-
backend, err := amazon.NewBackend(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
130-
if err != nil {
131-
return err
132-
}
116+
RunE: docker.WithAwsContext(dockerCli, func(clusteropts docker.AwsContext, backend *amazon.Backend, args []string) error {
133117
if len(args) == 0 {
134118
return errors.New("Missing mandatory parameter: [NAME]")
135119
}

pkg/amazon/backend/list.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/compose-spec/compose-go/types"
7+
"github.com/compose-spec/compose-go/cli"
88
"github.com/docker/ecs-plugin/pkg/compose"
99
)
1010

11-
func (b *Backend) Ps(ctx context.Context, project *types.Project) ([]compose.ServiceStatus, error) {
11+
func (b *Backend) Ps(ctx context.Context, options cli.ProjectOptions) ([]compose.ServiceStatus, error) {
12+
project, err := cli.ProjectFromOptions(&options)
13+
if err != nil {
14+
return nil, err
15+
}
16+
1217
cluster := b.Cluster
1318
if cluster == "" {
1419
cluster = project.Name

pkg/amazon/backend/logs.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ import (
88
"strconv"
99
"strings"
1010

11+
"github.com/compose-spec/compose-go/cli"
12+
1113
"github.com/docker/ecs-plugin/pkg/console"
1214
)
1315

14-
func (b *Backend) Logs(ctx context.Context, projectName string) error {
15-
err := b.api.GetLogs(ctx, projectName, &logConsumer{
16+
func (b *Backend) Logs(ctx context.Context, options cli.ProjectOptions) error {
17+
name := options.Name
18+
if name == "" {
19+
project, err := cli.ProjectFromOptions(&options)
20+
if err != nil {
21+
return err
22+
}
23+
name = project.Name
24+
}
25+
26+
err := b.api.GetLogs(ctx, name, &logConsumer{
1627
colors: map[string]console.ColorFunc{},
1728
width: 0,
1829
})

pkg/amazon/sdk/sdk.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ func (s sdk) StackExists(ctx context.Context, name string) (bool, error) {
143143
StackName: aws.String(name),
144144
})
145145
if err != nil {
146-
// FIXME doesn't work as expected
146+
if strings.HasPrefix(err.Error(), fmt.Sprintf("ValidationError: Stack with id %s does not exist", name)) {
147+
return false, nil
148+
}
147149
return false, nil
148150
}
149151
return len(stacks.Stacks) > 0, nil

pkg/compose/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type API interface {
1313
Down(ctx context.Context, options cli.ProjectOptions) error
1414

1515
Convert(project *types.Project) (*cloudformation.Template, error)
16-
Logs(ctx context.Context, projectName string) error
17-
Ps(background context.Context, project *types.Project) ([]ServiceStatus, error)
16+
Logs(ctx context.Context, projectName cli.ProjectOptions) error
17+
Ps(background context.Context, options cli.ProjectOptions) ([]ServiceStatus, error)
1818

1919
CreateSecret(ctx context.Context, secret Secret) (string, error)
2020
InspectSecret(ctx context.Context, id string) (Secret, error)

pkg/docker/contextStore.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package docker
33
import (
44
"fmt"
55

6+
"github.com/aws/aws-sdk-go/aws/awserr"
67
"github.com/docker/cli/cli/command"
78
cliconfig "github.com/docker/cli/cli/config"
89
"github.com/docker/cli/cli/context/store"
10+
amazon "github.com/docker/ecs-plugin/pkg/amazon/backend"
911
"github.com/mitchellh/mapstructure"
1012
"github.com/spf13/cobra"
1113
)
@@ -72,15 +74,23 @@ func checkAwsContextExists(contextName string) (*AwsContext, error) {
7274
return &awsContext, nil
7375
}
7476

75-
type ContextFunc func(ctx AwsContext, args []string) error
77+
type ContextFunc func(ctx AwsContext, backend *amazon.Backend, args []string) error
7678

7779
func WithAwsContext(dockerCli command.Cli, f ContextFunc) func(cmd *cobra.Command, args []string) error {
7880
return func(cmd *cobra.Command, args []string) error {
7981
ctx, err := GetAwsContext(dockerCli)
8082
if err != nil {
8183
return err
8284
}
83-
return f(*ctx, args)
85+
backend, err := amazon.NewBackend(ctx.Profile, ctx.Cluster, ctx.Region)
86+
if err != nil {
87+
return err
88+
}
89+
err = f(*ctx, backend, args)
90+
if e, ok := err.(awserr.Error); ok {
91+
return fmt.Errorf(e.Message())
92+
}
93+
return err
8494
}
8595
}
8696

0 commit comments

Comments
 (0)