forked from getlantern/marionette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmar.go
75 lines (61 loc) · 1.75 KB
/
mar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package mar
import (
"path"
"sort"
"strings"
)
//go:generate go-bindata -ignore .go -o mar.gen.go -pkg mar ./...
var FormatVersions = []string{"20150701", "20150702"}
// Format returns the contents of the named MAR file.
// If the verison is not specified then latest version is returned.
// Returns nil if the format does not exist.
func Format(name, version string) []byte {
// Return specific version, if specified.
if version != "" {
buf, _ := Asset(path.Join("formats", version, name+".mar"))
return buf
}
// Otherwise iterate over versions from newest to oldest.
for i := len(FormatVersions) - 1; i >= 0; i-- {
if buf, _ := Asset(path.Join("formats", FormatVersions[i], name+".mar")); buf != nil {
return buf
}
}
return nil
}
// Formats returns a list of available formats.
func Formats() []string {
var formats []string
names := AssetNames()
sort.Strings(names)
for _, name := range names {
// Ignore files outside 'formats' directory.
if !strings.HasPrefix(name, "formats/") {
continue
}
// Remove subdir and extension.
name = strings.TrimPrefix(name, "formats/")
name = strings.TrimSuffix(name, ".mar")
// Move version to the end.
segments := strings.SplitN(name, "/", 2)
format := segments[1] + ":" + segments[0]
// Add to format list.
formats = append(formats, format)
}
return formats
}
// SplitFormat splits a fully qualified format name into it's name and version parts.
func SplitFormat(s string) (name, version string) {
a := strings.SplitN(s, ":", 2)
if len(a) == 1 {
return a[0], ""
}
return a[0], a[1]
}
// StripFormatVersion removes any version specified on a format.
func StripFormatVersion(format string) string {
if i := strings.Index(format, ":"); i != -1 {
return format[:i]
}
return format
}