From 4fe7d03e54eb09c82d6856aa855bb7097ff4765f Mon Sep 17 00:00:00 2001 From: Yuxiang Zhang Date: Tue, 29 May 2018 18:48:33 +0800 Subject: [PATCH] Move the WebMirrorStatus to internal package. Fix the list command of tunasynctl --- cmd/tunasynctl/tunasynctl.go | 7 +++---- manager/status.go => internal/status_web.go | 12 +++++------- .../status_test.go => internal/status_web_test.go | 10 ++++------ manager/server.go | 4 ++-- 4 files changed, 14 insertions(+), 19 deletions(-) rename manager/status.go => internal/status_web.go (83%) rename manager/status_test.go => internal/status_web_test.go (88%) diff --git a/cmd/tunasynctl/tunasynctl.go b/cmd/tunasynctl/tunasynctl.go index ea12dcf..f335971 100644 --- a/cmd/tunasynctl/tunasynctl.go +++ b/cmd/tunasynctl/tunasynctl.go @@ -140,8 +140,7 @@ func listWorkers(c *cli.Context) error { } func listJobs(c *cli.Context) error { - // FIXME: there should be an API on manager server side that return MirrorStatus list to tunasynctl - var jobs []tunasync.MirrorStatus + var jobs []tunasync.WebMirrorStatus if c.Bool("all") { _, err := tunasync.GetJSON(baseURL+listJobsPath, &jobs, client) if err != nil { @@ -158,10 +157,10 @@ func listJobs(c *cli.Context) error { fmt.Sprintf("Usage Error: jobs command need at"+ " least one arguments or \"--all\" flag."), 1) } - ans := make(chan []tunasync.MirrorStatus, len(args)) + ans := make(chan []tunasync.WebMirrorStatus, len(args)) for _, workerID := range args { go func(workerID string) { - var workerJobs []tunasync.MirrorStatus + var workerJobs []tunasync.WebMirrorStatus _, err := tunasync.GetJSON(fmt.Sprintf("%s/workers/%s/jobs", baseURL, workerID), &workerJobs, client) if err != nil { diff --git a/manager/status.go b/internal/status_web.go similarity index 83% rename from manager/status.go rename to internal/status_web.go index 31bd1d5..33e15ca 100644 --- a/manager/status.go +++ b/internal/status_web.go @@ -1,11 +1,9 @@ -package manager +package internal import ( "encoding/json" "strconv" "time" - - . "github.com/tuna/tunasync/internal" ) type textTime struct { @@ -38,8 +36,8 @@ func (t *stampTime) UnmarshalJSON(b []byte) error { return err } -// webMirrorStatus is the mirror status to be shown in the web page -type webMirrorStatus struct { +// 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"` @@ -49,8 +47,8 @@ type webMirrorStatus struct { Size string `json:"size"` // approximate size } -func convertMirrorStatus(m MirrorStatus) webMirrorStatus { - return webMirrorStatus{ +func BuildWebMirrorStatus(m MirrorStatus) WebMirrorStatus { + return WebMirrorStatus{ Name: m.Name, IsMaster: m.IsMaster, Status: m.Status, diff --git a/manager/status_test.go b/internal/status_web_test.go similarity index 88% rename from manager/status_test.go rename to internal/status_web_test.go index 9cd046a..ce31630 100644 --- a/manager/status_test.go +++ b/internal/status_web_test.go @@ -1,12 +1,10 @@ -package manager +package internal import ( "encoding/json" "testing" "time" - tunasync "github.com/tuna/tunasync/internal" - . "github.com/smartystreets/goconvey/convey" ) @@ -16,9 +14,9 @@ func TestStatus(t *testing.T) { loc, err := time.LoadLocation(tz) So(err, ShouldBeNil) t := time.Date(2016, time.April, 16, 23, 8, 10, 0, loc) - m := webMirrorStatus{ + m := WebMirrorStatus{ Name: "tunalinux", - Status: tunasync.Success, + Status: Success, LastUpdate: textTime{t}, LastUpdateTs: stampTime{t}, Size: "5GB", @@ -28,7 +26,7 @@ func TestStatus(t *testing.T) { b, err := json.Marshal(m) So(err, ShouldBeNil) //fmt.Println(string(b)) - var m2 webMirrorStatus + var m2 WebMirrorStatus err = json.Unmarshal(b, &m2) So(err, ShouldBeNil) // fmt.Printf("%#v", m2) diff --git a/manager/server.go b/manager/server.go index 05c56bf..5953445 100644 --- a/manager/server.go +++ b/manager/server.go @@ -135,11 +135,11 @@ func (s *Manager) listAllJobs(c *gin.Context) { s.returnErrJSON(c, http.StatusInternalServerError, err) return } - webMirStatusList := []webMirrorStatus{} + webMirStatusList := []WebMirrorStatus{} for _, m := range mirrorStatusList { webMirStatusList = append( webMirStatusList, - convertMirrorStatus(m), + BuildWebMirrorStatus(m), ) } c.JSON(http.StatusOK, webMirStatusList)