/
main.go
250 lines (169 loc) · 5.94 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/sfomuseum/go-flags/multi"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"github.com/whosonfirst/go-reader"
export "github.com/whosonfirst/go-whosonfirst-export/v2"
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
wof_writer "github.com/whosonfirst/go-whosonfirst-writer/v3"
"github.com/whosonfirst/go-writer/v3"
)
func main() {
source := flag.String("s", "", "A valid path to the root directory of the Who's On First data repository. If empty (and -reader-uri or -writer-uri are empty) the current working directory will be used and appended with a 'data' subdirectory.")
id := flag.String("i", "", "A valid Who's On First ID.")
reader_uri := flag.String("reader-uri", "", "A valid whosonfirst/go-reader URI. If empty the value of the -s flag will be used in combination with the fs:// scheme.")
writer_uri := flag.String("writer-uri", "", "A valid whosonfirst/go-writer URI. If empty the value of the -s flag will be used in combination with the fs:// scheme.")
exporter_uri := flag.String("exporter-uri", "whosonfirst://", "A valid whosonfirst/go-whosonfirst-export URI.")
var str_properties multi.KeyValueString
flag.Var(&str_properties, "string-property", "One or more {KEY}={VALUE} properties to append to the new record where {KEY} is a valid tidwall/gjson path and {VALUE} is a string value.")
var int_properties multi.KeyValueInt64
flag.Var(&str_properties, "int-property", "One or more {KEY}={VALUE} properties to append to the new record where {KEY} is a valid tidwall/gjson path and {VALUE} is a int(64) value.")
var float_properties multi.KeyValueFloat64
flag.Var(&str_properties, "float-property", "One or more {KEY}={VALUE} properties to append to the new record where {KEY} is a valid tidwall/gjson path and {VALUE} is a float(64) value.")
var ids multi.MultiInt64
flag.Var(&ids, "id", "One or more Who's On First IDs. If left empty the value of the -i flag will be used.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Deprecate and supersede one or more Who's On First IDs.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n\t %s [options] wof-id-(N) wof-id-(N)\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "For example:\n")
fmt.Fprintf(os.Stderr, "\t%s -s . -i 1234\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s -reader-uri fs:///usr/local/data/whosonfirst-data-admin-ca/data -id 1234 -id 5678\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Valid options are:\n")
flag.PrintDefaults()
}
flag.Parse()
if *reader_uri == "" || *writer_uri == "" {
var path string
if *source == "" {
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to determine current working directory, %v", err)
}
path = cwd
} else {
abs_source, err := filepath.Abs(*source)
if err != nil {
log.Fatalf("Failed to derive absolute path for '%s', %v", *source, err)
}
path = abs_source
}
abs_path := fmt.Sprintf("fs://%s/data", path)
if *reader_uri == "" {
*reader_uri = abs_path
}
if *writer_uri == "" {
*writer_uri = abs_path
}
}
if *id != "" {
err := ids.Set(*id)
if err != nil {
log.Fatalf("Failed to assign '%s' (-id) flag, %v", *id, err)
}
}
ctx := context.Background()
ex, err := export.NewExporter(ctx, *exporter_uri)
if err != nil {
log.Fatalf("Failed create exporter for '%s', %v", *exporter_uri, err)
}
r, err := reader.NewReader(ctx, *reader_uri)
if err != nil {
log.Fatalf("Failed to create reader for '%s', %v", *reader_uri, err)
}
wr, err := writer.NewWriter(ctx, *writer_uri)
if err != nil {
log.Fatalf("Failed to create writer for '%s', %v", *writer_uri, err)
}
props := make([]multi.KeyValueFlag, 0)
for _, p := range str_properties {
props = append(props, p)
}
for _, p := range int_properties {
props = append(props, p)
}
for _, p := range float_properties {
props = append(props, p)
}
for _, old_id := range ids {
new_id, err := replaceId(ctx, r, wr, ex, old_id, props...)
if err != nil {
log.Fatalf("Failed to export record for '%d', %v", old_id, err)
}
log.Printf("%d replaced by %d\n", old_id, new_id)
}
}
func replaceId(ctx context.Context, r reader.Reader, wr writer.Writer, ex export.Exporter, old_id int64, props ...multi.KeyValueFlag) (int64, error) {
old_body, err := wof_reader.LoadBytes(ctx, r, old_id)
if err != nil {
return -1, err
}
// Create the new record
new_body := old_body
new_body, err = sjson.DeleteBytes(new_body, "properties.wof:id")
if err != nil {
return -1, err
}
new_body, err = ex.Export(ctx, new_body)
if err != nil {
return -1, err
}
id_rsp := gjson.GetBytes(new_body, "properties.wof:id")
if !id_rsp.Exists() {
return -1, fmt.Errorf("failed to derive new properties.wof:id property for record superseding '%d'", old_id)
}
new_id := id_rsp.Int()
// Update the new record
new_body, err = sjson.SetBytes(new_body, "properties.wof:supsersedes", []int64{old_id})
if err != nil {
return -1, err
}
for _, p := range props {
path := p.Key()
new_value := p.Value()
new_body, err = sjson.SetBytes(new_body, path, new_value)
if err != nil {
return -1, err
}
}
// Update the old record
now := time.Now()
deprecated := now.Format("2006-01-02")
old_body, err = sjson.SetBytes(old_body, "properties.edtf:deprecated", deprecated)
if err != nil {
return -1, err
}
old_body, err = sjson.SetBytes(old_body, "properties.mz:is_current", 0)
if err != nil {
return -1, err
}
old_body, err = sjson.SetBytes(old_body, "properties.wof:supserseded_by", []int64{new_id})
if err != nil {
return -1, err
}
// Write records
old_body, err = ex.Export(ctx, old_body)
if err != nil {
return -1, err
}
new_body, err = ex.Export(ctx, new_body)
if err != nil {
return -1, err
}
_, err = wof_writer.WriteBytes(ctx, wr, old_body)
if err != nil {
return -1, err
}
_, err = wof_writer.WriteBytes(ctx, wr, new_body)
if err != nil {
return -1, err
}
return new_id, nil
}