upgrade.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "crypto/sha256"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "os"
  10. "path/filepath"
  11. "runtime"
  12. "strconv"
  13. "time"
  14. "github.com/urfave/cli"
  15. )
  16. type archs struct {
  17. LinuxAmd64 string `json:"linux-amd64"`
  18. DarwinAmd64 string `json:"darwin_amd64"`
  19. }
  20. type internalInfo struct {
  21. version int
  22. maxVersion int
  23. up map[string]archs
  24. }
  25. func upgradeAction(c *cli.Context) error {
  26. upgrade()
  27. return nil
  28. }
  29. func copyFile(src, dst string) error {
  30. in, err := os.Open(src)
  31. if err != nil {
  32. return err
  33. }
  34. defer in.Close()
  35. out, err := os.Create(dst)
  36. if err != nil {
  37. return err
  38. }
  39. defer out.Close()
  40. _, err = io.Copy(out, in)
  41. if err != nil {
  42. return err
  43. }
  44. out.Chmod(0755)
  45. return out.Close()
  46. }
  47. func updateFile(sha, url string) {
  48. ex, err := os.Executable()
  49. if err != nil {
  50. fmt.Printf("fail to get download path")
  51. return
  52. }
  53. fpath := filepath.Dir(ex)
  54. tmpFilepath := fpath + "/kratos.tmp"
  55. // Get the data
  56. resp, err := http.Get(url)
  57. if err != nil {
  58. fmt.Printf("fail to download file: %v", err)
  59. return
  60. }
  61. out, err := os.OpenFile(tmpFilepath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. _, err = io.Copy(out, resp.Body)
  66. if err != nil {
  67. fmt.Println("fail to write download file")
  68. return
  69. }
  70. out.Close()
  71. resp.Body.Close()
  72. f, err := os.Open(tmpFilepath)
  73. if err != nil {
  74. fmt.Println("sha256 fail to open download file")
  75. return
  76. }
  77. h := sha256.New()
  78. _, err = io.Copy(h, f)
  79. if err != nil {
  80. fmt.Println("sha256 fail to init")
  81. return
  82. }
  83. getsha := fmt.Sprintf("%x", h.Sum(nil))
  84. if getsha != sha {
  85. fmt.Printf("sha256 wrong. expect %s get %s", sha, getsha)
  86. return
  87. }
  88. f.Close()
  89. err = copyFile(tmpFilepath, fpath+"/kratos")
  90. if err != nil {
  91. fmt.Println("fail to install kratos")
  92. return
  93. }
  94. err = os.Remove(tmpFilepath)
  95. if err != nil {
  96. fmt.Println("fail to remove tmp kratos")
  97. return
  98. }
  99. fmt.Println("Download successfully!")
  100. }
  101. func upgrade() error {
  102. target := make(map[string]archs)
  103. client := &http.Client{Timeout: 10 * time.Second}
  104. r, err := client.Get("http://bazel-cabin.bilibili.co/kratos/" + Channel + "/package.json")
  105. if err != nil {
  106. return err
  107. }
  108. defer r.Body.Close()
  109. json.NewDecoder(r.Body).Decode(&target)
  110. info := internalInfo{}
  111. info.up = target
  112. if info.version, err = strconv.Atoi(Version); err != nil {
  113. return err
  114. }
  115. for k := range target {
  116. ver, err := strconv.Atoi(k)
  117. if err != nil {
  118. return err
  119. }
  120. if info.maxVersion < ver {
  121. info.maxVersion = ver
  122. }
  123. }
  124. if info.maxVersion > info.version {
  125. fmt.Printf("kratos %d -> %d\n", info.version, info.maxVersion)
  126. switch runtime.GOOS + "-" + runtime.GOARCH {
  127. case "linux-amd64":
  128. updateFile(info.up[strconv.Itoa(info.maxVersion)].LinuxAmd64, "http://bazel-cabin.bilibili.co/kratos/"+Channel+"/"+strconv.Itoa(info.maxVersion)+"/linux-amd64/kratos")
  129. case "darwin-amd64":
  130. updateFile(info.up[strconv.Itoa(info.maxVersion)].DarwinAmd64, "http://bazel-cabin.bilibili.co/kratos/"+Channel+"/"+strconv.Itoa(info.maxVersion)+"/darwin-amd64/kratos")
  131. default:
  132. fmt.Println("not support this operate system")
  133. }
  134. } else {
  135. fmt.Println("Already up to the newest.")
  136. }
  137. return nil
  138. }