-
Notifications
You must be signed in to change notification settings - Fork 1
/
namaste.go
175 lines (160 loc) · 4.88 KB
/
namaste.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// namaste.go implements the "Name as Text" encoding/decoding for embedding metadata signatures in a directory.
//
// Authors R. S. Doiel, <[email protected]>
//
// Copyright (c) 2020, Caltech
// All rights not granted herein are expressly reserved by Caltech.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
package namaste
import (
"fmt"
"os"
"path"
"strings"
)
var (
normalizeFieldName = map[string]string{
"type": "0",
"who": "1",
"what": "2",
"when": "3",
"where": "4",
}
)
func Encode(tag, value string) string {
if s, ok := normalizeFieldName[strings.ToLower(tag)]; ok == true {
tag = s
}
return fmt.Sprintf("%s=%s", tag, charEncode(value))
}
func Decode(value string) string {
for _, prefix := range []string{"0=", "1=", "2=", "3=", "4="} {
if strings.HasPrefix(value, prefix) {
return charDecode(value[2:])
}
}
return charDecode(value)
}
func getNamaste(dName, tag string) ([]string, error) {
prefix := Encode(tag, "")
results := []string{}
dInfo, err := os.Stat(dName)
if err != nil {
return nil, err
}
if dInfo.IsDir() == false {
return nil, fmt.Errorf("expected %q to be a directory", dName)
}
items, err := os.ReadDir(dName)
if err != nil {
return nil, err
}
for _, item := range items {
name := item.Name()
if strings.HasPrefix(name, prefix) {
results = append(results, name)
}
}
return results, nil
}
func setNamaste(dName, tag, value string) (string, error) {
dInfo, err := os.Stat(dName)
if err != nil {
return "", err
}
if dInfo.IsDir() == false {
return "", fmt.Errorf("%q is not a directory", dName)
}
sNamaste := Encode(tag, value)
return sNamaste, os.WriteFile(path.Join(dName, sNamaste), []byte(value+"\n"), 0664)
}
func DirType(dName, val string) (string, error) {
return setNamaste(dName, "0", val)
}
func Who(dName, val string) (string, error) {
return setNamaste(dName, "1", val)
}
func What(dName, val string) (string, error) {
return setNamaste(dName, "2", val)
}
func When(dName, val string) (string, error) {
return setNamaste(dName, "3", val)
}
func Where(dName, val string) (string, error) {
return setNamaste(dName, "4", val)
}
func Note(dName, val string) (string, error) {
return setNamaste(dName, "note", val)
}
func Get(dName string, kinds []string) ([]string, error) {
if len(kinds) == 0 {
kinds = []string{"0", "1", "2", "3", "4", "note"}
} else {
// Convert to numeric string from human text, e.g. type, who, when
for i, val := range kinds {
if s, ok := normalizeFieldName[strings.ToLower(val)]; ok == true {
kinds[i] = s
}
}
}
results := []string{}
for _, kind := range kinds {
l, err := getNamaste(dName, kind)
if err != nil {
return results, err
}
if len(l) > 0 {
results = append(results, l...)
}
}
return results, nil
}
func GetTypes(dName string) (map[string]map[string]string, error) {
typeTags, err := getNamaste(dName, "0")
if err != nil {
return nil, err
}
types := map[string]map[string]string{}
var (
name string
version []string
)
for _, t := range typeTags {
s := strings.SplitN(strings.TrimPrefix(t, "0="), "_", 2)
name = s[0]
if len(s) > 1 {
version = strings.SplitN(s[1], ".", 2)
} else {
version = []string{}
}
switch len(version) {
case 2:
types[name] = map[string]string{
"name": name,
"major": version[0],
"minor": version[1],
}
case 1:
types[name] = map[string]string{
"name": name,
"major": version[1],
}
default:
types[name] = map[string]string{
"name": name,
}
}
}
return types, nil
}