| 1 |
//go:build ignore |
| 2 |
// +build ignore |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
|
| 13 |
yaml "gopkg.in/yaml.v2" |
| 14 |
) |
| 15 |
|
| 16 |
type HeaderYaml struct { |
| 17 |
FileType string `yaml:"filetype"` |
| 18 |
Detect struct { |
| 19 |
FNameRgx string `yaml:"filename"` |
| 20 |
HeaderRgx string `yaml:"header"` |
| 21 |
SignatureRgx string `yaml:"signature"` |
| 22 |
} `yaml:"detect"` |
| 23 |
} |
| 24 |
|
| 25 |
type Header struct { |
| 26 |
FileType string |
| 27 |
FNameRgx string |
| 28 |
HeaderRgx string |
| 29 |
SignatureRgx string |
| 30 |
} |
| 31 |
|
| 32 |
func main() { |
| 33 |
if len(os.Args) > 1 { |
| 34 |
os.Chdir(os.Args[1]) |
| 35 |
} |
| 36 |
files, _ := os.ReadDir(".") |
| 37 |
for _, f := range files { |
| 38 |
fname := f.Name() |
| 39 |
if strings.HasSuffix(fname, ".yaml") { |
| 40 |
convert(fname[:len(fname)-5]) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func convert(name string) { |
| 46 |
filename := name + ".yaml" |
| 47 |
var hdr HeaderYaml |
| 48 |
source, err := os.ReadFile(filename) |
| 49 |
if err != nil { |
| 50 |
panic(err) |
| 51 |
} |
| 52 |
err = yaml.Unmarshal(source, &hdr) |
| 53 |
if err != nil { |
| 54 |
panic(err) |
| 55 |
} |
| 56 |
encode(name, hdr) |
| 57 |
} |
| 58 |
|
| 59 |
func encode(name string, c HeaderYaml) { |
| 60 |
f, _ := os.Create(name + ".hdr") |
| 61 |
f.WriteString(c.FileType + "\n") |
| 62 |
f.WriteString(c.Detect.FNameRgx + "\n") |
| 63 |
f.WriteString(c.Detect.HeaderRgx + "\n") |
| 64 |
f.WriteString(c.Detect.SignatureRgx + "\n") |
| 65 |
f.Close() |
| 66 |
} |
| 67 |
|
| 68 |
func decode(name string) Header { |
| 69 |
start := time.Now() |
| 70 |
data, _ := os.ReadFile(name + ".hdr") |
| 71 |
strs := bytes.Split(data, []byte{'\n'}) |
| 72 |
var hdr Header |
| 73 |
hdr.FileType = string(strs[0]) |
| 74 |
hdr.FNameRgx = string(strs[1]) |
| 75 |
hdr.HeaderRgx = string(strs[2]) |
| 76 |
hdr.SignatureRgx = string(strs[3]) |
| 77 |
fmt.Printf("took %v\n", time.Since(start)) |
| 78 |
|
| 79 |
return hdr |
| 80 |
} |