1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-11-26 01:55:39 +00:00

gomtree: switch to using compare

Switch the commandline to use the .Compare() API when checking
specification files against the state of a tar archive or other archive.
The main purpose is to completely remove the check.go code from being
necessary (outside of a wrapper).

Signed-off-by: Aleksa Sarai <asarai@suse.de>
This commit is contained in:
Aleksa Sarai 2016-10-31 00:48:49 +11:00
parent 26ff922da6
commit d7f49531f8
No known key found for this signature in database
GPG key ID: 9E18AA267DDB8DB4

View file

@ -29,30 +29,34 @@ var (
flVersion = flag.Bool("version", false, "display the version of this tool") flVersion = flag.Bool("version", false, "display the version of this tool")
) )
var formats = map[string]func(*mtree.Result) string{ var formats = map[string]func([]mtree.InodeDelta) string{
// Outputs the errors in the BSD format. // Outputs the errors in the BSD format.
"bsd": func(r *mtree.Result) string { "bsd": func(d []mtree.InodeDelta) string {
var buffer bytes.Buffer var buffer bytes.Buffer
for _, fail := range r.Failures { for _, delta := range d {
fmt.Fprintln(&buffer, fail) if delta.Type() == mtree.Modified {
fmt.Fprintln(&buffer, delta)
}
} }
return buffer.String() return buffer.String()
}, },
// Outputs the full result struct in JSON. // Outputs the full result struct in JSON.
"json": func(r *mtree.Result) string { "json": func(d []mtree.InodeDelta) string {
var buffer bytes.Buffer var buffer bytes.Buffer
if err := json.NewEncoder(&buffer).Encode(r); err != nil { if err := json.NewEncoder(&buffer).Encode(d); err != nil {
panic(err) panic(err)
} }
return buffer.String() return buffer.String()
}, },
// Outputs only the paths which failed to validate. // Outputs only the paths which failed to validate.
"path": func(r *mtree.Result) string { "path": func(d []mtree.InodeDelta) string {
var buffer bytes.Buffer var buffer bytes.Buffer
for _, fail := range r.Failures { for _, delta := range d {
fmt.Fprintln(&buffer, fail.Path) if delta.Type() == mtree.Modified {
fmt.Fprintln(&buffer, delta.Path())
}
} }
return buffer.String() return buffer.String()
}, },
@ -66,6 +70,7 @@ func main() {
} }
// so that defers cleanly exec // so that defers cleanly exec
// TODO: Switch everything to being inside a function, to remove the need for isErr.
var isErr bool var isErr bool
defer func() { defer func() {
if isErr { if isErr {
@ -104,9 +109,11 @@ func main() {
} }
var ( var (
err error
tmpKeywords []string tmpKeywords []string
currentKeywords []string currentKeywords []string
) )
// -k <keywords> // -k <keywords>
if *flUseKeywords != "" { if *flUseKeywords != "" {
tmpKeywords = splitKeywordsArg(*flUseKeywords) tmpKeywords = splitKeywordsArg(*flUseKeywords)
@ -143,8 +150,23 @@ func main() {
currentKeywords = tmpKeywords currentKeywords = tmpKeywords
} }
// Check mutual exclusivity of keywords.
// TODO(cyphar): Abstract this inside keywords.go.
if inSlice("tar_time", currentKeywords) && inSlice("time", currentKeywords) {
log.Printf("tar_time and time are mutually exclusive keywords")
isErr = true
return
}
// If we're doing a comparison, we always are comparing between a spec and
// state DH. If specDh is nil, we are generating a new one.
var (
specDh *mtree.DirectoryHierarchy
stateDh *mtree.DirectoryHierarchy
specKeywords []string
)
// -f <file> // -f <file>
var dh *mtree.DirectoryHierarchy
if *flFile != "" && !*flCreate { if *flFile != "" && !*flCreate {
// 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(*flFile) fh, err := os.Open(*flFile)
@ -153,27 +175,30 @@ func main() {
isErr = true isErr = true
return return
} }
dh, err = mtree.ParseSpec(fh) specDh, err = mtree.ParseSpec(fh)
fh.Close() fh.Close()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
isErr = true isErr = true
return return
} }
// We can't check against more fields than in the specKeywords list, so
// currentKeywords can only have a subset of specKeywords.
specKeywords = mtree.CollectUsedKeywords(specDh)
} }
// -list-used // -list-used
if *flListUsedKeywords { if *flListUsedKeywords {
if *flFile == "" { if specDh == nil {
log.Println("no specification provided. please provide a validation manifest") log.Println("no specification provided. please provide a validation manifest")
defer os.Exit(1)
isErr = true isErr = true
return return
} }
usedKeywords := mtree.CollectUsedKeywords(dh)
if *flResultFormat == "json" { if *flResultFormat == "json" {
// if they're asking for json, give it to them // if they're asking for json, give it to them
data := map[string][]string{*flFile: usedKeywords} data := map[string][]string{*flFile: specKeywords}
buf, err := json.MarshalIndent(data, "", " ") buf, err := json.MarshalIndent(data, "", " ")
if err != nil { if err != nil {
defer os.Exit(1) defer os.Exit(1)
@ -183,7 +208,7 @@ func main() {
fmt.Println(string(buf)) fmt.Println(string(buf))
} else { } else {
fmt.Printf("Keywords used in [%s]:\n", *flFile) fmt.Printf("Keywords used in [%s]:\n", *flFile)
for _, kw := range usedKeywords { for _, kw := range specKeywords {
fmt.Printf(" %s", kw) fmt.Printf(" %s", kw)
if _, ok := mtree.KeywordFuncs[kw]; !ok { if _, ok := mtree.KeywordFuncs[kw]; !ok {
fmt.Print(" (unsupported)") fmt.Print(" (unsupported)")
@ -194,6 +219,36 @@ func main() {
return return
} }
if specKeywords != nil {
// If we didn't actually change the set of keywords, we can just use specKeywords.
if *flUseKeywords == "" && *flAddKeywords == "" {
currentKeywords = specKeywords
}
for _, keyword := range currentKeywords {
// As always, time is a special case.
// TODO: Fix that.
if (keyword == "time" && inSlice("tar_time", specKeywords)) || (keyword == "tar_time" && inSlice("time", specKeywords)) {
continue
}
if !inSlice(keyword, specKeywords) {
log.Printf("cannot verify keywords not in mtree specification: %s\n", keyword)
isErr = true
}
if isErr {
return
}
}
}
// -p and -T are mutually exclusive
if *flPath != "" && *flTar != "" {
log.Println("options -T and -p are mutually exclusive")
isErr = true
return
}
// -p <path> // -p <path>
var rootPath = "." var rootPath = "."
if *flPath != "" { if *flPath != "" {
@ -201,7 +256,6 @@ func main() {
} }
// -T <tar file> // -T <tar file>
var tdh *mtree.DirectoryHierarchy
if *flTar != "" { if *flTar != "" {
var input io.Reader var input io.Reader
if *flTar == "-" { if *flTar == "-" {
@ -229,7 +283,15 @@ func main() {
return return
} }
var err error var err error
tdh, err = ts.Hierarchy() stateDh, err = ts.Hierarchy()
if err != nil {
log.Println(err)
isErr = true
return
}
} else {
// with a root directory
stateDh, err = mtree.Walk(rootPath, nil, currentKeywords)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
isErr = true isErr = true
@ -239,36 +301,36 @@ func main() {
// -c // -c
if *flCreate { if *flCreate {
// create a directory hierarchy fh := os.Stdout
// with a tar stream if *flFile != "" {
if tdh != nil { fh, err = os.Create(*flFile)
tdh.WriteTo(os.Stdout)
} else {
// with a root directory
dh, err := mtree.Walk(rootPath, nil, currentKeywords)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
isErr = true isErr = true
return return
} }
dh.WriteTo(os.Stdout)
} }
} else if tdh != nil || dh != nil {
var res *mtree.Result // output stateDh
var err error stateDh.WriteTo(fh)
// else this is a validation return
if *flTar != "" {
res, err = mtree.TarCheck(tdh, dh, currentKeywords)
} else {
res, err = mtree.Check(rootPath, dh, currentKeywords)
} }
// This is a validation.
if specDh != nil && stateDh != nil {
var res []mtree.InodeDelta
res, err = mtree.Compare(specDh, stateDh, currentKeywords)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
isErr = true isErr = true
return return
} }
if res != nil && len(res.Failures) > 0 { if res != nil {
if len(res) > 0 {
defer os.Exit(1) defer os.Exit(1)
}
out := formatFunc(res) out := formatFunc(res)
if _, err := os.Stdout.Write([]byte(out)); err != nil { if _, err := os.Stdout.Write([]byte(out)); err != nil {
log.Println(err) log.Println(err)
@ -276,36 +338,9 @@ func main() {
return return
} }
} }
if res != nil {
if len(res.Extra) > 0 {
defer os.Exit(1)
for _, extra := range res.Extra {
extrapath, err := extra.Path()
if err != nil {
log.Println(err)
isErr = true
return
}
fmt.Printf("%s extra\n", extrapath)
}
}
if len(res.Missing) > 0 {
defer os.Exit(1)
for _, missing := range res.Missing {
missingpath, err := missing.Path()
if err != nil {
log.Println(err)
isErr = true
return
}
fmt.Printf("%s missing\n", missingpath)
}
}
}
} else { } else {
log.Println("neither validating or creating a manifest. Please provide additional arguments") log.Println("neither validating or creating a manifest. Please provide additional arguments")
isErr = true isErr = true
defer os.Exit(1)
return return
} }
} }