镜像自地址
https://github.com/tuna/tunasync.git
已同步 2025-12-06 06:26:46 +00:00
Move the WebMirrorStatus to internal package. Fix the list command of tunasynctl
这个提交包含在:
60
internal/status_web.go
普通文件
60
internal/status_web.go
普通文件
@@ -0,0 +1,60 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type textTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t textTime) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.Format("2006-01-02 15:04:05 -0700"))
|
||||
}
|
||||
func (t *textTime) UnmarshalJSON(b []byte) error {
|
||||
s := string(b)
|
||||
t2, err := time.Parse(`"2006-01-02 15:04:05 -0700"`, s)
|
||||
*t = textTime{t2}
|
||||
return err
|
||||
}
|
||||
|
||||
type stampTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t stampTime) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.Unix())
|
||||
}
|
||||
func (t *stampTime) UnmarshalJSON(b []byte) error {
|
||||
ts, err := strconv.Atoi(string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = stampTime{time.Unix(int64(ts), 0)}
|
||||
return err
|
||||
}
|
||||
|
||||
// WebMirrorStatus is the mirror status to be shown in the web page
|
||||
type WebMirrorStatus struct {
|
||||
Name string `json:"name"`
|
||||
IsMaster bool `json:"is_master"`
|
||||
Status SyncStatus `json:"status"`
|
||||
LastUpdate textTime `json:"last_update"`
|
||||
LastUpdateTs stampTime `json:"last_update_ts"`
|
||||
Upstream string `json:"upstream"`
|
||||
Size string `json:"size"` // approximate size
|
||||
}
|
||||
|
||||
func BuildWebMirrorStatus(m MirrorStatus) WebMirrorStatus {
|
||||
return WebMirrorStatus{
|
||||
Name: m.Name,
|
||||
IsMaster: m.IsMaster,
|
||||
Status: m.Status,
|
||||
LastUpdate: textTime{m.LastUpdate},
|
||||
LastUpdateTs: stampTime{m.LastUpdate},
|
||||
Upstream: m.Upstream,
|
||||
Size: m.Size,
|
||||
}
|
||||
}
|
||||
42
internal/status_web_test.go
普通文件
42
internal/status_web_test.go
普通文件
@@ -0,0 +1,42 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestStatus(t *testing.T) {
|
||||
Convey("status json ser-de should work", t, func() {
|
||||
tz := "Asia/Tokyo"
|
||||
loc, err := time.LoadLocation(tz)
|
||||
So(err, ShouldBeNil)
|
||||
t := time.Date(2016, time.April, 16, 23, 8, 10, 0, loc)
|
||||
m := WebMirrorStatus{
|
||||
Name: "tunalinux",
|
||||
Status: Success,
|
||||
LastUpdate: textTime{t},
|
||||
LastUpdateTs: stampTime{t},
|
||||
Size: "5GB",
|
||||
Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/",
|
||||
}
|
||||
|
||||
b, err := json.Marshal(m)
|
||||
So(err, ShouldBeNil)
|
||||
//fmt.Println(string(b))
|
||||
var m2 WebMirrorStatus
|
||||
err = json.Unmarshal(b, &m2)
|
||||
So(err, ShouldBeNil)
|
||||
// fmt.Printf("%#v", m2)
|
||||
So(m2.Name, ShouldEqual, m.Name)
|
||||
So(m2.Status, ShouldEqual, m.Status)
|
||||
So(m2.LastUpdate.Unix(), ShouldEqual, m.LastUpdate.Unix())
|
||||
So(m2.LastUpdateTs.Unix(), ShouldEqual, m.LastUpdate.Unix())
|
||||
So(m2.LastUpdate.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano())
|
||||
So(m2.LastUpdateTs.UnixNano(), ShouldEqual, m.LastUpdate.UnixNano())
|
||||
So(m2.Size, ShouldEqual, m.Size)
|
||||
So(m2.Upstream, ShouldEqual, m.Upstream)
|
||||
})
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户