-
Notifications
You must be signed in to change notification settings - Fork 19
/
storage_headers_add.go
174 lines (137 loc) · 4.75 KB
/
storage_headers_add.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
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
package cmd
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/pkg/storage/sos"
)
var storageHeaderAddCmd = &cobra.Command{
Use: "add sos://BUCKET/(OBJECT|PREFIX/)",
Short: "Add HTTP headers to an object",
Long: fmt.Sprintf(`This command adds response HTTP headers to objects.
Example:
exo storage headers add sos://my-bucket/data.json \
--content-type=application/json \
--cache-control=no-store
Note: adding an already existing header will overwrite its value.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&sos.ShowObjectOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
if !strings.Contains(args[0], "/") {
cmdExitOnUsageError(cmd, fmt.Sprintf("invalid argument: %q", args[0]))
}
if headers := storageHeadersFromCmdFlags(cmd.Flags()); headers == nil {
cmdExitOnUsageError(cmd, "no header flag specified")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
prefix string
)
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
parts := strings.SplitN(args[0], "/", 2)
bucket, prefix = parts[0], parts[1]
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptZoneFromBucket(gContext, bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
headers := storageHeadersFromCmdFlags(cmd.Flags())
if err := storage.UpdateObjectsHeaders(gContext, bucket, prefix, headers, recursive); err != nil {
return fmt.Errorf("unable to add headers to object: %w", err)
}
if !globalstate.Quiet && !recursive && !strings.HasSuffix(prefix, "/") {
return printOutput(storage.ShowObject(gContext, bucket, prefix))
}
if !globalstate.Quiet {
fmt.Println("Headers added successfully")
}
return nil
},
}
func init() {
storageHeaderAddCmd.Flags().BoolP("recursive", "r", false,
"add headers recursively (with object prefix only)")
storageHeaderAddCmd.Flags().String(strings.ToLower(sos.ObjectHeaderCacheControl), "",
`value for "Cache-Control" header`)
storageHeaderAddCmd.Flags().String(strings.ToLower(sos.ObjectHeaderContentDisposition), "",
`value for "Content-Disposition" header`)
storageHeaderAddCmd.Flags().String(strings.ToLower(sos.ObjectHeaderContentEncoding), "",
`value for "Content-Encoding" header`)
storageHeaderAddCmd.Flags().String(strings.ToLower(sos.ObjectHeaderContentLanguage), "",
`value for "Content-Language" header`)
storageHeaderAddCmd.Flags().String(strings.ToLower(sos.ObjectHeaderContentType), "",
`value for "Content-Type" header`)
storageHeaderAddCmd.Flags().String(strings.ToLower(sos.ObjectHeaderExpires), "",
`value for "Expires" header`)
storageHeaderCmd.AddCommand(storageHeaderAddCmd)
}
// storageHeadersFromCmdFlags returns a non-nil map if at least
// one of the header-related command flags is set.
func storageHeadersFromCmdFlags(flags *pflag.FlagSet) map[string]*string {
var headers map[string]*string
flags.VisitAll(func(flag *pflag.Flag) {
switch flag.Name {
case strings.ToLower(sos.ObjectHeaderCacheControl):
if v := flag.Value.String(); v != "" {
if headers == nil {
headers = make(map[string]*string)
}
headers[sos.ObjectHeaderCacheControl] = aws.String(v)
}
case strings.ToLower(sos.ObjectHeaderContentDisposition):
if v := flag.Value.String(); v != "" {
if headers == nil {
headers = make(map[string]*string)
}
headers[sos.ObjectHeaderContentDisposition] = aws.String(v)
}
case strings.ToLower(sos.ObjectHeaderContentEncoding):
if v := flag.Value.String(); v != "" {
if headers == nil {
headers = make(map[string]*string)
}
headers[sos.ObjectHeaderContentEncoding] = aws.String(v)
}
case strings.ToLower(sos.ObjectHeaderContentLanguage):
if v := flag.Value.String(); v != "" {
if headers == nil {
headers = make(map[string]*string)
}
headers[sos.ObjectHeaderContentLanguage] = aws.String(v)
}
case strings.ToLower(sos.ObjectHeaderContentType):
if v := flag.Value.String(); v != "" {
if headers == nil {
headers = make(map[string]*string)
}
headers[sos.ObjectHeaderContentType] = aws.String(v)
}
case strings.ToLower(sos.ObjectHeaderExpires):
if v := flag.Value.String(); v != "" {
if headers == nil {
headers = make(map[string]*string)
}
headers[sos.ObjectHeaderExpires] = aws.String(v)
}
default:
return
}
})
return headers
}