OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "go/build" | 8 "go/build" |
9 "io/ioutil" | 9 "io/ioutil" |
10 "os" | 10 "os" |
11 "path/filepath" | 11 "path/filepath" |
12 "strings" | 12 "strings" |
13 | 13 |
14 "github.com/golang/protobuf/proto" | 14 "github.com/golang/protobuf/proto" |
15 "github.com/luci/luci-go/common/errors" | 15 "github.com/luci/luci-go/common/errors" |
16 log "github.com/luci/luci-go/common/logging" | 16 log "github.com/luci/luci-go/common/logging" |
17 "golang.org/x/net/context" | 17 "golang.org/x/net/context" |
18 ) | 18 ) |
19 | 19 |
20 func unmarshalTextProtobuf(path string, msg proto.Message) error { | 20 func unmarshalTextProtobuf(path string, msg proto.Message) error { |
21 data, err := ioutil.ReadFile(path) | 21 data, err := ioutil.ReadFile(path) |
22 switch { | 22 switch { |
23 case err == nil: | 23 case err == nil: |
24 if err := proto.UnmarshalText(string(data), msg); err != nil { | 24 if err := proto.UnmarshalText(string(data), msg); err != nil { |
25 » » » return errors.Annotate(err).Reason("failed to unmarshal
%(type)T from [%(path)s]"). | 25 » » » return errors.Annotate(err, "failed to unmarshal %T from
[%s]", msg, path).Err() |
26 » » » » D("type", msg).D("path", path).Err() | |
27 } | 26 } |
28 return nil | 27 return nil |
29 | 28 |
30 case isNotExist(err): | 29 case isNotExist(err): |
31 // Forward this so it can be tested. | 30 // Forward this so it can be tested. |
32 return err | 31 return err |
33 | 32 |
34 default: | 33 default: |
35 » » return errors.Annotate(err).Reason("failed to read data from [%(
path)s]").D("path", path).Err() | 34 » » return errors.Annotate(err, "failed to read data from [%s]", pat
h).Err() |
36 } | 35 } |
37 } | 36 } |
38 | 37 |
39 func unmarshalTextProtobufDir(base string, fis []os.FileInfo, msg proto.Message,
cb func(name string) error) error { | 38 func unmarshalTextProtobufDir(base string, fis []os.FileInfo, msg proto.Message,
cb func(name string) error) error { |
40 for _, fi := range fis { | 39 for _, fi := range fis { |
41 name := fi.Name() | 40 name := fi.Name() |
42 if isHidden(name) { | 41 if isHidden(name) { |
43 continue | 42 continue |
44 } | 43 } |
45 | 44 |
46 if err := unmarshalTextProtobuf(filepath.Join(base, name), msg);
err != nil { | 45 if err := unmarshalTextProtobuf(filepath.Join(base, name), msg);
err != nil { |
47 » » » return errors.Annotate(err).Reason("failed to unmarshal
file [%(name)s]").D("name", name).Err() | 46 » » » return errors.Annotate(err, "failed to unmarshal file [%
s]", name).Err() |
48 } | 47 } |
49 if err := cb(name); err != nil { | 48 if err := cb(name); err != nil { |
50 » » » return errors.Annotate(err).Reason("failed to process fi
le [%(name)s]").D("name", name).Err() | 49 » » » return errors.Annotate(err, "failed to process file [%s]
", name).Err() |
51 } | 50 } |
52 } | 51 } |
53 return nil | 52 return nil |
54 } | 53 } |
55 | 54 |
56 func logError(c context.Context, err error, f string, args ...interface{}) { | 55 func logError(c context.Context, err error, f string, args ...interface{}) { |
57 log.WithError(err).Errorf(c, f, args...) | 56 log.WithError(err).Errorf(c, f, args...) |
58 if log.IsLogging(c, log.Debug) { | 57 if log.IsLogging(c, log.Debug) { |
59 » » log.Debugf(c, "Error stack:\n%s", strings.Join(errors.RenderStac
k(err).ToLines(), "\n")) | 58 » » log.Debugf(c, "Error stack:\n%s", strings.Join(errors.RenderStac
k(err), "\n")) |
60 } | 59 } |
61 } | 60 } |
62 | 61 |
63 func isNotExist(err error) bool { | 62 func isNotExist(err error) bool { |
64 return os.IsNotExist(errors.Unwrap(err)) | 63 return os.IsNotExist(errors.Unwrap(err)) |
65 } | 64 } |
66 | 65 |
67 func splitTitlePath(s string) (title, string) { | 66 func splitTitlePath(s string) (title, string) { |
68 switch v := strings.SplitN(s, "/", 2); len(v) { | 67 switch v := strings.SplitN(s, "/", 2); len(v) { |
69 case 1: | 68 case 1: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 116 |
118 func findGoPackage(pkg string, goPath []string) string { | 117 func findGoPackage(pkg string, goPath []string) string { |
119 bctx := build.Default | 118 bctx := build.Default |
120 bctx.GOPATH = strings.Join(goPath, string(os.PathListSeparator)) | 119 bctx.GOPATH = strings.Join(goPath, string(os.PathListSeparator)) |
121 p, err := bctx.Import(pkg, "", build.FindOnly) | 120 p, err := bctx.Import(pkg, "", build.FindOnly) |
122 if err != nil { | 121 if err != nil { |
123 return "" | 122 return "" |
124 } | 123 } |
125 return p.Dir | 124 return p.Dir |
126 } | 125 } |
OLD | NEW |