seed.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "log"
  9. "net/http"
  10. "os"
  11. "path"
  12. "git.aetherial.dev/aeth/keiji/pkg/helpers"
  13. _ "github.com/mattn/go-sqlite3"
  14. )
  15. const DEFAULT_URL = "http://localhost:8080"
  16. // authenticate and get the cookie needed to make updates
  17. func auth() string {
  18. return ""
  19. }
  20. var pngFile string
  21. var redirect string
  22. var text string
  23. var col string
  24. var cmd string
  25. func main() {
  26. flag.StringVar(&pngFile, "png", "", "The location of the PNG to upload")
  27. flag.StringVar(&redirect, "redirect", "", "the website that the navbar will redirect to")
  28. flag.StringVar(&text, "text", "", "the text to display on the menu item")
  29. flag.StringVar(&col, "col", "", "the column to add/populate the admin table item under")
  30. flag.StringVar(&cmd, "cmd", "", "the 'command' for the seed program to use, currently supports options 'admin', 'menu', and 'png")
  31. flag.Parse()
  32. client := http.Client{}
  33. switch cmd {
  34. case "png":
  35. fmt.Println(string(pngFile))
  36. b, err := os.ReadFile(pngFile)
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. _, fileName := path.Split(pngFile)
  41. fmt.Println(fileName)
  42. item := helpers.NavBarItem{
  43. Link: fileName,
  44. Redirect: redirect,
  45. Png: b,
  46. }
  47. data, _ := json.Marshal(item)
  48. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/navbar", DEFAULT_URL), bytes.NewReader(data))
  49. req.Header.Add("Content-Type", "application/json")
  50. resp, err := client.Do(req)
  51. if err != nil {
  52. fmt.Println("There was an error performing the desired request: ", err.Error())
  53. os.Exit(1)
  54. }
  55. if resp.StatusCode > 200 {
  56. defer resp.Body.Close()
  57. b, _ := io.ReadAll(resp.Body)
  58. fmt.Println("There was an error performing the desired request: ", string(b))
  59. os.Exit(2)
  60. }
  61. fmt.Println("navigation bar item upload successfully.")
  62. os.Exit(0)
  63. case "menu":
  64. b, _ := json.Marshal(helpers.MenuLinkPair{LinkText: text, MenuLink: redirect})
  65. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/menu", DEFAULT_URL), bytes.NewReader(b))
  66. req.Header.Add("Content-Type", "application/json")
  67. resp, err := client.Do(req)
  68. if err != nil {
  69. fmt.Println("There was an error performing the desired request: ", err.Error())
  70. os.Exit(1)
  71. }
  72. if resp.StatusCode > 200 {
  73. defer resp.Body.Close()
  74. b, _ := io.ReadAll(resp.Body)
  75. fmt.Println("There was an error performing the desired request: ", string(b))
  76. os.Exit(3)
  77. }
  78. fmt.Println("menu item uploaded successfully.")
  79. os.Exit(0)
  80. case "admin":
  81. tables := make(map[string][]helpers.TableData)
  82. adminPage := helpers.AdminPage{Tables: tables}
  83. adminPage.Tables[col] = append(adminPage.Tables[col], helpers.TableData{Link: redirect, DisplayName: text})
  84. b, _ := json.Marshal(adminPage)
  85. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/panel", DEFAULT_URL), bytes.NewReader(b))
  86. req.Header.Add("Content-Type", "application/json")
  87. resp, err := client.Do(req)
  88. if err != nil {
  89. fmt.Println("There was an error performing the desired request: ", err.Error())
  90. os.Exit(1)
  91. }
  92. if resp.StatusCode > 200 {
  93. defer resp.Body.Close()
  94. b, _ := io.ReadAll(resp.Body)
  95. fmt.Println("There was an error performing the desired request: ", string(b))
  96. os.Exit(4)
  97. }
  98. }
  99. }