-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_cors_add.go
120 lines (95 loc) · 3 KB
/
storage_cors_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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/storage/sos"
)
const (
storageCORSAddCmdFlagAllowedOrigin = "allowed-origin"
storageCORSAddCmdFlagAllowedMethod = "allowed-method"
storageCORSAddCmdFlagAllowedHeader = "allowed-header"
)
// CORSRuleFromCmdFlags returns a non-nil pointer to a sos.CORSRule struct if at least
// one of the CORS-related command flags is set.
func CORSRuleFromCmdFlags(flags *pflag.FlagSet) *sos.CORSRule {
var cors *sos.CORSRule
flags.VisitAll(func(flag *pflag.Flag) {
switch flag.Name {
case storageCORSAddCmdFlagAllowedOrigin:
if v, _ := flags.GetStringSlice(storageCORSAddCmdFlagAllowedOrigin); len(v) > 0 {
if cors == nil {
cors = &sos.CORSRule{}
}
cors.AllowedOrigins = v
}
case storageCORSAddCmdFlagAllowedMethod:
if v, _ := flags.GetStringSlice(storageCORSAddCmdFlagAllowedMethod); len(v) > 0 {
if cors == nil {
cors = &sos.CORSRule{}
}
cors.AllowedMethods = v
}
case storageCORSAddCmdFlagAllowedHeader:
if v, _ := flags.GetStringSlice(storageCORSAddCmdFlagAllowedHeader); len(v) > 0 {
if cors == nil {
cors = &sos.CORSRule{}
}
cors.AllowedHeaders = v
}
default:
return
}
})
return cors
}
var storageCORSAddCmd = &cobra.Command{
Use: "add sos://BUCKET",
Short: "Add a CORS configuration rule to a bucket",
Long: `This command adds a new rule to the current bucket CORS
configuration.
Example:
exo storage cors add sos://my-bucket \
--allowed-origin "https://my-website.net" \
--allowed-method "*"
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
return cmdCheckRequiredFlags(cmd, []string{
storageCORSAddCmdFlagAllowedOrigin,
storageCORSAddCmdFlagAllowedMethod,
})
},
RunE: func(cmd *cobra.Command, args []string) error {
bucket := args[0]
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptZoneFromBucket(gContext, bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
cors := CORSRuleFromCmdFlags(cmd.Flags())
if err := storage.AddBucketCORSRule(gContext, bucket, cors); err != nil {
return fmt.Errorf("unable to add rule to the bucket CORS configuration: %w", err)
}
if !globalstate.Quiet {
return printOutput(storage.ShowBucket(gContext, bucket))
}
return nil
},
}
func init() {
storageCORSAddCmd.Flags().StringSlice(storageCORSAddCmdFlagAllowedOrigin, nil,
"allowed origin (can be repeated multiple times)")
storageCORSAddCmd.Flags().StringSlice(storageCORSAddCmdFlagAllowedMethod, nil,
"allowed method (can be repeated multiple times)")
storageCORSAddCmd.Flags().StringSlice(storageCORSAddCmdFlagAllowedHeader, nil,
"allowed header (can be repeated multiple times)")
storageCORSCmd.AddCommand(storageCORSAddCmd)
}