/* guestbook - Standalone guestbook server for static websites * Copyright (C) 2025 Vidhu Kant Sharma * * 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 . */ 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, time 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, &entry.Time); 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)") }