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