-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathobject_stat.go
More file actions
150 lines (133 loc) · 3.84 KB
/
object_stat.go
File metadata and controls
150 lines (133 loc) · 3.84 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
/***************************************************************
*
* Copyright (C) 2024, 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"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/pelicanplatform/pelican/client"
"github.com/pelicanplatform/pelican/config"
)
var (
statCmd = &cobra.Command{
Use: "stat {object}",
Short: "Stat objects in a namespace from a federation",
Run: statMain,
}
)
func init() {
flagSet := statCmd.Flags()
flagSet.StringP("token", "t", "", "Token file to use for transfer")
flagSet.BoolP("json", "j", false, "Print results in JSON format")
flagSet.StringArray(
"checksums",
[]string{},
fmt.Sprintf("Checksums to request from the server. Known values are: %s",
strings.Join(client.KnownChecksumTypesAsHttpDigest(), ", "),
),
)
objectCmd.AddCommand(statCmd)
}
func statMain(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")
jsn, _ := cmd.Flags().GetBool("json")
// Get the checksums to request
checksums, _ := cmd.Flags().GetStringArray("checksums")
checksumTypes := make([]client.ChecksumType, 0, len(checksums))
if len(checksums) > 0 {
// Convert the checksums to the correct type
for _, checksum := range checksums {
checksumType := client.ChecksumFromHttpDigest(checksum)
if checksumType == client.AlgUnknown {
log.Errorf("Unknown checksum type: %s", checksum)
err = cmd.Help()
if err != nil {
log.Errorln("Failed to print out help:", err)
}
os.Exit(1)
}
checksumTypes = append(checksumTypes, checksumType)
}
}
if len(args) < 1 {
log.Errorln("No object provided")
err = cmd.Help()
if err != nil {
log.Errorln("Failed to print out help:", err)
}
os.Exit(1)
}
object := args[len(args)-1]
log.Debugln("Object:", object)
statInfo, err := client.DoStat(ctx, object, client.WithTokenLocation(tokenLocation), client.WithRequestChecksums(checksumTypes))
// Exit with failure
if err != nil {
// Print the list of errors
errMsg := err.Error()
var te *client.TransferErrors
if errors.As(err, &te) {
errMsg = te.UserError()
}
log.Errorln("Failure getting " + object + ": " + errMsg)
if client.ShouldRetry(err) {
log.Errorln("Errors are retryable")
os.Exit(11)
}
os.Exit(1)
}
if jsn {
// Print our stat info in JSON format:
jsonData, err := json.Marshal(statInfo)
if err != nil {
log.Errorf("Failed to parse object/collection stat info to JSON format: %v", err)
os.Exit(1)
}
fmt.Println(string(jsonData))
return
} else {
// Print our stat info:
fmt.Println("Name:", statInfo.Name)
fmt.Println("Size:", statInfo.Size)
fmt.Println("ModTime:", statInfo.ModTime)
fmt.Println("IsCollection:", statInfo.IsCollection)
if len(statInfo.Checksums) > 0 {
fmt.Println("Checksums:")
for alg, value := range statInfo.Checksums {
fmt.Printf(" %s: %s\n", alg, value)
}
}
return
}
}