OLD | NEW |
---|---|
1 // named is a utility for dealing with named fiddles. | 1 // named is a utility for dealing with named fiddles. |
2 package named | 2 package named |
3 | 3 |
4 import ( | 4 import ( |
5 "errors" | |
5 "fmt" | 6 "fmt" |
6 "path/filepath" | 7 "path/filepath" |
7 "regexp" | 8 "regexp" |
8 "strings" | 9 "strings" |
9 "sync" | 10 "sync" |
10 | 11 |
11 "github.com/skia-dev/glog" | 12 "github.com/skia-dev/glog" |
12 "go.skia.org/infra/fiddle/go/store" | 13 "go.skia.org/infra/fiddle/go/store" |
13 ) | 14 ) |
14 | 15 |
15 var ( | 16 var ( |
17 DuplicateNameErr = errors.New("Name already exists.") | |
18 | |
16 // fiddleHashRe is used to validate fiddle hashes. | 19 // fiddleHashRe is used to validate fiddle hashes. |
17 fiddleHashRe = regexp.MustCompile("^[0-9a-zA-Z]{32}$") | 20 fiddleHashRe = regexp.MustCompile("^[0-9a-zA-Z]{32}$") |
18 | 21 |
19 // fiddlNameRe is used to validate fiddle names. | 22 // fiddlNameRe is used to validate fiddle names. |
20 fiddleNameRe = regexp.MustCompile("^[0-9a-zA-Z_]+$") | 23 fiddleNameRe = regexp.MustCompile("^[0-9a-zA-Z_]+$") |
21 | 24 |
22 // trailingToMedia maps the end of each image URL to the store.Media typ e | 25 // trailingToMedia maps the end of each image URL to the store.Media typ e |
23 // that it corresponds to. | 26 // that it corresponds to. |
24 trailingToMedia = map[string]store.Media{ | 27 trailingToMedia = map[string]store.Media{ |
25 "_raster.png": store.CPU, | 28 "_raster.png": store.CPU, |
(...skipping 24 matching lines...) Expand all Loading... | |
50 // New creates a new Named. | 53 // New creates a new Named. |
51 func New(st NameStore) *Named { | 54 func New(st NameStore) *Named { |
52 return &Named{ | 55 return &Named{ |
53 cache: map[string]string{}, | 56 cache: map[string]string{}, |
54 st: st, | 57 st: st, |
55 } | 58 } |
56 } | 59 } |
57 | 60 |
58 // Add a named fiddle. | 61 // Add a named fiddle. |
59 // | 62 // |
60 // name - The name of the fidde, w/o the @ prefix. | 63 // name - The name of the fidde, w/o the @ prefix. |
61 // hash - The fiddle hash. | 64 // hash - The fiddle hash. |
62 // user - The email of the user that created the name. | 65 // user - The email of the user that created the name. |
63 func (n *Named) Add(name, hash, user string) error { | 66 // overwrite - True if the write should proceed if the name already exists. |
67 func (n *Named) Add(name, hash, user string, overwrite bool) error { | |
64 if !fiddleNameRe.MatchString(name) { | 68 if !fiddleNameRe.MatchString(name) { |
65 return fmt.Errorf("Not a valid fiddle name %q", name) | 69 return fmt.Errorf("Not a valid fiddle name %q", name) |
66 } | 70 } |
67 if !fiddleHashRe.MatchString(hash) { | 71 if !fiddleHashRe.MatchString(hash) { |
68 return fmt.Errorf("Not a valid fiddle hash %q", hash) | 72 return fmt.Errorf("Not a valid fiddle hash %q", hash) |
69 } | 73 } |
70 oldHash, err := n.DereferenceID("@" + name) | 74 oldHash, err := n.DereferenceID("@" + name) |
71 if err == nil { | 75 if err == nil { |
rmistry
2016/04/25 12:59:18
I had to look at this a few times to see what it w
| |
76 // This name exists already. | |
77 if !overwrite { | |
78 return DuplicateNameErr | |
79 } | |
72 if oldHash == hash { | 80 if oldHash == hash { |
73 // Don't bother writing if the hash is already correct. | 81 // Don't bother writing if the hash is already correct. |
74 return nil | 82 return nil |
75 } | 83 } |
76 glog.Infof("Named Fiddle Changed: %s %s -> %s by %s", name, oldH ash, hash, user) | 84 glog.Infof("Named Fiddle Changed: %s %s -> %s by %s", name, oldH ash, hash, user) |
77 } else { | 85 } else { |
78 glog.Infof("Named Fiddle Created: %s %s by %s", name, hash, user ) | 86 glog.Infof("Named Fiddle Created: %s %s by %s", name, hash, user ) |
79 } | 87 } |
80 if err := n.st.WriteName(name, hash, user); err != nil { | 88 if err := n.st.WriteName(name, hash, user); err != nil { |
81 return fmt.Errorf("Failed to write name: %s", err) | 89 return fmt.Errorf("Failed to write name: %s", err) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 id = strings.Join(parts[:len(parts)-1], "_") | 158 id = strings.Join(parts[:len(parts)-1], "_") |
151 } | 159 } |
152 media, ok := trailingToMedia[trailing] | 160 media, ok := trailingToMedia[trailing] |
153 if !ok { | 161 if !ok { |
154 return "", store.UNKNOWN, fmt.Errorf("Unknown media: %q", traili ng) | 162 return "", store.UNKNOWN, fmt.Errorf("Unknown media: %q", traili ng) |
155 } | 163 } |
156 // We are left with just the name or fiddle hash, dereference that. | 164 // We are left with just the name or fiddle hash, dereference that. |
157 fiddleHash, err := n.DereferenceID(id) | 165 fiddleHash, err := n.DereferenceID(id) |
158 return fiddleHash, media, err | 166 return fiddleHash, media, err |
159 } | 167 } |
OLD | NEW |