diff options
author | xengineering <me@xengineering.eu> | 2024-05-09 22:33:42 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-05-09 22:37:03 +0200 |
commit | 0ac3cc76b4b6c824c4b3f7a357d40b487984abfb (patch) | |
tree | 5391e5f27b344d9eab288876052ec63a2f97db72 | |
parent | 0142af99aba36241c276a56a088e7aac10c62f86 (diff) | |
download | ceres-0ac3cc76b4b6c824c4b3f7a357d40b487984abfb.tar ceres-0ac3cc76b4b6c824c4b3f7a357d40b487984abfb.tar.zst ceres-0ac3cc76b4b6c824c4b3f7a357d40b487984abfb.zip |
Inject examples only with new --example flag
The default use case should be to not inject example recipes.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | flags.go | 8 | ||||
-rw-r--r-- | main.go | 6 | ||||
-rw-r--r-- | model/database.go | 7 | ||||
-rw-r--r-- | model/recipes_test.go | 2 |
5 files changed, 19 insertions, 6 deletions
@@ -11,7 +11,7 @@ $(BUILD_DIR)/$(TARGET_EXEC): .PHONY: debug debug: - go run -ldflags "-X main.version=$(VERSION)" $(MODULE_NAME) + go run -ldflags "-X main.version=$(VERSION)" $(MODULE_NAME) -e .PHONY: tests tests: @@ -8,13 +8,16 @@ import ( const help = `Ceres - Recipe server for local networks Usage: ceres [-h | --help] [-v | --version] [-c | --config] + [-e | --example] -h, --help show this help page and exit -v, --version print version information -c, --config file path to configuration file + -e, --examples inject example recipes on startup ` -var printVersion bool +var printVersion bool +var injectExamples bool func init() { flag.BoolVar(&printVersion, "version", false, "print version information") @@ -23,6 +26,9 @@ func init() { flag.StringVar(&config.Path, "config", "", "file path to configuration file") flag.StringVar(&config.Path, "c", "", "file path to configuration file") + flag.BoolVar(&injectExamples, "examples", false, "inject example recipes on startup") + flag.BoolVar(&injectExamples, "e", false, "inject example recipes on startup") + flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), help) } @@ -37,7 +37,11 @@ func main() { model.ConnectDatabase(filepath.Join(storage.Path, "ceres.sqlite3")) defer model.DisconnectDatabase() model.MigrateDatabase(version) - model.InjectTestRecipes() + + if injectExamples { + model.InjectExampleRecipes() + log.Println("Added example recipes") + } server := NewServer(config.HttpAddress) go server.Start() diff --git a/model/database.go b/model/database.go index 85d2662..f5fd67a 100644 --- a/model/database.go +++ b/model/database.go @@ -149,8 +149,8 @@ func MigrateDatabase(execVersion string) { } } -func InjectTestRecipes() { - Transaction(func(tx *sql.Tx) error { +func InjectExampleRecipes() { + err := Transaction(func(tx *sql.Tx) error { recipes := RecipeTestData() for _, recipe := range recipes { @@ -162,6 +162,9 @@ func InjectTestRecipes() { return nil }) + if err != nil { + log.Fatalf("Failed to inject example recipes: %v", err) + } } func DisconnectDatabase() { diff --git a/model/recipes_test.go b/model/recipes_test.go index 159422f..13a8b2f 100644 --- a/model/recipes_test.go +++ b/model/recipes_test.go @@ -22,7 +22,7 @@ func TestRecipesRead(t *testing.T) { defer DisconnectDatabase() MigrateDatabase("dummy_version") - InjectTestRecipes() + InjectExampleRecipes() r := make(Recipes, 0) |