diff --git a/CHANGELOG.md b/CHANGELOG.md index b88b7c8cc..b376baad8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added support for ecs style validation for dataset fields. [#520](https://github.com/elastic/package-registry/pull/520) * Use BasePackage for search output data. [#529](https://github.com/elastic/package-registry/pull/529) * Add support for owner field in package manifest. [#536](https://github.com/elastic/package-registry/pull/536) +* Introduce development mode. [#543](https://github.com/elastic/package-registry/pull/543) ### Deprecated diff --git a/devmode/devmode.go b/devmode/devmode.go new file mode 100644 index 000000000..ed1aea2aa --- /dev/null +++ b/devmode/devmode.go @@ -0,0 +1,19 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package devmode + +import "log" + +var enabled bool + +func Enable() { + log.Println("Enable development mode") + + enabled = true +} + +func Enabled() bool { + return enabled +} diff --git a/main.go b/main.go index e695059a6..f0a8edbd6 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( ucfgYAML "github.com/elastic/go-ucfg/yaml" + "github.com/elastic/package-registry/devmode" "github.com/elastic/package-registry/util" ) @@ -42,6 +43,7 @@ var ( CacheTimeSearch: 10 * time.Minute, CacheTimeCategories: 10 * time.Minute, CacheTimeCatchAll: 10 * time.Minute, + DevMode: false, } ) @@ -57,6 +59,8 @@ type Config struct { CacheTimeSearch time.Duration `config:"cache_time.search"` CacheTimeCategories time.Duration `config:"cache_time.categories"` CacheTimeCatchAll time.Duration `config:"cache_time.catch_all"` + + DevMode bool `config:"dev_mode"` } func main() { @@ -68,6 +72,11 @@ func main() { packagesBasePaths := getPackagesBasePaths(config) ensurePackagesAvailable(packagesBasePaths) + // If config.DevMode is set, the service will not cache package content. + if config.DevMode { + devmode.Enable() + } + // If -dry-run=true is set, service stops here after validation if dryRun { return diff --git a/util/packages.go b/util/packages.go index 34b13d78a..f0c33e664 100644 --- a/util/packages.go +++ b/util/packages.go @@ -10,17 +10,19 @@ import ( "path/filepath" "strings" + "github.com/elastic/package-registry/devmode" + "github.com/pkg/errors" ) var packageList []Package // GetPackages returns a slice with all existing packages. -// The list is stored in memory and on the second request directly -// served from memory. This assumes chnages to packages only happen on restart. +// The list is stored in memory and on the second request directly served from memory. +// This assumes changes to packages only happen on restart (unless development mode is enabled). // Caching the packages request many file reads every time this method is called. func GetPackages(packagesBasePaths []string) ([]Package, error) { - if packageList != nil { + if !devmode.Enabled() && packageList != nil { return packageList, nil }