Skip to content

Commit f31312d

Browse files
authored
Merge pull request #2
foolin/feat-write
2 parents 013bd36 + 64794e0 commit f31312d

File tree

26 files changed

+425
-214
lines changed

26 files changed

+425
-214
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ install:
22

33
build:
44
mkdir -p ./bin
5-
go build -o ./bin/sumdiff ./cli/sumdiff.go
5+
go build -o ./bin/sumdiff ./cli/main.go
66

77
install: build
88
sudo rm -rf ~/go/bin/sumdiff

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,6 @@ tar -xvf sumdiff_Linux_x86_64.tar.gz && sudo mv sumdiff /usr/local/bin
177177
```
178178

179179
## TODO
180-
[ ] Output table,csv,yaml,json
181-
[ ] Config display statusbar
182-
[ ] Write file
180+
- [ ] Output table,csv,yaml,json
181+
- [ ] Config display statusbar
182+
- [ ] Write file or console
File renamed without changes.

cmd/cmp.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ package cmd
2323

2424
import (
2525
"github.com/foolin/sumdiff"
26-
"github.com/foolin/sumdiff/internal/plog"
2726
"github.com/foolin/sumdiff/internal/statusbar"
2827
"github.com/foolin/sumdiff/vo"
2928

3029
"github.com/spf13/cobra"
3130
)
3231

32+
var detail bool
33+
3334
// cmpCmd represents the cmp command
3435
var cmpCmd = &cobra.Command{
3536
Use: "cmp",
@@ -40,10 +41,14 @@ var cmpCmd = &cobra.Command{
4041
ok, list, err := sumdiff.Cmp(args[0], args[1])
4142
statusbar.Stop()
4243
if err != nil {
43-
plog.Println("Happen error:", err)
44+
writer.MustWrite(vo.NewErrInfo(err))
45+
return
46+
}
47+
if detail {
48+
writer.MustWrite(list)
49+
} else {
50+
writer.MustWrite(vo.NewAny("result", ok))
4451
}
45-
plog.WriteTable(vo.CmpToLiteTable(list))
46-
plog.Println("result:", ok)
4752
},
4853
}
4954

@@ -54,7 +59,7 @@ func init() {
5459

5560
// Cobra supports Persistent Flags which will work for this command
5661
// and all subcommands, e.g.:
57-
// cmpCmd.PersistentFlags().String("foo", "", "A help for foo")
62+
cmpCmd.PersistentFlags().BoolVarP(&detail, "detail", "", false, "Show detail result")
5863

5964
// Cobra supports local flags which will only run when this command
6065
// is called directly, e.g.:

cmd/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cmd
2+
3+
type Config struct {
4+
Verbose bool `json:"verbose"`
5+
Format string `json:"format"` //Format: table/json/csv/yaml
6+
Output string `json:"output"` //Output file
7+
}

cmd/eq.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ package cmd
2323

2424
import (
2525
"github.com/foolin/sumdiff"
26-
"github.com/foolin/sumdiff/internal/plog"
2726
"github.com/foolin/sumdiff/internal/statusbar"
27+
"github.com/foolin/sumdiff/vo"
2828
"github.com/spf13/cobra"
2929
)
3030

@@ -39,9 +39,10 @@ var eqCmd = &cobra.Command{
3939
ret, err := sumdiff.Equal(args[0], args[1])
4040
statusbar.Stop()
4141
if err != nil {
42-
plog.Println(err.Error())
42+
writer.MustWrite(vo.NewErrInfo(err))
43+
return
4344
}
44-
plog.Writeln(ret)
45+
writer.MustWrite(vo.NewAny("result", ret))
4546
},
4647
}
4748

cmd/hash.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ package cmd
2323

2424
import (
2525
"github.com/foolin/sumdiff"
26-
"github.com/foolin/sumdiff/internal/plog"
2726
"github.com/foolin/sumdiff/internal/statusbar"
2827
"github.com/foolin/sumdiff/vo"
2928
"github.com/spf13/cobra"
@@ -37,17 +36,13 @@ var hashCmd = &cobra.Command{
3736
Args: cobra.MinimumNArgs(2),
3837
Run: func(cmd *cobra.Command, args []string) {
3938
statusbar.Start()
40-
results, err := sumdiff.HashWithArgs(args...)
39+
list, err := sumdiff.HashWithArgs(args...)
4140
statusbar.Stop()
4241
if err != nil {
43-
plog.Println(err)
42+
writer.MustWrite(vo.NewErrInfo(err))
4443
return
4544
}
46-
if len(results) == 1 {
47-
plog.Writeln(results[0].Hash)
48-
} else {
49-
plog.WriteTable(vo.HashToTable(results))
50-
}
45+
writer.MustWrite(list)
5146
},
5247
}
5348

cmd/md5.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/foolin/sumdiff/vo"
2727

2828
"github.com/foolin/sumdiff"
29-
"github.com/foolin/sumdiff/internal/plog"
3029
"github.com/foolin/sumdiff/internal/statusbar"
3130
"github.com/spf13/cobra"
3231
)
@@ -39,17 +38,13 @@ var md5Cmd = &cobra.Command{
3938
Args: cobra.MinimumNArgs(1),
4039
Run: func(cmd *cobra.Command, args []string) {
4140
statusbar.Start()
42-
results, err := sumdiff.Hash(md5.New(), args...)
41+
list, err := sumdiff.Hash(md5.New(), args...)
4342
statusbar.Stop()
4443
if err != nil {
45-
plog.Println(err)
44+
writer.MustWrite(vo.NewErrInfo(err))
4645
return
4746
}
48-
if len(results) == 1 {
49-
plog.Writeln(results[0].Hash)
50-
} else {
51-
plog.WriteTable(vo.HashToTable(results))
52-
}
47+
writer.MustWrite(list)
5348
},
5449
}
5550

cmd/root.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ THE SOFTWARE.
2222
package cmd
2323

2424
import (
25-
"github.com/foolin/sumdiff/internal/plog"
2625
"os"
26+
"path/filepath"
2727

28+
"github.com/foolin/sumdiff/internal/util"
29+
"github.com/foolin/sumdiff/internal/vlog"
30+
"github.com/foolin/sumdiff/internal/write"
2831
"github.com/spf13/cobra"
2932
)
3033

31-
var verbose bool
34+
var config *Config
35+
var writer *write.Writer
36+
var file *os.File
3237

3338
// rootCmd represents the base command when called without any subcommands
3439
var rootCmd = &cobra.Command{
@@ -39,8 +44,43 @@ var rootCmd = &cobra.Command{
3944
// has an action associated with it:
4045
// Run: func(cmd *cobra.Command, args []string) { },
4146
PersistentPreRun: func(cmd *cobra.Command, args []string) {
42-
//fmt.Println("verbose:", verbose)
43-
plog.SetVerbose(verbose)
47+
//Verbose
48+
vlog.SetVerbose(config.Verbose)
49+
50+
//Write
51+
format := write.Table
52+
if config.Format != "" {
53+
var ok bool
54+
format, ok = write.FormatOfName(config.Format)
55+
if !ok {
56+
vlog.Exit(1, "Format invalid: %v\n", config.Format)
57+
return
58+
}
59+
}
60+
61+
w := os.Stdout
62+
if config.Output != "" {
63+
path := util.FormatPath(config.Output)
64+
_ = os.MkdirAll(filepath.Dir(path), 0755)
65+
var err error
66+
file, err = os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
67+
if err != nil {
68+
vlog.Exit(1, "Open file %v error: %\n", path, err)
69+
return
70+
}
71+
w = file
72+
}
73+
74+
//Create writer
75+
writer = write.New(w, format)
76+
},
77+
PersistentPostRun: func(cmd *cobra.Command, args []string) {
78+
if file != nil {
79+
err := file.Close()
80+
if err != nil {
81+
vlog.Printf("Close file error: %v\n", err)
82+
}
83+
}
4484
},
4585
}
4686

@@ -59,8 +99,15 @@ func init() {
5999
// Cobra supports persistent flags, which, if defined here,
60100
// will be global for your application.
61101

102+
config = &Config{
103+
Verbose: false,
104+
Format: "table",
105+
Output: "",
106+
}
62107
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sumdiff.yaml)")
63-
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output info")
108+
rootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "Verbose output info")
109+
rootCmd.PersistentFlags().StringVarP(&config.Format, "format", "f", "table", "Format: table|json|csv|yaml")
110+
rootCmd.PersistentFlags().StringVarP(&config.Output, "output", "o", "", "Output filename")
64111

65112
// Cobra also supports local flags, which will only run
66113
// when this action is called directly.

cmd/sha1.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/foolin/sumdiff/vo"
2727

2828
"github.com/foolin/sumdiff"
29-
"github.com/foolin/sumdiff/internal/plog"
3029
"github.com/foolin/sumdiff/internal/statusbar"
3130
"github.com/spf13/cobra"
3231
)
@@ -39,17 +38,13 @@ var sha1Cmd = &cobra.Command{
3938
Args: cobra.MinimumNArgs(1),
4039
Run: func(cmd *cobra.Command, args []string) {
4140
statusbar.Start()
42-
results, err := sumdiff.Hash(sha1.New(), args...)
41+
list, err := sumdiff.Hash(sha1.New(), args...)
4342
statusbar.Stop()
4443
if err != nil {
45-
plog.Println(err)
44+
writer.MustWrite(vo.NewErrInfo(err))
4645
return
4746
}
48-
if len(results) == 1 {
49-
plog.Writeln(results[0].Hash)
50-
} else {
51-
plog.WriteTable(vo.HashToTable(results))
52-
}
47+
writer.MustWrite(list)
5348
},
5449
}
5550

0 commit comments

Comments
 (0)