| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "go/build" | |
| 9 "io/ioutil" | |
| 10 "os" | |
| 11 "path/filepath" | |
| 12 "strings" | |
| 13 | |
| 14 "github.com/golang/protobuf/proto" | |
| 15 "github.com/luci/luci-go/common/errors" | |
| 16 log "github.com/luci/luci-go/common/logging" | |
| 17 "golang.org/x/net/context" | |
| 18 ) | |
| 19 | |
| 20 func unmarshalTextProtobuf(path string, msg proto.Message) error { | |
| 21 data, err := ioutil.ReadFile(path) | |
| 22 switch { | |
| 23 case 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]"). | |
| 26 D("type", msg).D("path", path).Err() | |
| 27 } | |
| 28 return nil | |
| 29 | |
| 30 case isNotExist(err): | |
| 31 // Forward this so it can be tested. | |
| 32 return err | |
| 33 | |
| 34 default: | |
| 35 return errors.Annotate(err).Reason("failed to read data from [%(
path)s]").D("path", path).Err() | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 func unmarshalTextProtobufDir(base string, fis []os.FileInfo, msg proto.Message,
cb func(name string) error) error { | |
| 40 for _, fi := range fis { | |
| 41 name := fi.Name() | |
| 42 if isHidden(name) { | |
| 43 continue | |
| 44 } | |
| 45 | |
| 46 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() | |
| 48 } | |
| 49 if err := cb(name); err != nil { | |
| 50 return errors.Annotate(err).Reason("failed to process fi
le [%(name)s]").D("name", name).Err() | |
| 51 } | |
| 52 } | |
| 53 return nil | |
| 54 } | |
| 55 | |
| 56 func logError(c context.Context, err error, f string, args ...interface{}) { | |
| 57 log.WithError(err).Errorf(c, f, args...) | |
| 58 if log.IsLogging(c, log.Debug) { | |
| 59 log.Debugf(c, "Error stack:\n%s", strings.Join(errors.RenderStac
k(err).ToLines(), "\n")) | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 func isNotExist(err error) bool { | |
| 64 return os.IsNotExist(errors.Unwrap(err)) | |
| 65 } | |
| 66 | |
| 67 func splitTitlePath(s string) (title, string) { | |
| 68 switch v := strings.SplitN(s, "/", 2); len(v) { | |
| 69 case 1: | |
| 70 return title(v[0]), "" | |
| 71 default: | |
| 72 return title(v[0]), v[1] | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 func joinPath(titles ...title) string { | |
| 77 comps := make([]string, len(titles)) | |
| 78 for i, t := range titles { | |
| 79 comps[i] = string(t) | |
| 80 } | |
| 81 return strings.Join(comps, "/") | |
| 82 } | |
| 83 | |
| 84 func splitSourcePath(v string) (group, source title) { | |
| 85 var tail string | |
| 86 group, tail = splitTitlePath(v) | |
| 87 source = title(tail) | |
| 88 return | |
| 89 } | |
| 90 | |
| 91 func splitComponentPath(v string) (deployment, component title) { | |
| 92 var tail string | |
| 93 deployment, tail = splitTitlePath(v) | |
| 94 if tail != "" { | |
| 95 component = title(tail) | |
| 96 } | |
| 97 return | |
| 98 } | |
| 99 | |
| 100 func splitGoPackage(pkg string) []string { | |
| 101 // Check intermediate paths to make sure there isn't a deployment | |
| 102 // conflict. | |
| 103 var ( | |
| 104 parts []string | |
| 105 lastIdx = 0 | |
| 106 ) | |
| 107 for { | |
| 108 idx := strings.IndexRune(pkg[lastIdx:], '/') | |
| 109 if idx < 0 { | |
| 110 // Last component, don't check/register. | |
| 111 return append(parts, pkg) | |
| 112 } | |
| 113 parts = append(parts, pkg[:lastIdx+idx]) | |
| 114 lastIdx += idx + len("/") | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 func findGoPackage(pkg string, goPath []string) string { | |
| 119 bctx := build.Default | |
| 120 bctx.GOPATH = strings.Join(goPath, string(os.PathListSeparator)) | |
| 121 p, err := bctx.Import(pkg, "", build.FindOnly) | |
| 122 if err != nil { | |
| 123 return "" | |
| 124 } | |
| 125 return p.Dir | |
| 126 } | |
| OLD | NEW |