summaryrefslogtreecommitdiffstats
path: root/golang/cmd/fatcatd
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-05-10 10:34:47 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-05-10 10:34:47 -0700
commitb9200623b0be3c8b663c1000b63c3ab581b7d34b (patch)
treed90857ef98a5b46aecff803e5c3f80beb7019995 /golang/cmd/fatcatd
parent6235211860b5b3794dfe9e64bc33856dfad21897 (diff)
downloadfatcat-b9200623b0be3c8b663c1000b63c3ab581b7d34b.tar.gz
fatcat-b9200623b0be3c8b663c1000b63c3ab581b7d34b.zip
basic database schema reload
Diffstat (limited to 'golang/cmd/fatcatd')
-rw-r--r--golang/cmd/fatcatd/reinit.go42
-rw-r--r--golang/cmd/fatcatd/root.go2
2 files changed, 43 insertions, 1 deletions
diff --git a/golang/cmd/fatcatd/reinit.go b/golang/cmd/fatcatd/reinit.go
new file mode 100644
index 00000000..b7cfa468
--- /dev/null
+++ b/golang/cmd/fatcatd/reinit.go
@@ -0,0 +1,42 @@
+
+package main
+
+import (
+ "github.com/spf13/viper"
+ log "github.com/sirupsen/logrus"
+ "github.com/go-pg/pg"
+ "github.com/spf13/cobra"
+ "github.com/gobuffalo/packr"
+)
+
+var reinitCmd = &cobra.Command{
+ Use: "reinit [options]",
+ Short: "Reset database schema",
+ Run: func(cmd *cobra.Command, args[] string) {
+ main_reinit()
+ },
+}
+
+func main_reinit() {
+
+ box := packr.NewBox("../../sql")
+ sql_schema, err := box.MustString("fatcat-schema.sql")
+ if err != nil {
+ log.Panicf("finding SQL file: {}", err)
+ }
+
+ db_options, err := pg.ParseURL(viper.GetString("db_url"))
+ if err != nil {
+ log.Panicf("parsing DB string: {}", err)
+ }
+ db := pg.Connect(db_options)
+ defer db.Close()
+
+ log.Info("Starting load...")
+ _, err = db.Exec(sql_schema)
+ if err != nil {
+ log.Fatalf("Error loading SQL: {}", err)
+ }
+ log.Info("Success!")
+
+}
diff --git a/golang/cmd/fatcatd/root.go b/golang/cmd/fatcatd/root.go
index b5263b06..12be35b3 100644
--- a/golang/cmd/fatcatd/root.go
+++ b/golang/cmd/fatcatd/root.go
@@ -29,12 +29,12 @@ func init() {
serveCmd.Flags().String("db-url", "postgres://", "database connecion string")
rootCmd.AddCommand(serveCmd)
+ rootCmd.AddCommand(reinitCmd)
}
func initConfig() {
viper.SetDefault("port", 9411)
- viper.SetDefault("db_url", "postgres://bnewbold@localhost/fatcat")
viper.SetEnvPrefix("FATCAT")
viper.AutomaticEnv()