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