|
@@ -2,12 +2,11 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
- "gopkg.in/yaml.v2"
|
|
|
"os"
|
|
|
"time"
|
|
|
|
|
|
"database/sql"
|
|
|
+
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
)
|
|
|
|
|
@@ -30,38 +29,12 @@ kurzd
|
|
|
schema Uninstall the curz database schema.
|
|
|
status Report on kurzd status
|
|
|
stop Stop all instances of kurzd on the current server (restricted)
|
|
|
- */
|
|
|
+*/
|
|
|
func main() {
|
|
|
- command, subCommand, options, err := parseCommand()
|
|
|
- if err != nil {
|
|
|
- panic(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- switch command {
|
|
|
- case "export":
|
|
|
- switch subCommand {
|
|
|
- case "content":
|
|
|
- dbDriver, dbDsn := parseDbCred(options)
|
|
|
- db, err := dbDial(dbDriver, dbDsn)
|
|
|
- if err != nil {
|
|
|
- panic("Could not open database")
|
|
|
- }
|
|
|
- defer db.Close()
|
|
|
- entries, err := exportContent(db)
|
|
|
- if err != nil {
|
|
|
- panic(err.Error())
|
|
|
- }
|
|
|
- y, err := yaml.Marshal(&entries)
|
|
|
- if err != nil {
|
|
|
- panic(err.Error())
|
|
|
- }
|
|
|
- fmt.Println(string(y))
|
|
|
- }
|
|
|
- }
|
|
|
+ Execute()
|
|
|
+ os.Exit(0)
|
|
|
}
|
|
|
|
|
|
-type stringMap map[string]string
|
|
|
-
|
|
|
type MapEntry struct {
|
|
|
Hash uint64
|
|
|
Url string
|
|
@@ -77,7 +50,7 @@ func dbDial(dbDriver, dbDsn string) (*sql.DB, error) {
|
|
|
return db, nil
|
|
|
}
|
|
|
|
|
|
-func parseDbCred(_ stringMap) (driver, dsn string) {
|
|
|
+func parseDbCred() (driver, dsn string) {
|
|
|
const DefaultDriver = "mysql"
|
|
|
const DefaultDsn = "root:root@tcp(localhost:3306)/kurz"
|
|
|
|
|
@@ -94,38 +67,3 @@ func parseDbCred(_ stringMap) (driver, dsn string) {
|
|
|
|
|
|
return envDriver, envDsn
|
|
|
}
|
|
|
-
|
|
|
-/* parseCommand returns a valid command/subCommand/options triplet and nil, or
|
|
|
- an error, in which case the values for the other results are not defined.
|
|
|
- */
|
|
|
-func parseCommand() (command, subCommand string, options stringMap, err error) {
|
|
|
- return "export", "content", stringMap{}, nil
|
|
|
-}
|
|
|
-
|
|
|
-func exportContent(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
|
|
|
-}
|