-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathobject_get.go
More file actions
202 lines (178 loc) · 6.26 KB
/
object_get.go
File metadata and controls
202 lines (178 loc) · 6.26 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/***************************************************************
*
* Copyright (C) 2025, Pelican Project, Morgridge Institute for Research
*
* 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 main
import (
"encoding/json"
"net/url"
"os"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/pelicanplatform/pelican/client"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/error_codes"
"github.com/pelicanplatform/pelican/param"
)
var (
getCmd = &cobra.Command{
Use: "get {source ...} {destination}",
Short: "Get a file from a Pelican federation",
Run: getMain,
PreRun: func(cmd *cobra.Command, args []string) {
commaFlagsListToViperSlice(cmd, map[string]string{"cache": param.Client_PreferredCaches.GetName()})
},
}
)
func init() {
flagSet := getCmd.Flags()
flagSet.StringP("cache", "c", "", `A comma-separated list of preferred caches to try for the transfer, where a "+" in the list indicates
the client should fallback to discovered caches if all preferred caches fail.`)
flagSet.StringP("token", "t", "", "Token file to use for transfer")
flagSet.BoolP("recursive", "r", false, "Recursively download a collection. Forces methods to only be http to get the freshest collection contents")
flagSet.StringP("cache-list-name", "n", "xroot", "(Deprecated) Cache list to use, currently either xroot or xroots; may be ignored")
flagSet.Lookup("cache-list-name").Hidden = true
flagSet.String("caches", "", "A JSON file containing the list of caches")
flagSet.String("transfer-stats", "", "A path to a file to write transfer statistics to")
flagSet.String("pack", "", "Package transfer using remote packing functionality (same as '?pack=' query). Options: auto, tar, tar.gz, tar.xz, zip. Default: auto when flag is provided without an explicit value")
objectCmd.AddCommand(getCmd)
}
func getMain(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
// Set up signal handlers to flush logs on SIGTERM
client.SetupSignalHandlers()
err := config.InitClient()
if err != nil {
log.Errorln(err)
if client.IsRetryable(err) {
log.Errorln("Errors are retryable")
os.Exit(11)
} else {
os.Exit(1)
}
}
tokenLocation, _ := cmd.Flags().GetString("token")
pb := newProgressBar()
defer pb.shutdown()
// Check if the program was executed from a terminal
// https://rosettacode.org/wiki/Check_output_device_is_a_terminal#Go
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode()&os.ModeCharDevice) != 0 && param.Logging_LogLocation.GetString() == "" && !param.Logging_DisableProgressBars.GetBool() {
pb.launchDisplay(ctx)
}
log.Debugln("Len of source:", len(args))
if len(args) < 2 {
log.Errorln("No Source or Destination\nTry 'pelican object get --help' for more information.")
os.Exit(1)
}
source := args[:len(args)-1]
dest := args[len(args)-1]
// Handle --pack flag by appending the appropriate query parameter to each source URL
packOption, _ := cmd.Flags().GetString("pack")
if cmd.Flags().Changed("pack") {
if packOption == "" {
packOption = "auto"
}
if _, err := client.GetBehavior(packOption); err != nil {
log.Errorln(err)
os.Exit(1)
}
for i, src := range source {
newSrc, err := addPackQuery(src, packOption)
if err != nil {
log.Errorln("Failed to process --pack option:", err)
os.Exit(1)
}
source[i] = newSrc
}
}
log.Debugln("Sources:", source)
log.Debugln("Destination:", dest)
// Get any configured preferred caches, to be passed along to the client
// as options.
caches, err := getPreferredCaches()
if err != nil {
log.Errorln("Failed to get preferred caches:", err)
os.Exit(1)
}
if len(source) > 1 {
if destStat, err := os.Stat(dest); err != nil {
log.Errorln("Destination does not exist")
os.Exit(1)
} else if !destStat.IsDir() {
log.Errorln("Destination is not a directory")
os.Exit(1)
}
}
var attemptErr error
lastSrc := ""
finalResults := make([][]client.TransferResults, 0)
for _, src := range source {
isRecursive, _ := cmd.Flags().GetBool("recursive")
transferResults, err := client.DoGet(ctx, src, dest, isRecursive, client.WithCallback(pb.callback), client.WithTokenLocation(tokenLocation), client.WithCaches(caches...))
if err != nil {
attemptErr = err
lastSrc = src
break
}
finalResults = append(finalResults, transferResults)
}
// Exit with failure
if attemptErr != nil {
// Print the list of errors
errMsg := attemptErr.Error()
var pe error_codes.PelicanError
var te *client.TransferErrors
if errors.As(attemptErr, &te) {
errMsg = te.UserError()
}
if errors.Is(attemptErr, &pe) {
errMsg = pe.Error()
log.Errorln("Failure getting " + lastSrc + ": " + errMsg)
os.Exit(pe.ExitCode())
} else { // For now, keeping this else here to catch any errors that are not classified PelicanErrors
log.Errorln("Failure getting " + lastSrc + ": " + errMsg)
if client.ShouldRetry(attemptErr) {
log.Errorln("Errors are retryable")
os.Exit(11)
}
os.Exit(1)
}
}
// No failures so we can write the transfer stats
transferStatsFile, _ := cmd.Flags().GetString("transfer-stats")
if transferStatsFile != "" {
transferStats, err := json.MarshalIndent(finalResults, "", " ")
if err != nil {
log.Errorln("Failed to marshal transfer results:", err)
}
err = os.WriteFile(transferStatsFile, transferStats, 0644)
if err != nil {
log.Errorln("Failed to write transfer stats to file:", err)
}
}
}
// addPackQuery appends or updates the "pack" query parameter on the provided URL string.
func addPackQuery(rawURL string, packOption string) (string, error) {
u, err := url.Parse(rawURL)
if err != nil {
return "", err
}
q := u.Query()
q.Set("pack", packOption)
u.RawQuery = q.Encode()
return u.String(), nil
}