Skip to content

Commit

Permalink
simpler version
Browse files Browse the repository at this point in the history
  • Loading branch information
raulb committed Jan 31, 2025
1 parent fa36b33 commit c0d321d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 182 deletions.
29 changes: 2 additions & 27 deletions cmd/conduit/cecdysis/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ package cecdysis

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/conduitio/conduit/cmd/conduit/api"
"github.com/conduitio/conduit/pkg/conduit"
"github.com/conduitio/conduit/pkg/foundation/cerrors"
"github.com/conduitio/ecdysis"
"github.com/spf13/cobra"
"google.golang.org/grpc/status"
)

// ------------------- CommandWithClient
Expand Down Expand Up @@ -65,38 +61,17 @@ func (CommandWithExecuteWithClientDecorator) Decorate(_ *ecdysis.Ecdysis, cmd *c

client, err := api.NewClient(cmd.Context(), grpcAddress)
if err != nil {
return handleError(err)
return err
}
defer client.Close()

ctx := ecdysis.ContextWithCobraCommand(cmd.Context(), cmd)
return handleError(v.ExecuteWithClient(ctx, client))
return v.ExecuteWithClient(ctx, client)
}

return nil
}

// check what type of error is to see if it's worth showing `cmd.Usage()` or not.
// if any error is returned, usage will be shown automatically.
func handleError(err error) error {
errMsg := err.Error()
st, ok := status.FromError(err)

// an API error, we try to parse `desc`
if ok {
errMsg = st.Message()

// st.Message() is already an entire representation of the error
// need to grab the desc
descIndex := strings.Index(errMsg, "desc =")
if descIndex != -1 {
errMsg = errMsg[descIndex+len("desc = "):]
}
}
_, _ = fmt.Fprintf(os.Stderr, "%v\n", errMsg)
return nil
}

// getGRPCAddress returns the gRPC address configured by the user. If no address is found, the default address is returned.
func getGRPCAddress(cmd *cobra.Command) (string, error) {
var (
Expand Down
153 changes: 0 additions & 153 deletions cmd/conduit/cecdysis/decorators_test.go

This file was deleted.

7 changes: 5 additions & 2 deletions cmd/conduit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package main

import (
"fmt"
"os"

"github.com/conduitio/conduit/cmd/conduit/cecdysis"
Expand All @@ -29,8 +28,12 @@ func main() {
cmd := e.MustBuildCobraCommand(&root.RootCommand{})
cmd.CompletionOptions.DisableDefaultCmd = true

// Don't want to show usage when there's some unexpected error executing the command
// Help will still be shown via --help
cmd.SilenceUsage = true

if err := cmd.Execute(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
// error is already printed out
os.Exit(1)
}
os.Exit(0)
Expand Down

0 comments on commit c0d321d

Please sign in to comment.