Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 20 | "errors" |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 21 | "fmt" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 22 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 23 | "github.com/opencord/cordctl/format" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | DEFAULT_TRANSFER_FORMAT = "table{{ .Status }}\t{{ .Checksum }}\t{{ .Chunks }}\t{{ .Bytes }}" |
| 29 | ) |
| 30 | |
| 31 | type TransferOutput struct { |
| 32 | Status string `json:"status"` |
| 33 | Checksum string `json:"checksum"` |
| 34 | Chunks int `json:"chunks"` |
| 35 | Bytes int `json:"bytes"` |
| 36 | } |
| 37 | |
| 38 | type TransferUpload struct { |
| 39 | OutputOptions |
| 40 | ChunkSize int `short:"h" long:"chunksize" default:"65536" description:"Host and port"` |
| 41 | Args struct { |
| 42 | LocalFileName string |
| 43 | URI string |
| 44 | } `positional-args:"yes" required:"yes"` |
| 45 | } |
| 46 | |
| 47 | type TransferDownload struct { |
| 48 | OutputOptions |
| 49 | Args struct { |
| 50 | URI string |
| 51 | LocalFileName string |
| 52 | } `positional-args:"yes" required:"yes"` |
| 53 | } |
| 54 | |
| 55 | type TransferOpts struct { |
| 56 | Upload TransferUpload `command:"upload"` |
| 57 | Download TransferDownload `command:"download"` |
| 58 | } |
| 59 | |
| 60 | var transferOpts = TransferOpts{} |
| 61 | |
| 62 | func RegisterTransferCommands(parser *flags.Parser) { |
| 63 | parser.AddCommand("transfer", "file transfer commands", "Commands to transfer files to and from XOS", &transferOpts) |
| 64 | } |
| 65 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 66 | /* Command processors */ |
| 67 | |
| 68 | func (options *TransferUpload) Execute(args []string) error { |
| 69 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 70 | conn, descriptor, err := InitReflectionClient() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | defer conn.Close() |
| 75 | |
| 76 | local_name := options.Args.LocalFileName |
| 77 | uri := options.Args.URI |
| 78 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 79 | if IsFileUri(local_name) { |
| 80 | return errors.New("local_name argument should not be a uri") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 83 | if !IsFileUri(uri) { |
| 84 | return errors.New("uri argument should be a file:// uri") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 87 | h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, options.ChunkSize) |
| 88 | |
| 89 | if upload_result.GetFieldByName("checksum").(string) != h.GetChecksum() { |
| 90 | return fmt.Errorf("Checksum mismatch, expected=%s, received=%s", |
| 91 | h.GetChecksum(), |
| 92 | upload_result.GetFieldByName("checksum").(string)) |
| 93 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 94 | |
| 95 | outputFormat := CharReplacer.Replace(options.Format) |
| 96 | if outputFormat == "" { |
| 97 | outputFormat = DEFAULT_TRANSFER_FORMAT |
| 98 | } |
| 99 | if options.Quiet { |
| 100 | outputFormat = "{{.Status}}" |
| 101 | } |
| 102 | |
| 103 | data := make([]TransferOutput, 1) |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 104 | data[0].Checksum = upload_result.GetFieldByName("checksum").(string) |
| 105 | data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32)) |
| 106 | data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32)) |
| 107 | data[0].Status = GetEnumValue(upload_result, "status") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 108 | |
| 109 | result := CommandResult{ |
| 110 | Format: format.Format(outputFormat), |
| 111 | OutputAs: toOutputType(options.OutputAs), |
| 112 | Data: data, |
| 113 | } |
| 114 | |
| 115 | GenerateOutput(&result) |
| 116 | |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func IsFileUri(s string) bool { |
| 121 | return strings.HasPrefix(s, "file://") |
| 122 | } |
| 123 | |
| 124 | func (options *TransferDownload) Execute(args []string) error { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 125 | conn, descriptor, err := InitReflectionClient() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | defer conn.Close() |
| 130 | |
| 131 | local_name := options.Args.LocalFileName |
| 132 | uri := options.Args.URI |
| 133 | |
| 134 | if IsFileUri(local_name) { |
| 135 | return errors.New("local_name argument should not be a uri") |
| 136 | } |
| 137 | |
| 138 | if !IsFileUri(uri) { |
| 139 | return errors.New("uri argument should be a file:// uri") |
| 140 | } |
| 141 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 142 | h, err := DownloadFile(conn, descriptor, uri, local_name) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | |
| 147 | outputFormat := CharReplacer.Replace(options.Format) |
| 148 | if outputFormat == "" { |
| 149 | outputFormat = DEFAULT_TRANSFER_FORMAT |
| 150 | } |
| 151 | if options.Quiet { |
| 152 | outputFormat = "{{.Status}}" |
| 153 | } |
| 154 | |
| 155 | data := make([]TransferOutput, 1) |
| 156 | data[0].Chunks = h.chunks |
| 157 | data[0].Bytes = h.bytes |
| 158 | data[0].Status = h.status |
| 159 | |
| 160 | result := CommandResult{ |
| 161 | Format: format.Format(outputFormat), |
| 162 | OutputAs: toOutputType(options.OutputAs), |
| 163 | Data: data, |
| 164 | } |
| 165 | |
| 166 | GenerateOutput(&result) |
| 167 | |
| 168 | return nil |
| 169 | } |