aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/dump.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/dump.go')
-rw-r--r--cmd/dump.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/cmd/dump.go b/cmd/dump.go
new file mode 100644
index 0000000..281b7d6
--- /dev/null
+++ b/cmd/dump.go
@@ -0,0 +1,85 @@
+/* guestbook - Standalone guestbook server for static websites
+ * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package cmd
+
+import (
+ "fmt"
+ "log"
+ "bytes"
+ "text/template"
+
+ "vidhukant.com/guestbook/db"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/viper"
+)
+
+var dumpCmd = &cobra.Command{
+ Use: "dump",
+ Short: "Dump the entries into STDOUT",
+ Long: "Dump the entries into STDOUT",
+ Run: func(cmd *cobra.Command, args []string) {
+ guestbookName, err := cmd.Flags().GetString("guestbook")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ outputTemplate := viper.GetString(guestbookName + ".template")
+
+ conn := db.Connect()
+ defer conn.Close()
+
+ rows, err := conn.Query(fmt.Sprintf("SELECT name, website, message FROM %s", guestbookName))
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer rows.Close()
+
+
+ var entries []db.GuestbookEntry
+
+ for rows.Next() {
+ var entry db.GuestbookEntry
+ if err := rows.Scan(&entry.Name, &entry.Website, &entry.Message); err != nil {
+ log.Fatal(err)
+ }
+ entries = append(entries, entry)
+ }
+ if rows.Err() != nil {
+ log.Fatal(err)
+ }
+
+ tmpl, err := template.New("render").Parse(outputTemplate)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var buf bytes.Buffer
+ if err := tmpl.Execute(&buf, entries); err != nil {
+ panic(err)
+ }
+
+ fmt.Println(buf.String())
+ },
+}
+
+func init() {
+ rootCmd.AddCommand(dumpCmd)
+
+ dumpCmd.Flags().StringP("guestbook", "g", "my_guestbook", "Guestbook name (defined in /etc/guestbook.toml)")
+}