diff options
| author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2026-02-03 09:00:02 +0530 |
|---|---|---|
| committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2026-02-03 09:00:02 +0530 |
| commit | ca4595c15b7cd89fc04beb9e3587ecea2f2407f5 (patch) | |
| tree | c4693ba9c5837b48ab8dac36f40bdef1ecd147ce /cmd | |
| parent | 5cc5783f453ce68a5d5276ba9833ae1fb9acb3f9 (diff) | |
added del commandv1.6.0
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/del.go | 91 | ||||
| -rw-r--r-- | cmd/root.go | 4 |
2 files changed, 93 insertions, 2 deletions
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 <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 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 <vidhukant@vidhukant.com> + * Copyright (C) 2025-2026 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 @@ -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", } |