mirror of
https://github.com/tuna/tunasync.git
synced 2025-04-20 20:22:46 +00:00
refactor: moved mirrorStatus back to manager
This commit is contained in:
parent
96f38363ea
commit
ed69dde18e
66
internal/status.go
Normal file
66
internal/status.go
Normal file
@ -0,0 +1,66 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type SyncStatus uint8
|
||||
|
||||
const (
|
||||
None SyncStatus = iota
|
||||
Failed
|
||||
Success
|
||||
Syncing
|
||||
PreSyncing
|
||||
Paused
|
||||
Disabled
|
||||
)
|
||||
|
||||
func (s SyncStatus) MarshalJSON() ([]byte, error) {
|
||||
var strStatus string
|
||||
switch s {
|
||||
case None:
|
||||
strStatus = "none"
|
||||
case Failed:
|
||||
strStatus = "failed"
|
||||
case Success:
|
||||
strStatus = "success"
|
||||
case Syncing:
|
||||
strStatus = "syncing"
|
||||
case PreSyncing:
|
||||
strStatus = "pre-syncing"
|
||||
case Paused:
|
||||
strStatus = "paused"
|
||||
case Disabled:
|
||||
strStatus = "disabled"
|
||||
default:
|
||||
return []byte{}, errors.New("Invalid status value")
|
||||
}
|
||||
|
||||
return json.Marshal(strStatus)
|
||||
}
|
||||
|
||||
func (s *SyncStatus) UnmarshalJSON(v []byte) error {
|
||||
sv := string(v)
|
||||
switch sv {
|
||||
case `"none"`:
|
||||
*s = None
|
||||
case `"failed"`:
|
||||
*s = Failed
|
||||
case `"success"`:
|
||||
*s = Success
|
||||
case `"syncing"`:
|
||||
*s = Syncing
|
||||
case `"pre-syncing"`:
|
||||
*s = PreSyncing
|
||||
case `"paused"`:
|
||||
*s = Paused
|
||||
case `"disabled"`:
|
||||
*s = Disabled
|
||||
default:
|
||||
return fmt.Errorf("Invalid status value: %s", string(v))
|
||||
}
|
||||
return nil
|
||||
}
|
23
internal/status_test.go
Normal file
23
internal/status_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestSyncStatus(t *testing.T) {
|
||||
Convey("SyncStatus json ser-de should work", t, func() {
|
||||
|
||||
b, err := json.Marshal(PreSyncing)
|
||||
So(err, ShouldBeNil)
|
||||
So(b, ShouldResemble, []byte(`"pre-syncing"`)) // deep equal should be used
|
||||
|
||||
var s SyncStatus
|
||||
|
||||
err = json.Unmarshal([]byte(`"failed"`), &s)
|
||||
So(err, ShouldBeNil)
|
||||
So(s, ShouldEqual, Failed)
|
||||
})
|
||||
}
|
@ -1,35 +1,24 @@
|
||||
package status
|
||||
package manager
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
. "github.com/tuna/tunasync/internal"
|
||||
)
|
||||
|
||||
type syncStatus uint8
|
||||
|
||||
const (
|
||||
None syncStatus = iota
|
||||
Failed
|
||||
Success
|
||||
Syncing
|
||||
PreSyncing
|
||||
Paused
|
||||
Disabled
|
||||
)
|
||||
|
||||
type MirrorStatus struct {
|
||||
type mirrorStatus struct {
|
||||
Name string
|
||||
Status syncStatus
|
||||
Status SyncStatus
|
||||
LastUpdate time.Time
|
||||
Upstream string
|
||||
Size string // approximate size
|
||||
}
|
||||
|
||||
func (s MirrorStatus) MarshalJSON() ([]byte, error) {
|
||||
func (s mirrorStatus) MarshalJSON() ([]byte, error) {
|
||||
m := map[string]interface{}{
|
||||
"name": s.Name,
|
||||
"status": s.Status,
|
||||
@ -41,7 +30,7 @@ func (s MirrorStatus) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func (s *MirrorStatus) UnmarshalJSON(v []byte) error {
|
||||
func (s *mirrorStatus) UnmarshalJSON(v []byte) error {
|
||||
var m map[string]interface{}
|
||||
|
||||
err := json.Unmarshal(v, &m)
|
||||
@ -99,46 +88,3 @@ func (s *MirrorStatus) UnmarshalJSON(v []byte) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s syncStatus) MarshalJSON() ([]byte, error) {
|
||||
var strStatus string
|
||||
switch s {
|
||||
case None:
|
||||
strStatus = "none"
|
||||
case Success:
|
||||
strStatus = "success"
|
||||
case Syncing:
|
||||
strStatus = "syncing"
|
||||
case PreSyncing:
|
||||
strStatus = "pre-syncing"
|
||||
case Paused:
|
||||
strStatus = "paused"
|
||||
case Disabled:
|
||||
strStatus = "disabled"
|
||||
default:
|
||||
return []byte{}, errors.New("Invalid status value")
|
||||
}
|
||||
|
||||
return json.Marshal(strStatus)
|
||||
}
|
||||
|
||||
func (s *syncStatus) UnmarshalJSON(v []byte) error {
|
||||
sv := strings.Trim(string(v), `"`)
|
||||
switch sv {
|
||||
case "none":
|
||||
*s = None
|
||||
case "success":
|
||||
*s = Success
|
||||
case "syncing":
|
||||
*s = Syncing
|
||||
case "pre-syncing":
|
||||
*s = PreSyncing
|
||||
case "paused":
|
||||
*s = Paused
|
||||
case "disabled":
|
||||
*s = Disabled
|
||||
default:
|
||||
return fmt.Errorf("Invalid status value: %s", string(v))
|
||||
}
|
||||
return nil
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
package status
|
||||
package manager
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
tunasync "github.com/tuna/tunasync/internal"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
@ -14,9 +16,9 @@ func TestStatus(t *testing.T) {
|
||||
loc, err := time.LoadLocation(tz)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
m := MirrorStatus{
|
||||
m := mirrorStatus{
|
||||
Name: "tunalinux",
|
||||
Status: Success,
|
||||
Status: tunasync.Success,
|
||||
LastUpdate: time.Date(2016, time.April, 16, 23, 8, 10, 0, loc),
|
||||
Size: "5GB",
|
||||
Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/",
|
||||
@ -25,7 +27,7 @@ func TestStatus(t *testing.T) {
|
||||
b, err := json.Marshal(m)
|
||||
So(err, ShouldBeNil)
|
||||
// fmt.Println(string(b))
|
||||
var m2 MirrorStatus
|
||||
var m2 mirrorStatus
|
||||
err = json.Unmarshal(b, &m2)
|
||||
So(err, ShouldBeNil)
|
||||
// fmt.Printf("%#v", m2)
|
Loading…
x
Reference in New Issue
Block a user