package main import ( "code.osinet.fr/fgm/kurz/infrastructure" "database/sql" "fmt" "github.com/spf13/cobra" "gopkg.in/yaml.v2" "time" ) type MapEntry struct { Hash uint64 Url string Date1, Date2, Date3 time.Time RefCount uint32 } var cmdExportContent = &cobra.Command{ Use: "content", Short: "Export the database contents", Long: "Export the whole list of Kurz URLs in the database", Run: exportContentHandler, } func init() { cmdExport.AddCommand(cmdExportContent) } func exportContentLoader(db *sql.DB) ([]MapEntry, error) { stmt, err := db.Prepare(` SELECT Hash, url, Date1, Date2, Date3, RefCount FROM map ORDER BY url`) if err != nil { panic(err.Error()) } defer stmt.Close() rows, err := stmt.Query() if err != nil { return nil, err } defer rows.Close() entry := MapEntry{} var entries []MapEntry for rows.Next() { err = rows.Scan(&entry.Hash, &entry.Url, &entry.Date1, &entry.Date2, &entry.Date3, &entry.RefCount) if err != nil { return nil, err } entries = append(entries, entry) } return entries, nil } func exportContentHandler(cmd *cobra.Command, args []string) { dbDriver, dbDsn := infrastructure.ParseDbCred() db, err := infrastructure.DbDial(dbDriver, dbDsn) if err != nil { panic(err) } defer db.Close() entries, err := exportContentLoader(db) if err != nil { panic(err.Error()) } y, err := yaml.Marshal(&entries) if err != nil { panic(err.Error()) } fmt.Println(string(y)) }