blob: 59be175c7c8c164fda97e9c8a866a0366e389f17 [file] [log] [blame]
Scott Baker2c0ebda2019-05-06 16:55:47 -07001/*
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 */
17package commands
18
19import (
Scott Baker2c0ebda2019-05-06 16:55:47 -070020 "errors"
Scott Baker72efd752019-05-15 13:12:20 -070021 "fmt"
Scott Baker2c0ebda2019-05-06 16:55:47 -070022 flags "github.com/jessevdk/go-flags"
Scott Baker2c0ebda2019-05-06 16:55:47 -070023 "strings"
24)
25
26const (
27 DEFAULT_TRANSFER_FORMAT = "table{{ .Status }}\t{{ .Checksum }}\t{{ .Chunks }}\t{{ .Bytes }}"
28)
29
30type TransferOutput struct {
31 Status string `json:"status"`
32 Checksum string `json:"checksum"`
33 Chunks int `json:"chunks"`
34 Bytes int `json:"bytes"`
35}
36
37type 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
46type TransferDownload struct {
47 OutputOptions
48 Args struct {
49 URI string
50 LocalFileName string
51 } `positional-args:"yes" required:"yes"`
52}
53
54type TransferOpts struct {
55 Upload TransferUpload `command:"upload"`
56 Download TransferDownload `command:"download"`
57}
58
59var transferOpts = TransferOpts{}
60
61func RegisterTransferCommands(parser *flags.Parser) {
62 parser.AddCommand("transfer", "file transfer commands", "Commands to transfer files to and from XOS", &transferOpts)
63}
64
Scott Baker2c0ebda2019-05-06 16:55:47 -070065/* Command processors */
66
67func (options *TransferUpload) Execute(args []string) error {
68
Scott Baker6cf525a2019-05-09 12:25:08 -070069 conn, descriptor, err := InitReflectionClient()
Scott Baker2c0ebda2019-05-06 16:55:47 -070070 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 Baker6cf525a2019-05-09 12:25:08 -070078 if IsFileUri(local_name) {
79 return errors.New("local_name argument should not be a uri")
Scott Baker2c0ebda2019-05-06 16:55:47 -070080 }
81
Scott Baker6cf525a2019-05-09 12:25:08 -070082 if !IsFileUri(uri) {
83 return errors.New("uri argument should be a file:// uri")
Scott Baker2c0ebda2019-05-06 16:55:47 -070084 }
85
Scott Baker72efd752019-05-15 13:12:20 -070086 h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, options.ChunkSize)
Scott Baker55694ca2019-06-13 14:52:28 -070087 if err != nil {
88 return err
89 }
Scott Baker72efd752019-05-15 13:12:20 -070090
91 if upload_result.GetFieldByName("checksum").(string) != h.GetChecksum() {
92 return fmt.Errorf("Checksum mismatch, expected=%s, received=%s",
93 h.GetChecksum(),
94 upload_result.GetFieldByName("checksum").(string))
95 }
Scott Baker2c0ebda2019-05-06 16:55:47 -070096
Scott Baker2c0ebda2019-05-06 16:55:47 -070097 data := make([]TransferOutput, 1)
Scott Baker72efd752019-05-15 13:12:20 -070098 data[0].Checksum = upload_result.GetFieldByName("checksum").(string)
99 data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32))
100 data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32))
101 data[0].Status = GetEnumValue(upload_result, "status")
Scott Baker2c0ebda2019-05-06 16:55:47 -0700102
Scott Baker5281d002019-05-16 10:45:26 -0700103 FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_TRANSFER_FORMAT, "{{.Status}}", data)
Scott Baker2c0ebda2019-05-06 16:55:47 -0700104
105 return nil
106}
107
108func IsFileUri(s string) bool {
109 return strings.HasPrefix(s, "file://")
110}
111
112func (options *TransferDownload) Execute(args []string) error {
Scott Baker6cf525a2019-05-09 12:25:08 -0700113 conn, descriptor, err := InitReflectionClient()
Scott Baker2c0ebda2019-05-06 16:55:47 -0700114 if err != nil {
115 return err
116 }
117 defer conn.Close()
118
119 local_name := options.Args.LocalFileName
120 uri := options.Args.URI
121
122 if IsFileUri(local_name) {
123 return errors.New("local_name argument should not be a uri")
124 }
125
126 if !IsFileUri(uri) {
127 return errors.New("uri argument should be a file:// uri")
128 }
129
Scott Baker6cf525a2019-05-09 12:25:08 -0700130 h, err := DownloadFile(conn, descriptor, uri, local_name)
Scott Baker2c0ebda2019-05-06 16:55:47 -0700131 if err != nil {
132 return err
133 }
134
Scott Baker2c0ebda2019-05-06 16:55:47 -0700135 data := make([]TransferOutput, 1)
136 data[0].Chunks = h.chunks
137 data[0].Bytes = h.bytes
138 data[0].Status = h.status
Scott Baker5281d002019-05-16 10:45:26 -0700139 data[0].Checksum = h.GetChecksum()
Scott Baker2c0ebda2019-05-06 16:55:47 -0700140
Scott Baker5281d002019-05-16 10:45:26 -0700141 FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_TRANSFER_FORMAT, "{{.Status}}", data)
Scott Baker2c0ebda2019-05-06 16:55:47 -0700142
143 return nil
144}