1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-21 16:05:40 +00:00

Merge pull request #173 from vbatts/compare-two-files

Compare two files
This commit is contained in:
Vincent Batts 2022-04-04 18:17:07 -04:00 committed by GitHub
commit 7fed71e221
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 19 deletions

View file

@ -27,7 +27,7 @@ func main() {
Aliases: []string{"c"}, Aliases: []string{"c"},
Usage: "Create a directory hierarchy spec", Usage: "Create a directory hierarchy spec",
}, },
&cli.StringFlag{ &cli.StringSliceFlag{
Name: "file", Name: "file",
Aliases: []string{"f"}, Aliases: []string{"f"},
Usage: "Directory hierarchy spec to validate", Usage: "Directory hierarchy spec to validate",
@ -182,9 +182,9 @@ func mainApp(c *cli.Context) error {
) )
// -f <file> // -f <file>
if c.String("file") != "" && !c.Bool("create") { if len(c.StringSlice("file")) > 0 && !c.Bool("create") {
// load the hierarchy, if we're not creating a new spec // load the hierarchy, if we're not creating a new spec
fh, err := os.Open(c.String("file")) fh, err := os.Open(c.StringSlice("file")[0])
if err != nil { if err != nil {
return err return err
} }
@ -206,21 +206,25 @@ func mainApp(c *cli.Context) error {
} }
if c.String("result-format") == "json" { if c.String("result-format") == "json" {
// if they're asking for json, give it to them for _, file := range c.StringSlice("file") {
data := map[string][]mtree.Keyword{c.String("file"): specKeywords} // if they're asking for json, give it to them
buf, err := json.MarshalIndent(data, "", " ") data := map[string][]mtree.Keyword{file: specKeywords}
if err != nil { buf, err := json.MarshalIndent(data, "", " ")
return err if err != nil {
} return err
fmt.Println(string(buf)) }
} else { fmt.Println(string(buf))
fmt.Printf("Keywords used in [%s]:\n", c.String("file")) }
for _, kw := range specKeywords { } else {
fmt.Printf(" %s", kw) for _, file := range c.StringSlice("file") {
if _, ok := mtree.KeywordFuncs[kw]; !ok { fmt.Printf("Keywords used in [%s]:\n", file)
fmt.Print(" (unsupported)") for _, kw := range specKeywords {
fmt.Printf(" %s", kw)
if _, ok := mtree.KeywordFuncs[kw]; !ok {
fmt.Print(" (unsupported)")
}
fmt.Printf("\n")
} }
fmt.Printf("\n")
} }
} }
return nil return nil
@ -290,6 +294,17 @@ func mainApp(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
} else if len(c.StringSlice("file")) > 1 {
// load this second hierarchy file provided
fh, err := os.Open(c.StringSlice("file")[1])
if err != nil {
return err
}
stateDh, err = mtree.ParseSpec(fh)
fh.Close()
if err != nil {
return err
}
} else { } else {
// with a root directory // with a root directory
stateDh, err = mtree.Walk(rootPath, excludes, currentKeywords, nil) stateDh, err = mtree.Walk(rootPath, excludes, currentKeywords, nil)
@ -338,8 +353,8 @@ func mainApp(c *cli.Context) error {
// -c // -c
if c.Bool("create") { if c.Bool("create") {
fh := os.Stdout fh := os.Stdout
if c.String("file") != "" { if len(c.StringSlice("file")) > 0 {
fh, err = os.Create(c.String("file")) fh, err = os.Create(c.StringSlice("file")[0])
if err != nil { if err != nil {
return err return err
} }

25
test/cli/0010.sh Normal file
View file

@ -0,0 +1,25 @@
#!/bin/bash
set -e
name=$(basename $0)
root="$(dirname $(dirname $(dirname $0)))"
gomtree=$(go run ${root}/test/realpath/main.go ${root}/gomtree)
t=$(mktemp -d /tmp/go-mtree.XXXXXX)
echo "[${name}] Running in ${t}"
## testing comparing two files
pushd ${root}
mkdir -p ${t}/extract
git archive --format=tar HEAD^{tree} . | tar -C ${t}/extract/ -x
${gomtree} -K sha256digest -c -p ${t}/extract/ > ${t}/${name}-1.mtree
rm -rf ${t}/extract/*.go
${gomtree} -K sha256digest -c -p ${t}/extract/ > ${t}/${name}-2.mtree
# this _ought_ to fail because the files are missing now
! ${gomtree} -f ${t}/${name}-1.mtree -f ${t}/${name}-2.mtree
popd
rm -rf ${t}