kurzd.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Package kurzd contains the Kurz daemon command.
  2. package main
  3. import (
  4. "fmt"
  5. "gopkg.in/yaml.v2"
  6. "os"
  7. "time"
  8. "database/sql"
  9. _ "github.com/go-sql-driver/mysql"
  10. )
  11. /**
  12. kurzd
  13. -v Verbose (default: false).
  14. help Display help.
  15. server Serve kurz (default option).
  16. -p|--port <port_id> Serve on this IP port (default: 80).
  17. -m|--monitoring <port_id> Serve monitoring on this IP port (default: none)
  18. install Install kurzd completely.
  19. config Install the configuration define by command line to ~.
  20. schema Install the kurz database schema.
  21. export Export kurzd data.
  22. -o <file> Specify a destination file.
  23. config Export kurz configuration.
  24. content Export kurz content.
  25. uninstall Uninstall kurzd completely.
  26. config Uninstall the curz configuration file from ~.
  27. schema Uninstall the curz database schema.
  28. status Report on kurzd status
  29. stop Stop all instances of kurzd on the current server (restricted)
  30. */
  31. func main() {
  32. command, subCommand, options, err := parseCommand()
  33. if err != nil {
  34. panic(err.Error())
  35. }
  36. switch command {
  37. case "export":
  38. switch subCommand {
  39. case "content":
  40. dbDriver, dbDsn := parseDbCred(options)
  41. db, err := dbDial(dbDriver, dbDsn)
  42. if err != nil {
  43. panic("Could not open database")
  44. }
  45. defer db.Close()
  46. entries, err := exportContent(db)
  47. if err != nil {
  48. panic(err.Error())
  49. }
  50. y, err := yaml.Marshal(&entries)
  51. if err != nil {
  52. panic(err.Error())
  53. }
  54. fmt.Println(string(y))
  55. }
  56. }
  57. }
  58. type stringMap map[string]string
  59. type MapEntry struct {
  60. Hash uint64
  61. Url string
  62. Date1, Date2, Date3 time.Time
  63. RefCount uint32
  64. }
  65. func dbDial(dbDriver, dbDsn string) (*sql.DB, error) {
  66. db, err := sql.Open(dbDriver, dbDsn)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return db, nil
  71. }
  72. func parseDbCred(_ stringMap) (driver, dsn string) {
  73. const DefaultDriver = "mysql"
  74. const DefaultDsn = "root:root@tcp(localhost:3306)/kurz"
  75. envDriver := os.Getenv("DB_DRIVER")
  76. if envDriver == "" {
  77. envDriver = DefaultDriver
  78. }
  79. envDsn := os.Getenv("DB_DSN")
  80. if envDsn == "" {
  81. envDsn = DefaultDsn
  82. }
  83. envDsn += "?parseTime=true"
  84. return envDriver, envDsn
  85. }
  86. /* parseCommand returns a valid command/subCommand/options triplet and nil, or
  87. an error, in which case the values for the other results are not defined.
  88. */
  89. func parseCommand() (command, subCommand string, options stringMap, err error) {
  90. return "export", "content", stringMap{}, nil
  91. }
  92. func exportContent(db *sql.DB) ([]MapEntry, error) {
  93. stmt, err := db.Prepare(`
  94. SELECT Hash, url, Date1, Date2, Date3, RefCount
  95. FROM map
  96. ORDER BY url`)
  97. if err != nil {
  98. panic(err.Error())
  99. }
  100. defer stmt.Close()
  101. rows, err := stmt.Query()
  102. if err != nil {
  103. return nil, err
  104. }
  105. defer rows.Close()
  106. entry := MapEntry{}
  107. var entries []MapEntry
  108. for rows.Next() {
  109. err = rows.Scan(&entry.Hash, &entry.Url, &entry.Date1, &entry.Date2, &entry.Date3, &entry.RefCount)
  110. if err != nil {
  111. return nil, err
  112. }
  113. entries = append(entries, entry)
  114. }
  115. return entries, nil
  116. }