seed.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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://horus-ctn01.void:10277"
  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 "asset":
  35. b, err := os.ReadFile(pngFile)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. _, fileName := path.Split(pngFile)
  40. item := helpers.Asset{
  41. Name: fileName,
  42. Data: b,
  43. }
  44. data, _ := json.Marshal(item)
  45. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/asset", DEFAULT_URL), bytes.NewReader(data))
  46. req.Header.Add("Content-Type", "application/json")
  47. resp, err := client.Do(req)
  48. if err != nil {
  49. fmt.Println("There was an error performing the desired request: ", err.Error())
  50. os.Exit(1)
  51. }
  52. if resp.StatusCode > 200 {
  53. defer resp.Body.Close()
  54. b, _ := io.ReadAll(resp.Body)
  55. fmt.Println("There was an error performing the desired request: ", string(b))
  56. os.Exit(2)
  57. }
  58. fmt.Println("navigation bar item upload successfully.")
  59. os.Exit(0)
  60. case "png":
  61. fmt.Println(string(pngFile))
  62. b, err := os.ReadFile(pngFile)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. _, fileName := path.Split(pngFile)
  67. fmt.Println(fileName)
  68. item := helpers.NavBarItem{
  69. Link: fileName,
  70. Redirect: redirect,
  71. Png: b,
  72. }
  73. data, _ := json.Marshal(item)
  74. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/navbar", DEFAULT_URL), bytes.NewReader(data))
  75. req.Header.Add("Content-Type", "application/json")
  76. resp, err := client.Do(req)
  77. if err != nil {
  78. fmt.Println("There was an error performing the desired request: ", err.Error())
  79. os.Exit(1)
  80. }
  81. if resp.StatusCode > 200 {
  82. defer resp.Body.Close()
  83. b, _ := io.ReadAll(resp.Body)
  84. fmt.Println("There was an error performing the desired request: ", string(b))
  85. os.Exit(2)
  86. }
  87. fmt.Println("navigation bar item upload successfully.")
  88. os.Exit(0)
  89. case "menu":
  90. b, _ := json.Marshal(helpers.MenuLinkPair{LinkText: text, MenuLink: redirect})
  91. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/menu", DEFAULT_URL), bytes.NewReader(b))
  92. req.Header.Add("Content-Type", "application/json")
  93. resp, err := client.Do(req)
  94. if err != nil {
  95. fmt.Println("There was an error performing the desired request: ", err.Error())
  96. os.Exit(1)
  97. }
  98. if resp.StatusCode > 200 {
  99. defer resp.Body.Close()
  100. b, _ := io.ReadAll(resp.Body)
  101. fmt.Println("There was an error performing the desired request: ", string(b))
  102. os.Exit(3)
  103. }
  104. fmt.Println("menu item uploaded successfully.")
  105. os.Exit(0)
  106. case "admin":
  107. tables := make(map[string][]helpers.TableData)
  108. adminPage := helpers.AdminPage{Tables: tables}
  109. adminPage.Tables[col] = append(adminPage.Tables[col], helpers.TableData{Link: redirect, DisplayName: text})
  110. b, _ := json.Marshal(adminPage)
  111. req, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/admin/panel", DEFAULT_URL), bytes.NewReader(b))
  112. req.Header.Add("Content-Type", "application/json")
  113. resp, err := client.Do(req)
  114. if err != nil {
  115. fmt.Println("There was an error performing the desired request: ", err.Error())
  116. os.Exit(1)
  117. }
  118. if resp.StatusCode > 200 {
  119. defer resp.Body.Close()
  120. b, _ := io.ReadAll(resp.Body)
  121. fmt.Println("There was an error performing the desired request: ", string(b))
  122. os.Exit(4)
  123. }
  124. }
  125. }