From 5147753a799149cebd6e8b66fd947b848ab08ea7 Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Wed, 23 Aug 2023 19:25:52 +0200 Subject: [PATCH] Address comments part 2 * OpenAPI Mock spec path adjusted * Dropbox FileMetadata reflection refactored * NaturalNumber type added --- cmd/backup/config.go | 21 ++++++++++++++++++++- cmd/backup/script.go | 2 +- internal/storage/dropbox/dropbox.go | 20 ++++++++++---------- test/dropbox/docker-compose.yml | 4 +++- test/dropbox/openapi-mock.yaml | 25 ------------------------- 5 files changed, 34 insertions(+), 38 deletions(-) delete mode 100644 test/dropbox/openapi-mock.yaml diff --git a/cmd/backup/config.go b/cmd/backup/config.go index c696f9c..13f76c5 100644 --- a/cmd/backup/config.go +++ b/cmd/backup/config.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "os" "regexp" + "strconv" "time" ) @@ -75,7 +76,7 @@ type Config struct { DropboxAppKey string `split_words:"true"` DropboxAppSecret string `split_words:"true"` DropboxRemotePath string `split_words:"true"` - DropboxConcurrencyLevel int `split_words:"true" default:"6"` + DropboxConcurrencyLevel NaturalNumber `split_words:"true" default:"6"` } func (c *Config) resolveSecret(envVar string, secretPath string) (string, error) { @@ -141,3 +142,21 @@ func (r *RegexpDecoder) Decode(v string) error { *r = RegexpDecoder{Re: re} return nil } + +type NaturalNumber int + +func (n *NaturalNumber) Decode(v string) error { + asInt, err := strconv.Atoi(v) + if err != nil { + return fmt.Errorf("config: error converting %s to int", v) + } + if asInt <= 0 { + return fmt.Errorf("config: expected a natural number, got %d", asInt) + } + *n = NaturalNumber(asInt) + return nil +} + +func (n *NaturalNumber) Int() int { + return int(*n) +} diff --git a/cmd/backup/script.go b/cmd/backup/script.go index cff52a0..1dace6c 100644 --- a/cmd/backup/script.go +++ b/cmd/backup/script.go @@ -227,7 +227,7 @@ func newScript() (*script, error) { AppKey: s.c.DropboxAppKey, AppSecret: s.c.DropboxAppSecret, RemotePath: s.c.DropboxRemotePath, - ConcurrencyLevel: s.c.DropboxConcurrencyLevel, + ConcurrencyLevel: s.c.DropboxConcurrencyLevel.Int(), } dropboxBackend, err := dropbox.NewStorageBackend(dropboxConfig, logFunc) if err != nil { diff --git a/internal/storage/dropbox/dropbox.go b/internal/storage/dropbox/dropbox.go index f81637c..51b504b 100644 --- a/internal/storage/dropbox/dropbox.go +++ b/internal/storage/dropbox/dropbox.go @@ -8,7 +8,6 @@ import ( "os" "path" "path/filepath" - "reflect" "strings" "sync" "time" @@ -233,17 +232,18 @@ func (b *dropboxStorage) Prune(deadline time.Time, pruningPrefix string) (*stora var matches []*files.FileMetadata var lenCandidates int for _, candidate := range entries { - if reflect.Indirect(reflect.ValueOf(candidate)).Type() != reflect.TypeOf(files.FileMetadata{}) { + switch candidate := candidate.(type) { + case *files.FileMetadata: + if !strings.HasPrefix(candidate.Name, pruningPrefix) { + continue + } + lenCandidates++ + if candidate.ServerModified.Before(deadline) { + matches = append(matches, candidate) + } + default: continue } - candidate := candidate.(*files.FileMetadata) - if !strings.HasPrefix(candidate.Name, pruningPrefix) { - continue - } - lenCandidates++ - if candidate.ServerModified.Before(deadline) { - matches = append(matches, candidate) - } } stats := &storage.PruneStats{ diff --git a/test/dropbox/docker-compose.yml b/test/dropbox/docker-compose.yml index 748bbb4..a734098 100644 --- a/test/dropbox/docker-compose.yml +++ b/test/dropbox/docker-compose.yml @@ -5,9 +5,11 @@ services: image: muonsoft/openapi-mock environment: OPENAPI_MOCK_USE_EXAMPLES: if_present - OPENAPI_MOCK_SPECIFICATION_URL: https://raw.githubusercontent.com/offen/docker-volume-backup/2ea59a649be79d6cb22b8b5ad29e24377d4ab9d4/test/dropbox/user_v2.yaml + OPENAPI_MOCK_SPECIFICATION_URL: '/etc/openapi/user_v2.yaml' ports: - 8080:8080 + volumes: + - ./user_v2.yaml:/etc/openapi/user_v2.yaml backup: image: offen/docker-volume-backup:${TEST_VERSION:-canary} diff --git a/test/dropbox/openapi-mock.yaml b/test/dropbox/openapi-mock.yaml deleted file mode 100644 index 21707b6..0000000 --- a/test/dropbox/openapi-mock.yaml +++ /dev/null @@ -1,25 +0,0 @@ -# OpenAPI specification options -openapi: - specification_url: '/etc/openapi/user_v2.yaml' - -# web server options -http: - cors_enabled: false - port: 8080 - response_timeout: 1.0 - -# application specific options -application: - debug: false - log_format: tty - log_level: info - -# options to control generation process -generation: - default_min_float: -1.073741823e+09 - default_max_float: 1.073741823e+09 - default_min_int: 0 - default_max_int: 2147483647 - null_probability: 0 - suppress_errors: false - use_examples: 'if_present'