Skip to content

Commit 1b2fb9c

Browse files
authored
Merge pull request #3
Add version
2 parents f31312d + 03bdfc2 commit 1b2fb9c

File tree

18 files changed

+186
-87
lines changed

18 files changed

+186
-87
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ before:
1717

1818
builds:
1919
- binary: sumdiff
20-
main: ./cli/sumdiff.go
20+
main: ./cli/main.go
2121
goos:
2222
- linux
2323
- darwin

README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# sumdiff
22
Compare sdk/tools for directory and files.
33

4+
## Features
5+
- [x] Support comparing files or directories result is equals.
6+
- [x] Supports comparing files or directories with the difference list.
7+
- [x] Support for calculating the hash of a file or directories md5/sha1/sha256/sha512.
8+
- [x] This library supports both SDK and CLI tools.
9+
- [x] CLI support result format : table/json/csv/yaml
10+
- [x] CLI support result output to a file.
11+
412
# SDK
513

614
## Install
@@ -12,38 +20,42 @@ go get -u github.com/foolin/sumdiff
1220
```go
1321

1422
//Compare
15-
ok, result, err := Cmp(path1, path2)
23+
ok, result, err := sumdiff.Cmp(path1, path2)
1624

1725
//Equal
18-
ok, err := Equal(v.path1, v.path2)
26+
ok, err := sumdiff.Equal(v.path1, v.path2)
1927

2028
```
2129

2230
# CLI Tool
2331

2432
```
2533
sumdiff --help
34+
2635
A useful comparison tool for differences and hash
2736
2837
Usage:
2938
sumdiff [command]
3039
3140
Available Commands:
32-
eq Compare whether two files or directory are equal
3341
cmp Compare the two files or directories are different
42+
completion Generate the autocompletion script for the specified shell
43+
eq Compare whether two files or directory are equal
3444
hash Calculate hash algorithm [md5|sha1|sha256|sha512] hex string
3545
help Help about any command
3646
md5 Calculate md5 hex string
3747
sha1 Calculate sha1 hex string
3848
sha256 Calculate sha256 hex string
39-
completion Generate the autocompletion script for the specified shell
40-
49+
4150
Flags:
42-
-h, --help help for sumdiff
43-
-v, --verbose Verbose output info
51+
-f, --format string Format: table|json|csv|yaml (default "table")
52+
-h, --help help for sumdiff
53+
-o, --output string Output filename
54+
-v, --verbose Verbose output info
4455
4556
Use "sumdiff [command] --help" for more information about a command.
4657
58+
4759
```
4860

4961
# Usage
@@ -58,7 +70,11 @@ Use "sumdiff [command] --help" for more information about a command.
5870
```
5971
Output1:
6072
```text
61-
true
73+
+--------+
74+
| result |
75+
+--------+
76+
| true |
77+
+--------+
6278
```
6379

6480
Example2:
@@ -174,9 +190,4 @@ linux:
174190
175191
tar -xvf sumdiff_Linux_x86_64.tar.gz && sudo mv sumdiff /usr/local/bin
176192
177-
```
178-
179-
## TODO
180-
- [ ] Output table,csv,yaml,json
181-
- [ ] Config display statusbar
182-
- [ ] Write file or console
193+
```

bin/sumdiff

786 KB
Binary file not shown.

cli/main.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,32 @@ THE SOFTWARE.
2121
*/
2222
package main
2323

24-
import "github.com/foolin/sumdiff/cmd"
24+
import (
25+
"github.com/foolin/sumdiff/cmd"
26+
"github.com/foolin/sumdiff/vo"
27+
)
28+
29+
var (
30+
version = "devel"
31+
commit = "none"
32+
date = "unknown"
33+
)
2534

2635
func main() {
27-
cmd.Execute()
36+
cmd.Execute(buildVersion(version, commit, date))
37+
}
38+
39+
func buildVersion(version, commit, date string) vo.AppInfo {
40+
info := vo.NewAppInfo()
41+
if version != "" {
42+
info.Version = version
43+
}
44+
if commit != "" {
45+
info.Commit = commit
46+
}
47+
if date != "" {
48+
info.Date = date
49+
}
50+
51+
return info
2852
}

cmd/cmp.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,24 @@ package cmd
2424
import (
2525
"github.com/foolin/sumdiff"
2626
"github.com/foolin/sumdiff/internal/statusbar"
27-
"github.com/foolin/sumdiff/vo"
28-
27+
"github.com/foolin/sumdiff/internal/vlog"
2928
"github.com/spf13/cobra"
3029
)
3130

32-
var detail bool
33-
3431
// cmpCmd represents the cmp command
3532
var cmpCmd = &cobra.Command{
3633
Use: "cmp",
37-
Short: "Compare the two files or directories are different",
38-
Long: `Compare the two files or directories are different`,
34+
Short: "Compare two files or directories for a list of differences",
35+
Long: `Compare two files or directories for a list of differences`,
3936
Run: func(cmd *cobra.Command, args []string) {
4037
statusbar.Start()
4138
ok, list, err := sumdiff.Cmp(args[0], args[1])
4239
statusbar.Stop()
4340
if err != nil {
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))
41+
vlog.Printf("Error: %v", err)
5142
}
43+
vlog.Printf("Result: %v", ok)
44+
writer.MustWrite(list)
5245
},
5346
}
5447

@@ -59,7 +52,7 @@ func init() {
5952

6053
// Cobra supports Persistent Flags which will work for this command
6154
// and all subcommands, e.g.:
62-
cmpCmd.PersistentFlags().BoolVarP(&detail, "detail", "", false, "Show detail result")
55+
//cmpCmd.PersistentFlags().BoolVarP(&detail, "detail", "", false, "Show detail result")
6356

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

cmd/eq.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package cmd
2424
import (
2525
"github.com/foolin/sumdiff"
2626
"github.com/foolin/sumdiff/internal/statusbar"
27+
"github.com/foolin/sumdiff/internal/vlog"
2728
"github.com/foolin/sumdiff/vo"
2829
"github.com/spf13/cobra"
2930
)
@@ -39,10 +40,9 @@ var eqCmd = &cobra.Command{
3940
ret, err := sumdiff.Equal(args[0], args[1])
4041
statusbar.Stop()
4142
if err != nil {
42-
writer.MustWrite(vo.NewErrInfo(err))
43-
return
43+
vlog.Printf("Error: %v", err)
4444
}
45-
writer.MustWrite(vo.NewAny("result", ret))
45+
writer.MustWrite(vo.NewAnyValue(ret))
4646
},
4747
}
4848

cmd/md5.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import (
3333
// md5Cmd represents the md5 command
3434
var md5Cmd = &cobra.Command{
3535
Use: "md5 <path> ...",
36-
Short: "Calculate md5 hex string",
37-
Long: `Calculate md5 hex string, eg: hash sha1 a.text`,
36+
Short: "Calculate md5 hexadecimal string",
37+
Long: `Calculate md5 hexadecimal string, eg: hash sha1 a.text`,
3838
Args: cobra.MinimumNArgs(1),
3939
Run: func(cmd *cobra.Command, args []string) {
4040
statusbar.Start()

cmd/root.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ THE SOFTWARE.
2222
package cmd
2323

2424
import (
25+
"fmt"
26+
"github.com/foolin/sumdiff/vo"
2527
"os"
2628
"path/filepath"
2729

@@ -53,7 +55,7 @@ var rootCmd = &cobra.Command{
5355
var ok bool
5456
format, ok = write.FormatOfName(config.Format)
5557
if !ok {
56-
vlog.Exit(1, "Format invalid: %v\n", config.Format)
58+
vlog.Exit("Format invalid: %v\n", config.Format)
5759
return
5860
}
5961
}
@@ -65,7 +67,7 @@ var rootCmd = &cobra.Command{
6567
var err error
6668
file, err = os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
6769
if err != nil {
68-
vlog.Exit(1, "Open file %v error: %\n", path, err)
70+
vlog.Exit("Open file %v error: %\n", path, err)
6971
return
7072
}
7173
w = file
@@ -86,9 +88,11 @@ var rootCmd = &cobra.Command{
8688

8789
// Execute adds all child commands to the root command and sets flags appropriately.
8890
// This is called by main.main(). It only needs to happen once to the rootCmd.
89-
func Execute() {
91+
func Execute(appInfo vo.AppInfo) {
92+
appInfo.Description = fmt.Sprintf("%v: %v", rootCmd.Use, rootCmd.Long)
93+
rootCmd.Version = appInfo.String()
94+
rootCmd.SetVersionTemplate("{{.Version}}")
9095
err := rootCmd.Execute()
91-
9296
if err != nil {
9397
os.Exit(1)
9498
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ go 1.20
44

55
require (
66
github.com/hashicorp/go-multierror v1.1.1
7+
github.com/liushuochen/gotable v0.0.0-20221119160816-1113793e7092
78
github.com/mattn/go-runewidth v0.0.15
89
github.com/spf13/cobra v1.8.0
910
github.com/stretchr/testify v1.8.4
11+
gopkg.in/yaml.v3 v3.0.1
1012
)
1113

1214
require (
1315
github.com/davecgh/go-spew v1.1.1 // indirect
1416
github.com/hashicorp/errwrap v1.0.0 // indirect
1517
github.com/inconshreveable/mousetrap v1.1.0 // indirect
16-
github.com/liushuochen/gotable v0.0.0-20221119160816-1113793e7092 // indirect
1718
github.com/pmezard/go-difflib v1.0.0 // indirect
1819
github.com/rivo/uniseg v0.2.0 // indirect
1920
github.com/spf13/pflag v1.0.5 // indirect
20-
gopkg.in/yaml.v3 v3.0.1 // indirect
2121
)

internal/util/file.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ func ListPath(path string, acceptFn func(info PathInfo) bool) (map[string]PathIn
3434
func WalkPath(root string, fn WalkFunc) error {
3535
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
3636
relative := RelativePath(path, root)
37+
if relative == "" {
38+
return nil
39+
}
3740
walkInfo := PathInfo{
3841
Relative: relative,
3942
Path: path,
@@ -78,5 +81,11 @@ func AbsPath(path string) string {
7881
}
7982

8083
func RelativePath(path, root string) string {
81-
return strings.TrimPrefix(AbsPath(path), AbsPath(root))
84+
absPath := AbsPath(path)
85+
absRoot := AbsPath(root)
86+
relative := strings.TrimPrefix(absPath, absRoot)
87+
if len(absPath) > len(relative) {
88+
return strings.TrimPrefix(relative, string(filepath.Separator))
89+
}
90+
return relative
8291
}

0 commit comments

Comments
 (0)