| 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" |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 168 |
| 169 var index logpb.LogIndex | 169 var index logpb.LogIndex |
| 170 if err := unmarshalAndDump(c, os.Stdout, data, &index); err != nil { | 170 if err := unmarshalAndDump(c, os.Stdout, data, &index); err != nil { |
| 171 log.WithError(err).Errorf(c, "Failed to dump index protobuf.") | 171 log.WithError(err).Errorf(c, "Failed to dump index protobuf.") |
| 172 return 1 | 172 return 1 |
| 173 } | 173 } |
| 174 return 0 | 174 return 0 |
| 175 } | 175 } |
| 176 | 176 |
| 177 func unmarshalAndDump(c context.Context, out io.Writer, data []byte, msg proto.M
essage) error { | 177 func unmarshalAndDump(c context.Context, out io.Writer, data []byte, msg proto.M
essage) error { |
| 178 » if err := proto.Unmarshal(data, msg); err != nil { | 178 » if data != nil { |
| 179 » » log.WithError(err).Errorf(c, "Failed to unmarshal protobuf.") | 179 » » if err := proto.Unmarshal(data, msg); err != nil { |
| 180 » » return err | 180 » » » log.WithError(err).Errorf(c, "Failed to unmarshal protob
uf.") |
| 181 » » » return err |
| 182 » » } |
| 181 } | 183 } |
| 184 |
| 182 if err := proto.MarshalText(out, msg); err != nil { | 185 if err := proto.MarshalText(out, msg); err != nil { |
| 183 log.WithError(err).Errorf(c, "Failed to dump protobuf to output.
") | 186 log.WithError(err).Errorf(c, "Failed to dump protobuf to output.
") |
| 184 return err | 187 return err |
| 185 } | 188 } |
| 186 return nil | 189 return nil |
| 187 } | 190 } |
| 188 | 191 |
| 189 //////////////////////////////////////////////////////////////////////////////// | 192 //////////////////////////////////////////////////////////////////////////////// |
| 190 // Subcommand: dump-stream | 193 // Subcommand: dump-stream |
| 191 //////////////////////////////////////////////////////////////////////////////// | 194 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 if err != nil { | 343 if err != nil { |
| 341 log.WithError(err).Errorf(c, "Failed to create storage client.") | 344 log.WithError(err).Errorf(c, "Failed to create storage client.") |
| 342 return 1 | 345 return 1 |
| 343 } | 346 } |
| 344 defer stClient.Close() | 347 defer stClient.Close() |
| 345 | 348 |
| 346 var innerErr error | 349 var innerErr error |
| 347 err = stClient.Get(storage.GetRequest{ | 350 err = stClient.Get(storage.GetRequest{ |
| 348 Index: types.MessageIndex(cmd.index), | 351 Index: types.MessageIndex(cmd.index), |
| 349 Limit: cmd.limit, | 352 Limit: cmd.limit, |
| 350 » }, func(idx types.MessageIndex, data []byte) bool { | 353 » }, func(e *storage.Entry) bool { |
| 354 » » le, err := e.GetLogEntry() |
| 355 » » if err != nil { |
| 356 » » » log.WithError(err).Errorf(c, "Failed to unmarshal log en
try.") |
| 357 » » » return false |
| 358 » » } |
| 359 |
| 351 log.Fields{ | 360 log.Fields{ |
| 352 » » » "index": idx, | 361 » » » "index": le.StreamIndex, |
| 353 }.Infof(c, "Fetched log entry.") | 362 }.Infof(c, "Fetched log entry.") |
| 354 | 363 |
| 355 » » var log logpb.LogEntry | 364 » » if innerErr = unmarshalAndDump(c, os.Stdout, nil, le); innerErr
!= nil { |
| 356 » » if innerErr = unmarshalAndDump(c, os.Stdout, data, &log); innerE
rr != nil { | |
| 357 return false | 365 return false |
| 358 } | 366 } |
| 359 return true | 367 return true |
| 360 }) | 368 }) |
| 361 switch { | 369 switch { |
| 362 case innerErr != nil: | 370 case innerErr != nil: |
| 363 log.WithError(innerErr).Errorf(c, "Failed to process fetched log
entries.") | 371 log.WithError(innerErr).Errorf(c, "Failed to process fetched log
entries.") |
| 364 return 1 | 372 return 1 |
| 365 | 373 |
| 366 case err != nil: | 374 case err != nil: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 IndexURL: cmd.indexPath, | 427 IndexURL: cmd.indexPath, |
| 420 StreamURL: cmd.streamPath, | 428 StreamURL: cmd.streamPath, |
| 421 Client: client, | 429 Client: client, |
| 422 }) | 430 }) |
| 423 if err != nil { | 431 if err != nil { |
| 424 log.WithError(err).Errorf(c, "Failed to create storage client.") | 432 log.WithError(err).Errorf(c, "Failed to create storage client.") |
| 425 return 1 | 433 return 1 |
| 426 } | 434 } |
| 427 defer stClient.Close() | 435 defer stClient.Close() |
| 428 | 436 |
| 429 » data, idx, err := stClient.Tail("", "") | 437 » e, err := stClient.Tail("", "") |
| 430 if err != nil { | 438 if err != nil { |
| 431 log.WithError(err).Errorf(c, "Failed to Tail log entries.") | 439 log.WithError(err).Errorf(c, "Failed to Tail log entries.") |
| 432 return 1 | 440 return 1 |
| 433 } | 441 } |
| 434 | 442 |
| 435 » if data == nil { | 443 » if e == nil { |
| 436 log.Infof(c, "No log data to tail.") | 444 log.Infof(c, "No log data to tail.") |
| 437 return 0 | 445 return 0 |
| 438 } | 446 } |
| 439 | 447 |
| 448 le, err := e.GetLogEntry() |
| 449 if err != nil { |
| 450 log.WithError(err).Errorf(c, "Failed to unmarshal log entry.") |
| 451 return 1 |
| 452 } |
| 453 |
| 440 log.Fields{ | 454 log.Fields{ |
| 441 » » "index": idx, | 455 » » "index": le.StreamIndex, |
| 442 » » "size": len(data), | 456 » » "size": len(e.D), |
| 443 }.Debugf(c, "Dumping tail entry.") | 457 }.Debugf(c, "Dumping tail entry.") |
| 444 » var entry logpb.LogEntry | 458 » if err := unmarshalAndDump(c, os.Stdout, nil, le); err != nil { |
| 445 » if err := unmarshalAndDump(c, os.Stdout, data, &entry); err != nil { | |
| 446 log.WithError(err).Errorf(c, "Failed to dump tail entry.") | 459 log.WithError(err).Errorf(c, "Failed to dump tail entry.") |
| 447 return 1 | 460 return 1 |
| 448 } | 461 } |
| 449 return 0 | 462 return 0 |
| 450 } | 463 } |
| OLD | NEW |