Skip to content

Commit eefd2fd

Browse files
committed
update codes.
1 parent 2e817a8 commit eefd2fd

5 files changed

Lines changed: 38 additions & 20 deletions

File tree

cmd/config.go

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

33
type Config struct {
44
Verbose bool `json:"verbose"`
5-
Format string `json:"format"` //Format: table/json/csv/yaml2
6-
Output string `json:"output"` //Output write file
5+
Type string `json:"type"` //Type: table/json/csv/yaml
6+
Output string `json:"output"` //Output file
77
}

cmd/root.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ THE SOFTWARE.
2222
package cmd
2323

2424
import (
25+
"fmt"
26+
"os"
27+
"path/filepath"
28+
2529
"github.com/foolin/sumdiff/internal/util"
2630
"github.com/foolin/sumdiff/internal/vlog"
2731
"github.com/foolin/sumdiff/internal/write"
2832
"github.com/spf13/cobra"
29-
"os"
30-
"path/filepath"
3133
)
3234

33-
var config Config
35+
var config *Config
3436
var writer *write.Writer
3537
var file *os.File
3638

@@ -43,18 +45,21 @@ var rootCmd = &cobra.Command{
4345
// has an action associated with it:
4446
// Run: func(cmd *cobra.Command, args []string) { },
4547
PersistentPreRun: func(cmd *cobra.Command, args []string) {
48+
49+
fmt.Printf("Config: %#v", config)
4650
//Verbose
4751
vlog.SetVerbose(config.Verbose)
4852

4953
//Write
5054
t := write.Table
51-
if config.Format != "" {
55+
if config.Type != "" {
5256
var ok bool
53-
t, ok = write.TypeOfName(config.Format)
57+
t, ok = write.TypeOfName(config.Type)
5458
if !ok {
55-
vlog.Exit(1, "Format invalid: %v\n", config.Format)
59+
vlog.Exit(1, "Format invalid: %v\n", config.Type)
5660
return
5761
}
62+
fmt.Printf("Parse type: %v\n", t)
5863
}
5964

6065
w := os.Stdout
@@ -98,14 +103,14 @@ func init() {
98103
// Cobra supports persistent flags, which, if defined here,
99104
// will be global for your application.
100105

101-
config = Config{
106+
config = &Config{
102107
Verbose: false,
103-
Format: "",
108+
Type: "table",
104109
Output: "",
105110
}
106111
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sumdiff.yaml)")
107112
rootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "Verbose output info")
108-
rootCmd.PersistentFlags().StringVarP(&config.Format, "format", "f", "table", "Format: table|json|csv|yaml")
113+
rootCmd.PersistentFlags().StringVarP(&config.Type, "type", "t", "table", "Format: table|json|csv|yaml")
109114
rootCmd.PersistentFlags().StringVarP(&config.Output, "output", "o", "", "Output filename")
110115

111116
// Cobra also supports local flags, which will only run

internal/write/type_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package write
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestTypeOfName(t *testing.T) {
9+
typ, ok := TypeOfName("json")
10+
fmt.Printf("%v %v", typ, ok)
11+
}

internal/write/writer.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"encoding/csv"
55
"encoding/json"
66
"fmt"
7-
"github.com/liushuochen/gotable"
8-
"gopkg.in/yaml.v3"
97
"io"
108
"os"
9+
10+
"github.com/liushuochen/gotable"
11+
"gopkg.in/yaml.v3"
1112
)
1213

1314
type Writer struct {
14-
w io.Writer
15-
t Type
15+
writer io.Writer
16+
typ Type
1617
}
1718

1819
type TableData interface {
@@ -21,8 +22,8 @@ type TableData interface {
2122

2223
func New(w io.Writer, t Type) *Writer {
2324
return &Writer{
24-
w: w,
25-
t: t,
25+
writer: w,
26+
typ: t,
2627
}
2728
}
2829

@@ -31,7 +32,8 @@ func NewStd() *Writer {
3132
}
3233

3334
func (r *Writer) Write(data TableData) error {
34-
switch r.t {
35+
fmt.Printf("type: %v\n", r.typ)
36+
switch r.typ {
3537
case Table:
3638
return r.Table(data.Array())
3739
case Csv:
@@ -57,7 +59,7 @@ func (r *Writer) MustWrite(data TableData) {
5759
}
5860

5961
func (r *Writer) Printf(s string, a ...any) (n int, err error) {
60-
return r.w.Write([]byte(fmt.Sprintf(s, a...)))
62+
return r.writer.Write([]byte(fmt.Sprintf(s, a...)))
6163
}
6264

6365
func (r *Writer) Table(records [][]string) error {
@@ -85,7 +87,7 @@ func (r *Writer) Table(records [][]string) error {
8587
}
8688

8789
func (r *Writer) Csv(records [][]string) error {
88-
writer := csv.NewWriter(r.w)
90+
writer := csv.NewWriter(r.writer)
8991
return writer.WriteAll(records)
9092
}
9193

json

Whitespace-only changes.

0 commit comments

Comments
 (0)