| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 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 "os" | 8 "os" |
| 9 | 9 |
| 10 "github.com/luci/luci-go/common/errors" | 10 "github.com/luci/luci-go/common/errors" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 subcommands.CommandRunBase | 39 subcommands.CommandRunBase |
| 40 | 40 |
| 41 path string // The stream input path. | 41 path string // The stream input path. |
| 42 stream streamConfig // Stream configuration parameters. | 42 stream streamConfig // Stream configuration parameters. |
| 43 } | 43 } |
| 44 | 44 |
| 45 // subcommands.Run | 45 // subcommands.Run |
| 46 func (cmd *streamCommandRun) Run(app subcommands.Application, args []string) int
{ | 46 func (cmd *streamCommandRun) Run(app subcommands.Application, args []string) int
{ |
| 47 a := app.(*application) | 47 a := app.(*application) |
| 48 | 48 |
| 49 » streamFile := (*os.File)(nil) | 49 » var ( |
| 50 » » streamFile *os.File |
| 51 » » defaultStreamName streamproto.StreamNameFlag |
| 52 » ) |
| 50 if cmd.path == "-" { | 53 if cmd.path == "-" { |
| 51 » » cmd.stream.Name = "stdin" | 54 » » defaultStreamName = "stdin" |
| 52 streamFile = os.Stdin | 55 streamFile = os.Stdin |
| 53 } else { | 56 } else { |
| 54 streamName, err := types.MakeStreamName("file:", cmd.path) | 57 streamName, err := types.MakeStreamName("file:", cmd.path) |
| 55 if err != nil { | 58 if err != nil { |
| 56 log.Fields{ | 59 log.Fields{ |
| 57 log.ErrorKey: err, | 60 log.ErrorKey: err, |
| 58 }.Errorf(a, "Failed to generate stream name.") | 61 }.Errorf(a, "Failed to generate stream name.") |
| 59 return runtimeErrorReturnCode | 62 return runtimeErrorReturnCode |
| 60 } | 63 } |
| 61 » » cmd.stream.Name = streamproto.StreamNameFlag(streamName) | 64 » » defaultStreamName = streamproto.StreamNameFlag(streamName) |
| 62 | 65 |
| 63 file, err := os.Open(cmd.path) | 66 file, err := os.Open(cmd.path) |
| 64 if err != nil { | 67 if err != nil { |
| 65 log.Fields{ | 68 log.Fields{ |
| 66 log.ErrorKey: err, | 69 log.ErrorKey: err, |
| 67 "path": cmd.path, | 70 "path": cmd.path, |
| 68 }.Errorf(a, "Failed to open input stream file.") | 71 }.Errorf(a, "Failed to open input stream file.") |
| 69 return runtimeErrorReturnCode | 72 return runtimeErrorReturnCode |
| 70 } | 73 } |
| 71 streamFile = file | 74 streamFile = file |
| 72 } | 75 } |
| 76 if cmd.stream.Name == "" { |
| 77 cmd.stream.Name = defaultStreamName |
| 78 } |
| 73 | 79 |
| 74 // We think everything should work. Configure our Output instance. | 80 // We think everything should work. Configure our Output instance. |
| 75 output, err := a.configOutput() | 81 output, err := a.configOutput() |
| 76 if err != nil { | 82 if err != nil { |
| 77 log.WithError(err).Errorf(a, "Failed to create output instance."
) | 83 log.WithError(err).Errorf(a, "Failed to create output instance."
) |
| 78 return runtimeErrorReturnCode | 84 return runtimeErrorReturnCode |
| 79 } | 85 } |
| 80 defer output.Close() | 86 defer output.Close() |
| 81 | 87 |
| 82 // Instantiate our Processor. | 88 // Instantiate our Processor. |
| 83 err = a.runWithButler(a, output, func(ctx context.Context, b *butler.But
ler) error { | 89 err = a.runWithButler(a, output, func(ctx context.Context, b *butler.But
ler) error { |
| 84 if err := b.AddStream(streamFile, cmd.stream.properties()); err
!= nil { | 90 if err := b.AddStream(streamFile, cmd.stream.properties()); err
!= nil { |
| 85 return errors.Annotate(err).Reason("failed to add stream
").Err() | 91 return errors.Annotate(err).Reason("failed to add stream
").Err() |
| 86 } | 92 } |
| 87 | 93 |
| 88 b.Activate() | 94 b.Activate() |
| 89 return b.Wait() | 95 return b.Wait() |
| 90 }) | 96 }) |
| 91 if err != nil { | 97 if err != nil { |
| 92 logAnnotatedErr(a, err, "Failed to stream file.") | 98 logAnnotatedErr(a, err, "Failed to stream file.") |
| 93 return runtimeErrorReturnCode | 99 return runtimeErrorReturnCode |
| 94 } | 100 } |
| 95 | 101 |
| 96 return 0 | 102 return 0 |
| 97 } | 103 } |
| OLD | NEW |