Skip to content

Commit

Permalink
Refactor error messages to include the provided output format
Browse files Browse the repository at this point in the history
  • Loading branch information
TaeyeonRoyce committed Sep 28, 2024
1 parent 2a834ee commit 621be0e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
12 changes: 6 additions & 6 deletions cmd/yorkie/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package context

import (
"encoding/json"
"errors"
"fmt"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -81,8 +81,8 @@ func newListCommand() *cobra.Command {
}
}

func printContexts(cmd *cobra.Command, outputFormat string, contexts []contextInfo) error {
switch outputFormat {
func printContexts(cmd *cobra.Command, output string, contexts []contextInfo) error {
switch output {
case "":
tw := table.NewWriter()
tw.Style().Options.DrawBorder = false
Expand All @@ -99,17 +99,17 @@ func printContexts(cmd *cobra.Command, outputFormat string, contexts []contextIn
case "json":
marshalled, err := json.MarshalIndent(contexts, "", " ")
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(marshalled))
case "yaml":
marshalled, err := yaml.Marshal(contexts)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(marshalled))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down
11 changes: 6 additions & 5 deletions cmd/yorkie/document/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -81,8 +82,8 @@ func newListCommand() *cobra.Command {
}
}

func printDocuments(cmd *cobra.Command, outputFormat string, documents []*types.DocumentSummary) error {
switch outputFormat {
func printDocuments(cmd *cobra.Command, output string, documents []*types.DocumentSummary) error {
switch output {
case "":
tw := table.NewWriter()
tw.Style().Options.DrawBorder = false
Expand Down Expand Up @@ -112,17 +113,17 @@ func printDocuments(cmd *cobra.Command, outputFormat string, documents []*types.
case "json":
jsonOutput, err := json.MarshalIndent(documents, "", " ")
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(jsonOutput))
case "yaml":
yamlOutput, err := yaml.Marshal(documents)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(yamlOutput))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions cmd/yorkie/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -109,17 +110,17 @@ func printHistories(cmd *cobra.Command, output string, changes []*types.ChangeSu
case JSONOutput:
jsonOutput, err := json.MarshalIndent(changes, "", " ")
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(jsonOutput))
case YamlOutput:
yamlOutput, err := yaml.Marshal(changes)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(yamlOutput))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions cmd/yorkie/project/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -88,17 +89,17 @@ func printCreateProjectInfo(cmd *cobra.Command, output string, project *types.Pr
case JSONOutput, DefaultOutput:
encoded, err := json.Marshal(project)
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(encoded))
case YamlOutput:
marshalled, err := yaml.Marshal(project)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(marshalled))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/yorkie/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package project
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -104,17 +104,17 @@ func printProjects(cmd *cobra.Command, output string, projects []*types.Project)
case JSONOutput:
jsonOutput, err := json.MarshalIndent(projects, "", " ")
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(jsonOutput))
case YamlOutput:
yamlOutput, err := yaml.Marshal(projects)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(yamlOutput))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions cmd/yorkie/project/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -123,17 +124,17 @@ func printUpdateProjectInfo(cmd *cobra.Command, output string, project *types.Pr
case JSONOutput, DefaultOutput:
encoded, err := json.Marshal(project)
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(encoded))
case YamlOutput:
encoded, err := yaml.Marshal(project)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(encoded))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions cmd/yorkie/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime"

"connectrpc.com/connect"
Expand Down Expand Up @@ -53,7 +54,7 @@ func newVersionCmd() *cobra.Command {
info.ServerVersion, serverErr = fetchServerVersion()
}

output = viper.GetString("output")
output := viper.GetString("output")
if err := printVersionInfo(cmd, output, &info); err != nil {
return err
}
Expand Down Expand Up @@ -128,17 +129,17 @@ func printVersionInfo(cmd *cobra.Command, output string, versionInfo *types.Vers
case YamlOutput:
marshalled, err := yaml.Marshal(versionInfo)
if err != nil {
return errors.New("marshal YAML")
return fmt.Errorf("failed to marshal YAML: %w", err)
}
cmd.Println(string(marshalled))
case JSONOutput:
marshalled, err := json.MarshalIndent(versionInfo, "", " ")
if err != nil {
return errors.New("marshal JSON")
return fmt.Errorf("failed to marshal JSON: %w", err)
}
cmd.Println(string(marshalled))
default:
return errors.New("unknown output format")
return fmt.Errorf("unknown output format: %s", output)
}

return nil
Expand Down

0 comments on commit 621be0e

Please sign in to comment.