forked from fsouza/fake-gcs-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
185 lines (168 loc) · 6.18 KB
/
config.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
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2019 Francisco Souza. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package config provides utilities for managing fake-gcs-server's
// configuration using command line flags.
package config
import (
"flag"
"fmt"
"math"
"strings"
"github.com/fsouza/fake-gcs-server/fakestorage"
"github.com/fsouza/fake-gcs-server/internal/notification"
"github.com/sirupsen/logrus"
)
const (
filesystemBackend = "filesystem"
memoryBackend = "memory"
eventFinalize = "finalize"
eventDelete = "delete"
eventMetadataUpdate = "metadataUpdate"
eventArchive = "archive"
)
type Config struct {
Seed string
publicHost string
externalURL string
allowedCORSHeaders []string
scheme string
host string
port uint
backend string
fsRoot string
event EventConfig
bucketLocation string
certificateLocation string
privateKeyLocation string
LogLevel logrus.Level
}
type EventConfig struct {
pubsubProjectID string
pubsubTopic string
prefix string
list []string
}
// Load parses the given arguments list and return a config object (and/or an
// error in case of failures).
func Load(args []string) (Config, error) {
var cfg Config
var allowedCORSHeaders string
var eventList string
var givenLogLevel string
fs := flag.NewFlagSet("fake-gcs-server", flag.ContinueOnError)
fs.StringVar(&cfg.backend, "backend", filesystemBackend, "storage backend (memory or filesystem)")
fs.StringVar(&cfg.fsRoot, "filesystem-root", "/storage", "filesystem root (required for the filesystem backend). folder will be created if it doesn't exist")
fs.StringVar(&cfg.publicHost, "public-host", "storage.googleapis.com", "Optional URL for public host")
fs.StringVar(&cfg.externalURL, "external-url", "", "optional external URL, returned in the Location header for uploads. Defaults to the address where the server is running")
fs.StringVar(&cfg.scheme, "scheme", "https", "using http or https")
fs.StringVar(&cfg.host, "host", "0.0.0.0", "host to bind to")
fs.StringVar(&cfg.Seed, "data", "", "where to load data from (provided that the directory exists)")
fs.StringVar(&allowedCORSHeaders, "cors-headers", "", "comma separated list of headers to add to the CORS allowlist")
fs.UintVar(&cfg.port, "port", 4443, "port to bind to")
fs.StringVar(&cfg.event.pubsubProjectID, "event.pubsub-project-id", "", "project ID containing the pubsub topic")
fs.StringVar(&cfg.event.pubsubTopic, "event.pubsub-topic", "", "pubsub topic name to publish events on")
fs.StringVar(&cfg.event.prefix, "event.object-prefix", "", "if not empty, only objects having this prefix will generate trigger events")
fs.StringVar(&eventList, "event.list", eventFinalize, "comma separated list of events to publish on cloud function URl. Options are: finalize, delete, and metadataUpdate")
fs.StringVar(&cfg.bucketLocation, "location", "US-CENTRAL1", "location for buckets")
fs.StringVar(&cfg.certificateLocation, "cert-location", "", "location for server certificate")
fs.StringVar(&cfg.privateKeyLocation, "private-key-location", "", "location for private key")
fs.StringVar(&givenLogLevel, "log-level", "info", "level for logging. Options same as for logrus: trace, debug, info, warn, error, fatal, and panic")
err := fs.Parse(args)
if err != nil {
return cfg, err
}
cfg.LogLevel, err = logrus.ParseLevel(givenLogLevel)
if err != nil {
return cfg, err
}
if allowedCORSHeaders != "" {
cfg.allowedCORSHeaders = strings.Split(allowedCORSHeaders, ",")
}
if eventList != "" {
cfg.event.list = strings.Split(eventList, ",")
}
return cfg, cfg.validate()
}
func (c *Config) validate() error {
if c.backend != memoryBackend && c.backend != filesystemBackend {
return fmt.Errorf(`invalid backend %q, must be either "memory" or "filesystem"`, c.backend)
}
if c.backend == filesystemBackend && c.fsRoot == "" {
return fmt.Errorf("backend %q requires the filesystem-root to be defined", c.backend)
}
if c.scheme != "http" && c.scheme != "https" {
return fmt.Errorf(`invalid scheme %s, must be either "http"" or "https"`, c.scheme)
}
if c.port > math.MaxUint16 {
return fmt.Errorf("port %d is too high, maximum value is %d", c.port, math.MaxUint16)
}
return c.event.validate()
}
func (c *EventConfig) validate() error {
switch c.pubsubProjectID {
case "":
if c.pubsubTopic != "" {
return fmt.Errorf("missing event pubsub project ID")
}
default:
if c.pubsubTopic == "" {
return fmt.Errorf("missing event pubsub topic ID")
}
for i, event := range c.list {
e := strings.TrimSpace(event)
switch e {
case eventFinalize, eventDelete, eventMetadataUpdate, eventArchive:
default:
return fmt.Errorf("%s is an invalid event", e)
}
c.list[i] = e
}
if len(c.list) == 0 {
return fmt.Errorf("event list cannot be empty")
}
}
return nil
}
func (c *Config) ToFakeGcsOptions() fakestorage.Options {
storageRoot := c.fsRoot
if c.backend == memoryBackend {
storageRoot = ""
}
eventOptions := notification.EventManagerOptions{
ProjectID: c.event.pubsubProjectID,
TopicName: c.event.pubsubTopic,
ObjectPrefix: c.event.prefix,
}
if c.event.pubsubProjectID != "" && c.event.pubsubTopic != "" {
for _, event := range c.event.list {
switch event {
case eventFinalize:
eventOptions.NotifyOn.Finalize = true
case eventDelete:
eventOptions.NotifyOn.Delete = true
case eventMetadataUpdate:
eventOptions.NotifyOn.MetadataUpdate = true
case eventArchive:
eventOptions.NotifyOn.Archive = true
}
}
}
logger := logrus.New()
logger.SetLevel(c.LogLevel)
opts := fakestorage.Options{
StorageRoot: storageRoot,
Scheme: c.scheme,
Host: c.host,
Port: uint16(c.port),
PublicHost: c.publicHost,
ExternalURL: c.externalURL,
AllowedCORSHeaders: c.allowedCORSHeaders,
Writer: logger.Writer(),
EventOptions: eventOptions,
BucketsLocation: c.bucketLocation,
CertificateLocation: c.certificateLocation,
PrivateKeyLocation: c.privateKeyLocation,
}
return opts
}