Skip to content

Commit

Permalink
fix formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ShohamBit committed Nov 28, 2024
1 parent 276a26d commit 0737107
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 193 deletions.
17 changes: 0 additions & 17 deletions cmd/traceectl/pkg/cmd/formatter/JSONFormatter.go

This file was deleted.

128 changes: 0 additions & 128 deletions cmd/traceectl/pkg/cmd/formatter/TableFormatter.go

This file was deleted.

70 changes: 22 additions & 48 deletions cmd/traceectl/pkg/cmd/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,41 @@ package formatter

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

"github.com/spf13/cobra"
)

const (
FormatJSON = "json"
FormatTable = "table"
FormatGoTpl = "gotemplate"
)

var SupportedFormats = []string{FormatJSON, FormatTable, FormatGoTpl}
var SupportedFormats = []string{FormatJSON, FormatTable}

type Formatter struct {
Format string
Output string
CMD *cobra.Command
format string
cmd *cobra.Command
paddings map[int][]int
}

func New(format string, output string, cmd *cobra.Command) (*Formatter, error) {
if !containsFormat(format) {
func NewFormatter(format string, cmd *cobra.Command) (*Formatter, error) {
switch format {
case FormatJSON:
return &Formatter{
format: format,
cmd: cmd,
}, nil
case FormatTable:
//add padding for table
return &Formatter{
format: format,
cmd: cmd,
paddings: map[int][]int{
4: {20, 15, 15, 20}, // Padding for 4 columns
5: {15, 10, 20, 15, 10}, // Padding for 5 columns
},
}, nil
default:
return nil, fmt.Errorf("format %s is not supported", format)
}
if err := initOutput(cmd, output); err != nil {
return nil, err
}
return &Formatter{
Format: format,
Output: output,
CMD: cmd,
}, nil
}
func containsFormat(format string) bool {
for _, f := range SupportedFormats {
if f == format {
return true
}
}
return false
}
func initOutput(cmd *cobra.Command, output string) error {
if (output != "") && (output != "stdout") {
if output == "" || strings.TrimSpace(output) == "" {
return fmt.Errorf("output file path is empty or invalid")
}
dir := filepath.Dir(output)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("failed to create directories for output file: %v", err)
}
file, err := os.Create(output)
if err != nil {
return fmt.Errorf("failed to open output file: %v", err)
}

cmd.SetOut(file)
cmd.SetErr(file)
cmd.PersistentPostRun = func(cmd *cobra.Command, args []string) {
file.Close()
}
}
return nil
}
47 changes: 47 additions & 0 deletions cmd/traceectl/pkg/cmd/formatter/tableFormat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package formatter

// PrintTableHeaders prints table headers with padding based on their length.
func (f *Formatter) PrintTableHeaders(headers []string) {
switch len(headers) {
case 4:
f.cmd.Printf("%-20s %-15s %-15s %-20s\n",
headers[0],
headers[1],
headers[2],
headers[3],
)
case 5:
f.cmd.Printf("%-15s %-10s %-20s %-15s %-10s\n",
headers[0],
headers[1],
headers[2],
headers[3],
headers[4],
)
default:
f.cmd.Println("Error: Unsupported number of headers.")
}
}

// PrintTableRow prints a single row with padding matching the header format.
func (f *Formatter) PrintTableRow(row []string) {
switch len(row) {
case 4:
f.cmd.Printf("%-20s %-15s %-15s %-20s\n",
row[0],
row[1],
row[2],
row[3],
)
case 5:
f.cmd.Printf("%-15s %-10s %-20s %-15s %-10s\n",
row[0],
row[1],
row[2],
row[3],
row[4],
)
default:
f.cmd.Println("Error: Unsupported number of columns in row.")
}
}

0 comments on commit 0737107

Please sign in to comment.