From ca4595c15b7cd89fc04beb9e3587ecea2f2407f5 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 3 Feb 2026 09:00:02 +0530 Subject: added del command --- cmd/del.go | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 4 +-- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 cmd/del.go (limited to 'cmd') diff --git a/cmd/del.go b/cmd/del.go new file mode 100644 index 0000000..fa4809c --- /dev/null +++ b/cmd/del.go @@ -0,0 +1,91 @@ +/* guestbook - Standalone guestbook server for static websites + * Copyright (C) 2025-2026 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 delCmd = &cobra.Command{ + Use: "del", + Short: "Delete all entries permanently (will print to STDOUT for one last time)", + Long: "Delete all entries permanently (will print to STDOUT for one last time)", + 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 ORDER BY time DESC", 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()) + + _, err := conn.Exec(fmt.Sprintf("DELETE from %s", guestbookName)) + if err != nil { + log.Fatal(err) + } + + fmt.Println("All entries have been deleted.") + }, +} + +func init() { + rootCmd.AddCommand(delCmd) + + dumpCmd.Flags().StringP("guestbook", "g", "my_guestbook", "Guestbook name (defined in /etc/guestbook.toml)") +} diff --git a/cmd/root.go b/cmd/root.go index 5ba7ae6..c7756cc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,5 +1,5 @@ /* guestbook - Standalone guestbook server for static websites - * Copyright (C) 2025 Vidhu Kant Sharma + * Copyright (C) 2025-2026 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 @@ -27,7 +27,7 @@ import ( var rootCmd = &cobra.Command{ Use: "guestbook", - Version: "v1.5.0", + Version: "v1.6.0", Short: "Standalone guestbook server for static websites", Long: "Standalone guestbook server for static websites", } -- cgit v1.2.3