main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "cr-launcher/utils"
  4. "fmt"
  5. "math/rand"
  6. "os/exec"
  7. "time"
  8. "github.com/fatih/color"
  9. "github.com/vbauerster/mpb/v8"
  10. "github.com/vbauerster/mpb/v8/decor"
  11. )
  12. func main() {
  13. // fmt.Println("xxxxxxxxxxxxx")
  14. // http 获取版本信息
  15. // 比较远程版本与本地版本
  16. // http下载文件,更新版本信息
  17. // 解压
  18. // err := utils.WriteConfig(&utils.Config{
  19. // Version: "2.0.0",
  20. // Url: "http://www.baidu.com",
  21. // })
  22. // fmt.Println(err)
  23. // fmt.Println(config)
  24. // fmt.Println(path.Base("http://www.dddfd.com/windows.zip"))
  25. version, err := utils.GetLatest("http://127.0.0.1:8085/cr/version/latest")
  26. config, _ := utils.ReadConfig()
  27. if config == nil {
  28. // fmt.Println(err)
  29. fmt.Println("配置文件错误")
  30. return
  31. }
  32. // 服务器最新版本有变动 更新
  33. if version.Version != config.Version {
  34. // todo
  35. fmt.Println("更新")
  36. }
  37. // 运行应用程序
  38. cmd := exec.Command("data/cr.exe")
  39. data, err := cmd.Output()
  40. if err != nil {
  41. fmt.Println(err)
  42. fmt.Println("启动失败")
  43. return
  44. }
  45. fmt.Println(data)
  46. fmt.Println("启动成功")
  47. fmt.Println(err)
  48. fmt.Println(config)
  49. // to support color in Windows following both options are required
  50. p := mpb.New(
  51. mpb.WithOutput(color.Output),
  52. mpb.WithAutoRefresh(),
  53. )
  54. red, green := color.New(color.FgRed), color.New(color.FgGreen)
  55. fmt.Println("应用更新中...")
  56. task := "windows.zip"
  57. queue := make([]*mpb.Bar, 2)
  58. queue[0] = p.AddBar(rand.Int63n(201)+100,
  59. mpb.PrependDecorators(
  60. decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
  61. decor.Name("下载中:", decor.WCSyncSpaceR),
  62. decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
  63. ),
  64. mpb.AppendDecorators(
  65. decor.OnComplete(decor.Percentage(decor.WC{W: 5}), "done"),
  66. ),
  67. )
  68. queue[1] = p.AddBar(rand.Int63n(101)+100,
  69. mpb.BarQueueAfter(queue[0]), // this bar is queued
  70. mpb.BarFillerClearOnComplete(),
  71. mpb.PrependDecorators(
  72. decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
  73. decor.OnCompleteMeta(
  74. decor.OnComplete(
  75. decor.Meta(decor.Name("安装中:", decor.WCSyncSpaceR), toMetaFunc(red)),
  76. "更新完成!",
  77. ),
  78. toMetaFunc(green),
  79. ),
  80. decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
  81. ),
  82. mpb.AppendDecorators(
  83. decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
  84. ),
  85. )
  86. go func() {
  87. for _, b := range queue {
  88. complete(b)
  89. }
  90. }()
  91. p.Wait()
  92. }
  93. func complete(bar *mpb.Bar) {
  94. max := 100 * time.Millisecond
  95. for !bar.Completed() {
  96. // start variable is solely for EWMA calculation
  97. // EWMA's unit of measure is an iteration's duration
  98. start := time.Now()
  99. time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
  100. // we need to call EwmaIncrement to fulfill ewma decorator's contract
  101. bar.EwmaIncrInt64(rand.Int63n(5)+1, time.Since(start))
  102. }
  103. }
  104. func toMetaFunc(c *color.Color) func(string) string {
  105. return func(s string) string {
  106. return c.Sprint(s)
  107. }
  108. }