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 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 21 | corderrors "github.com/opencord/cordctl/error" |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | DEFAULT_TRANSFER_FORMAT = "table{{ .Status }}\t{{ .Checksum }}\t{{ .Chunks }}\t{{ .Bytes }}" |
| 27 | ) |
| 28 | |
| 29 | type TransferOutput struct { |
| 30 | Status string `json:"status"` |
| 31 | Checksum string `json:"checksum"` |
| 32 | Chunks int `json:"chunks"` |
| 33 | Bytes int `json:"bytes"` |
| 34 | } |
| 35 | |
| 36 | type TransferUpload struct { |
| 37 | OutputOptions |
| 38 | ChunkSize int `short:"h" long:"chunksize" default:"65536" description:"Host and port"` |
| 39 | Args struct { |
| 40 | LocalFileName string |
| 41 | URI string |
| 42 | } `positional-args:"yes" required:"yes"` |
| 43 | } |
| 44 | |
| 45 | type TransferDownload struct { |
| 46 | OutputOptions |
| 47 | Args struct { |
| 48 | URI string |
| 49 | LocalFileName string |
| 50 | } `positional-args:"yes" required:"yes"` |
| 51 | } |
| 52 | |
| 53 | type TransferOpts struct { |
| 54 | Upload TransferUpload `command:"upload"` |
| 55 | Download TransferDownload `command:"download"` |
| 56 | } |
| 57 | |
| 58 | var transferOpts = TransferOpts{} |
| 59 | |
| 60 | func RegisterTransferCommands(parser *flags.Parser) { |
| 61 | parser.AddCommand("transfer", "file transfer commands", "Commands to transfer files to and from XOS", &transferOpts) |
| 62 | } |
| 63 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 64 | /* Command processors */ |
| 65 | |
| 66 | func (options *TransferUpload) Execute(args []string) error { |
| 67 | |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 68 | conn, descriptor, err := InitClient(INIT_DEFAULT) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | defer conn.Close() |
| 73 | |
| 74 | local_name := options.Args.LocalFileName |
| 75 | uri := options.Args.URI |
| 76 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 77 | if IsFileUri(local_name) { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 78 | return corderrors.NewInvalidInputError("local_name argument should not be a uri") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 81 | if !IsFileUri(uri) { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 82 | return corderrors.NewInvalidInputError("uri argument should be a file:// uri") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 85 | h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, options.ChunkSize) |
Scott Baker | 55694ca | 2019-06-13 14:52:28 -0700 | [diff] [blame] | 86 | if err != nil { |
| 87 | return err |
| 88 | } |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 89 | |
| 90 | if upload_result.GetFieldByName("checksum").(string) != h.GetChecksum() { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 91 | return corderrors.WithStackTrace(&corderrors.ChecksumMismatchError{ |
| 92 | Expected: h.GetChecksum(), |
| 93 | Actual: upload_result.GetFieldByName("checksum").(string)}) |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 94 | } |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 95 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 96 | data := make([]TransferOutput, 1) |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 97 | data[0].Checksum = upload_result.GetFieldByName("checksum").(string) |
| 98 | data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32)) |
| 99 | data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32)) |
| 100 | data[0].Status = GetEnumValue(upload_result, "status") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 101 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 102 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_TRANSFER_FORMAT, "{{.Status}}", data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 103 | |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | func IsFileUri(s string) bool { |
| 108 | return strings.HasPrefix(s, "file://") |
| 109 | } |
| 110 | |
| 111 | func (options *TransferDownload) Execute(args []string) error { |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 112 | conn, descriptor, err := InitClient(INIT_DEFAULT) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | defer conn.Close() |
| 117 | |
| 118 | local_name := options.Args.LocalFileName |
| 119 | uri := options.Args.URI |
| 120 | |
| 121 | if IsFileUri(local_name) { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 122 | return corderrors.NewInvalidInputError("local_name argument should not be a uri") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if !IsFileUri(uri) { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 126 | return corderrors.NewInvalidInputError("uri argument should be a file:// uri") |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 129 | h, err := DownloadFile(conn, descriptor, uri, local_name) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 134 | data := make([]TransferOutput, 1) |
| 135 | data[0].Chunks = h.chunks |
| 136 | data[0].Bytes = h.bytes |
| 137 | data[0].Status = h.status |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 138 | data[0].Checksum = h.GetChecksum() |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 139 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 140 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_TRANSFER_FORMAT, "{{.Status}}", data) |
Scott Baker | 2c0ebda | 2019-05-06 16:55:47 -0700 | [diff] [blame] | 141 | |
| 142 | return nil |
| 143 | } |