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 implements a simple CLI tool to load and interact with Google | 5 // Package main implements a simple CLI tool to load and interact with Google |
6 // Storage archived data. | 6 // Storage archived data. |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "bytes" | 10 "bytes" |
11 "flag" | 11 "flag" |
12 "io" | 12 "io" |
13 "io/ioutil" | 13 "io/ioutil" |
14 "math" | 14 "math" |
15 "os" | 15 "os" |
| 16 "strings" |
16 | 17 |
17 "github.com/luci/luci-go/client/authcli" | 18 "github.com/luci/luci-go/client/authcli" |
18 "github.com/luci/luci-go/common/auth" | 19 "github.com/luci/luci-go/common/auth" |
19 "github.com/luci/luci-go/common/cli" | 20 "github.com/luci/luci-go/common/cli" |
20 "github.com/luci/luci-go/common/data/rand/mathrand" | 21 "github.com/luci/luci-go/common/data/rand/mathrand" |
21 "github.com/luci/luci-go/common/data/recordio" | 22 "github.com/luci/luci-go/common/data/recordio" |
22 "github.com/luci/luci-go/common/errors" | 23 "github.com/luci/luci-go/common/errors" |
23 "github.com/luci/luci-go/common/gcloud/gs" | 24 "github.com/luci/luci-go/common/gcloud/gs" |
24 log "github.com/luci/luci-go/common/logging" | 25 log "github.com/luci/luci-go/common/logging" |
25 "github.com/luci/luci-go/common/logging/gologger" | 26 "github.com/luci/luci-go/common/logging/gologger" |
(...skipping 21 matching lines...) Expand all Loading... |
47 | 48 |
48 func getApplication(base subcommands.Application) (*application, context.Context
) { | 49 func getApplication(base subcommands.Application) (*application, context.Context
) { |
49 app := base.(*application) | 50 app := base.(*application) |
50 return app, app.Context(context.Background()) | 51 return app, app.Context(context.Background()) |
51 } | 52 } |
52 | 53 |
53 func (a *application) getGSClient(c context.Context) (gs.Client, error) { | 54 func (a *application) getGSClient(c context.Context) (gs.Client, error) { |
54 authenticator := auth.NewAuthenticator(c, auth.OptionalLogin, a.authOpts
) | 55 authenticator := auth.NewAuthenticator(c, auth.OptionalLogin, a.authOpts
) |
55 transport, err := authenticator.Transport() | 56 transport, err := authenticator.Transport() |
56 if err != nil { | 57 if err != nil { |
57 » » return nil, errors.Annotate(err).Reason("failed to get auth tran
sport").Err() | 58 » » return nil, errors.Annotate(err, "failed to get auth transport")
.Err() |
58 } | 59 } |
59 return gs.NewProdClient(c, transport) | 60 return gs.NewProdClient(c, transport) |
60 } | 61 } |
61 | 62 |
62 func mainImpl(c context.Context, defaultAuthOpts auth.Options, args []string) in
t { | 63 func mainImpl(c context.Context, defaultAuthOpts auth.Options, args []string) in
t { |
63 c = gologger.StdConfig.Use(c) | 64 c = gologger.StdConfig.Use(c) |
64 | 65 |
65 logConfig := log.Config{ | 66 logConfig := log.Config{ |
66 Level: log.Warning, | 67 Level: log.Warning, |
67 } | 68 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // Execute our subcommand. | 111 // Execute our subcommand. |
111 return subcommands.Run(&a, fs.Args()) | 112 return subcommands.Run(&a, fs.Args()) |
112 } | 113 } |
113 | 114 |
114 func main() { | 115 func main() { |
115 mathrand.SeedRandomly() | 116 mathrand.SeedRandomly() |
116 os.Exit(mainImpl(context.Background(), chromeinfra.DefaultAuthOptions(),
os.Args[1:])) | 117 os.Exit(mainImpl(context.Background(), chromeinfra.DefaultAuthOptions(),
os.Args[1:])) |
117 } | 118 } |
118 | 119 |
119 func renderErr(c context.Context, err error) { | 120 func renderErr(c context.Context, err error) { |
120 » rerr := errors.RenderStack(err) | 121 » log.Errorf(c, "Error encountered during operation: %s\n%s", err, |
121 | 122 » » strings.Join(errors.RenderStack(err), "\n")) |
122 » var buf bytes.Buffer | |
123 » rerr.DumpTo(&buf) | |
124 » log.Errorf(c, "Error encountered during operation: %s\n%s", err, buf.Byt
es()) | |
125 } | 123 } |
126 | 124 |
127 //////////////////////////////////////////////////////////////////////////////// | 125 //////////////////////////////////////////////////////////////////////////////// |
128 // Subcommand: dump-index | 126 // Subcommand: dump-index |
129 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
130 | 128 |
131 type cmdRunDumpIndex struct { | 129 type cmdRunDumpIndex struct { |
132 subcommands.CommandRunBase | 130 subcommands.CommandRunBase |
133 | 131 |
134 path string | 132 path string |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 } | 242 } |
245 defer reader.Close() | 243 defer reader.Close() |
246 | 244 |
247 descFrame := true | 245 descFrame := true |
248 err = dumpRecordIO(c, reader, func(c context.Context, d []byte) error { | 246 err = dumpRecordIO(c, reader, func(c context.Context, d []byte) error { |
249 if descFrame { | 247 if descFrame { |
250 descFrame = false | 248 descFrame = false |
251 | 249 |
252 var desc logpb.LogStreamDescriptor | 250 var desc logpb.LogStreamDescriptor |
253 if err := unmarshalAndDump(c, os.Stdout, d, &desc); err
!= nil { | 251 if err := unmarshalAndDump(c, os.Stdout, d, &desc); err
!= nil { |
254 » » » » return errors.Annotate(err).Reason("failed to du
mp log descriptor").Err() | 252 » » » » return errors.Annotate(err, "failed to dump log
descriptor").Err() |
255 } | 253 } |
256 return nil | 254 return nil |
257 } | 255 } |
258 | 256 |
259 var entry logpb.LogEntry | 257 var entry logpb.LogEntry |
260 if err := unmarshalAndDump(c, os.Stdout, d, &entry); err != nil
{ | 258 if err := unmarshalAndDump(c, os.Stdout, d, &entry); err != nil
{ |
261 » » » return errors.Annotate(err).Reason("failed to dump log e
ntry").Err() | 259 » » » return errors.Annotate(err, "failed to dump log entry").
Err() |
262 } | 260 } |
263 return nil | 261 return nil |
264 }) | 262 }) |
265 if err != nil { | 263 if err != nil { |
266 renderErr(c, err) | 264 renderErr(c, err) |
267 return 1 | 265 return 1 |
268 } | 266 } |
269 | 267 |
270 return 0 | 268 return 0 |
271 } | 269 } |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 switch err { | 455 switch err { |
458 case nil: | 456 case nil: |
459 break | 457 break |
460 | 458 |
461 case io.EOF: | 459 case io.EOF: |
462 log.Debugf(c, "Encountered EOF.") | 460 log.Debugf(c, "Encountered EOF.") |
463 eof = true | 461 eof = true |
464 break | 462 break |
465 | 463 |
466 default: | 464 default: |
467 » » » return errors.Annotate(err).Reason("Encountered error re
ading log stream."). | 465 » » » return errors.Annotate(err, "Encountered error reading l
og stream."). |
468 » » » » D("frameIndex", frameIndex).Err() | 466 » » » » InternalReason("frameIndex(%d)", frameIndex).Err
() |
469 } | 467 } |
470 | 468 |
471 if frameSize > 0 { | 469 if frameSize > 0 { |
472 buf.Reset() | 470 buf.Reset() |
473 buf.Grow(int(frameSize)) | 471 buf.Grow(int(frameSize)) |
474 | 472 |
475 if _, err := buf.ReadFrom(r); err != nil { | 473 if _, err := buf.ReadFrom(r); err != nil { |
476 » » » » return errors.Annotate(err).Reason("Failed to bu
ffer frame."). | 474 » » » » return errors.Annotate(err, "Failed to buffer fr
ame."). |
477 » » » » » D("frameIndex", frameIndex).Err() | 475 » » » » » InternalReason("frameIndex(%d)", frameIn
dex).Err() |
478 } | 476 } |
479 | 477 |
480 if err := cb(c, buf.Bytes()); err != nil { | 478 if err := cb(c, buf.Bytes()); err != nil { |
481 return err | 479 return err |
482 } | 480 } |
483 } | 481 } |
484 | 482 |
485 log.Fields{ | 483 log.Fields{ |
486 "index": frameIndex, | 484 "index": frameIndex, |
487 "size": buf.Len(), | 485 "size": buf.Len(), |
488 }.Debugf(c, "Read frame.") | 486 }.Debugf(c, "Read frame.") |
489 frameIndex++ | 487 frameIndex++ |
490 } | 488 } |
491 return nil | 489 return nil |
492 } | 490 } |
OLD | NEW |