1
0
Fork 0
mirror of https://github.com/vbatts/go-mtree.git synced 2024-09-29 13:49:49 +00:00
go-mtree/hierarchy.go
Stephen Chung 5372b5fc47 hierarchy: provide option to list the used keywords in a spec
To increase a user's control on how they validate a directory or
tar archive with a specification, it is helpful to know which
keywords are actually used in the spec provided. This way, the user
can see what keywords to use or not use with the '-k' or '-K' flags.

Signed-off-by: Stephen Chung <schung@redhat.com>
2016-08-05 10:50:54 -04:00

51 lines
1.1 KiB
Go

package mtree
import (
"io"
"sort"
)
// DirectoryHierarchy is the mapped structure for an mtree directory hierarchy
// spec
type DirectoryHierarchy struct {
Entries []Entry
}
// WriteTo simplifies the output of the resulting hierarchy spec
func (dh DirectoryHierarchy) WriteTo(w io.Writer) (n int64, err error) {
sort.Sort(byPos(dh.Entries))
var sum int64
for _, e := range dh.Entries {
str := e.String()
i, err := io.WriteString(w, str+"\n")
if err != nil {
return sum, err
}
sum += int64(i)
}
return sum, nil
}
// CollectUsedKeywords collects and returns all the keywords used in a
// a DirectoryHierarchy
func CollectUsedKeywords(dh *DirectoryHierarchy) []string {
if dh != nil {
usedkeywords := []string{}
for _, e := range dh.Entries {
switch e.Type {
case FullType, RelativeType, SpecialType:
if e.Type != SpecialType || e.Name == "/set" {
kvs := e.Keywords
for _, kv := range kvs {
kw := KeyVal(kv).Keyword()
if !inSlice(kw, usedkeywords) {
usedkeywords = append(usedkeywords, kw)
}
}
}
}
}
return usedkeywords
}
return nil
}