Skip to content

Commit

Permalink
chore: fix go mod and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyasdf committed Jul 13, 2023
1 parent f9d91bf commit 75d940b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 91 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/containers/image/v5 v5.25.0
github.com/docker/go-units v0.5.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc2
github.com/sirupsen/logrus v1.9.0
Expand All @@ -25,7 +26,6 @@ require (
github.com/docker/docker v23.0.2+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down
60 changes: 30 additions & 30 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type Client struct {
taskList *concurrent.List
failedTaskList *concurrent.List

urlPairList *concurrent.List
failedGenerateTaskUrlPairList *concurrent.List
generateTaskList *concurrent.List
failedGenerateTaskList *concurrent.List

taskCounter *concurrent.Counter
generateTaskCounter *concurrent.Counter
Expand All @@ -33,14 +33,13 @@ type Client struct {
logger *logrus.Logger
}

// URLPair is a pair of source and destination url
type URLPair struct {
type generateTask struct {
source string
destination string
}

func (u *URLPair) String() string {
return u.source + "->" + u.destination
func (g *generateTask) String() string {
return g.source + "->" + g.destination
}

// NewSyncClient creates a synchronization client
Expand All @@ -57,18 +56,18 @@ func NewSyncClient(configFile, authFile, imageFile, logFile string,
}

return &Client{
taskList: concurrent.NewList(),
urlPairList: concurrent.NewList(),
failedTaskList: concurrent.NewList(),
failedGenerateTaskUrlPairList: concurrent.NewList(),
generateTaskCounter: concurrent.NewCounter(0, 0),
failedGenerateTaskCounter: concurrent.NewCounter(0, 0),
taskCounter: concurrent.NewCounter(0, 0),
failedTaskCounter: concurrent.NewCounter(0, 0),
config: config,
routineNum: routineNum,
retries: retries,
logger: logger,
taskList: concurrent.NewList(),
generateTaskList: concurrent.NewList(),
failedTaskList: concurrent.NewList(),
failedGenerateTaskList: concurrent.NewList(),
generateTaskCounter: concurrent.NewCounter(0, 0),
failedGenerateTaskCounter: concurrent.NewCounter(0, 0),
taskCounter: concurrent.NewCounter(0, 0),
failedTaskCounter: concurrent.NewCounter(0, 0),
config: config,
routineNum: routineNum,
retries: retries,
logger: logger,
}, nil
}

Expand All @@ -77,7 +76,7 @@ func (c *Client) Run() error {
c.logger.Infof("Start to generate sync tasks, please wait ...")

for source, dest := range c.config.GetImageList() {
c.urlPairList.PushBack(&URLPair{
c.generateTaskList.PushBack(&generateTask{
source: source,
destination: dest,
})
Expand All @@ -97,9 +96,9 @@ func (c *Client) Run() error {
c.generateTaskCounter, c.failedGenerateTaskCounter = c.failedGenerateTaskCounter,
concurrent.NewCounter(0, 0)

if c.failedGenerateTaskUrlPairList.Len() != 0 {
c.urlPairList.PushBackList(c.failedGenerateTaskUrlPairList)
c.failedGenerateTaskUrlPairList.Reset()
if c.failedGenerateTaskList.Len() != 0 {
c.generateTaskList.PushBackList(c.failedGenerateTaskList)
c.failedGenerateTaskList.Reset()

// retry to generate task
c.logger.Infof("Start to retry to generate sync tasks, please wait ...")
Expand All @@ -119,7 +118,7 @@ func (c *Client) Run() error {
}

endMsg := fmt.Sprintf("Finished, %v tasks failed, %v rules failed to be generated to tasks",
c.failedTaskList.Len(), c.failedGenerateTaskUrlPairList.Len())
c.failedTaskList.Len(), c.failedGenerateTaskList.Len())

c.logger.Infof(utils.Green(endMsg))

Expand All @@ -135,28 +134,29 @@ func (c *Client) Run() error {
func (c *Client) openRoutinesGenTaskAndWaitForFinish() {
concurrent.CreateRoutinesAndWaitForFinish(c.routineNum, func() {
for {
urlPair := c.urlPairList.PopFront()
gTask := c.generateTaskList.PopFront()
// no more task to generate
if urlPair == nil {
if gTask == nil {
break
}
genTask := gTask.(*generateTask)

c.logger.Infof("Generating tasks for %v...", urlPair.(*URLPair).String())
c.logger.Infof("Generating tasks for %v...", genTask.String())

if err := c.GenerateSyncTasks(urlPair.(*URLPair).source, urlPair.(*URLPair).destination); err != nil {
if err := c.GenerateSyncTasks(genTask.source, genTask.destination); err != nil {
c.logger.Errorf("Generate sync task %s error: %v",
urlPair.(*URLPair).String(), err)
genTask.String(), err)

// put to failedTaskGenerateList
c.failedGenerateTaskUrlPairList.PushBack(urlPair)
c.failedGenerateTaskList.PushBack(gTask)
c.failedGenerateTaskCounter.IncreaseTotal()
}

count, total := c.generateTaskCounter.Increase()
finishedNumString := utils.Green(fmt.Sprintf("%d", count))
totalNumString := utils.Green(fmt.Sprintf("%d", total))

c.logger.Infof("Finish generating tasks for %v, %v/%v generate tasks executed", urlPair.(*URLPair).String(),
c.logger.Infof("Finish generating tasks for %v, %v/%v generate tasks executed", genTask.String(),
finishedNumString, totalNumString)
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/task/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func (b *BlobTask) Run() (Task, string, error) {
return nil, resultMsg, fmt.Errorf("failed to put blob %s(%v): %v", b.info.Digest, b.info.Size, err)
}
} else {
resultMsg = fmt.Sprintf("ignore exist blob")
resultMsg = "ignore exist blob"
}

if b.primary.ReleaseOnce() {
resultMsg = fmt.Sprintf("start to sync manifest")
resultMsg = "start to sync manifest"
return b.primary, resultMsg, nil
}
return nil, resultMsg, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/task/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (m *ManifestTask) Run() (Task, string, error) {
}

if m.primary.ReleaseOnce() {
resultMsg = fmt.Sprintf("start to sync manifest list")
resultMsg = "start to sync manifest list"
return m.primary, resultMsg, nil
}
return nil, resultMsg, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/color_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func Brown(str string, modifier ...interface{}) string {
}

func cliColorRender(str string, color int, weight int, extraArgs ...interface{}) string {
var isBlink int64 = 0
var isBlink int64
if len(extraArgs) > 0 {
isBlink = reflect.ValueOf(extraArgs[0]).Int()
}

var isUnderLine int64 = 0
var isUnderLine int64
if len(extraArgs) > 1 {
isUnderLine = reflect.ValueOf(extraArgs[1]).Int()
}
Expand All @@ -97,6 +97,6 @@ func cliColorRender(str string, color int, weight int, extraArgs ...interface{})
if len(mo) <= 0 {
mo = append(mo, "0")
}

return fmt.Sprintf("\033[%s;%dm"+str+"\033[0m", strings.Join(mo, ";"), color)
}
104 changes: 50 additions & 54 deletions pkg/utils/color_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,56 @@ import (
"testing"
)

func TestColorCli(t *testing.T) {
//默认的不带任何效果的字体显示
fmt.Println(Green("字体:Green"))
fmt.Println(LightGreen("字体:LightGreen"))
fmt.Println(Cyan("字体:Cyan"))
fmt.Println(LightCyan("字体:LightCyan"))
fmt.Println(Red("字体:Red"))
fmt.Println(LightRed("字体:LightRed"))
fmt.Println(Yellow("字体:Yellow"))
fmt.Println(Black("字体:Black"))
fmt.Println(DarkGray("字体:DarkGray"))
fmt.Println(LightGray("字体:LightGray"))
fmt.Println(White("字体:White"))
fmt.Println(Blue("字体:Blue"))
fmt.Println(LightBlue("字体:LightBlue"))
fmt.Println(Purple("字体:Purple"))
fmt.Println(LightPurple("字体:LightPurple"))
fmt.Println(Brown("字体:Brown"))
fmt.Println(Blue("字体:Blue", 1, 1))
func TestColorCli(_ *testing.T) {
fmt.Println(Green("Green"))
fmt.Println(LightGreen("LightGreen"))
fmt.Println(Cyan("Cyan"))
fmt.Println(LightCyan("LightCyan"))
fmt.Println(Red("Red"))
fmt.Println(LightRed("LightRed"))
fmt.Println(Yellow("Yellow"))
fmt.Println(Black("Black"))
fmt.Println(DarkGray("DarkGray"))
fmt.Println(LightGray("LightGray"))
fmt.Println(White("White"))
fmt.Println(Blue("Blue"))
fmt.Println(LightBlue("LightBlue"))
fmt.Println(Purple("Purple"))
fmt.Println(LightPurple("LightPurple"))
fmt.Println(Brown("Brown"))
fmt.Println(Blue("Blue", 1, 1))

//带闪烁效果的彩色字体显示
fmt.Println(Green("闪烁字体:Green", 1, 1))
fmt.Println(LightGreen("闪烁字体:LightGreen", 1))
fmt.Println(Cyan("闪烁字体:Cyan", 1))
fmt.Println(LightCyan("闪烁字体:LightCyan", 1))
fmt.Println(Red("闪烁字体:Red", 1))
fmt.Println(LightRed("闪烁字体:LightRed", 1))
fmt.Println(Yellow("闪烁字体:Yellow", 1))
fmt.Println(Black("闪烁字体:Black", 1))
fmt.Println(DarkGray("闪烁字体:DarkGray", 1))
fmt.Println(LightGray("闪烁字体:LightGray", 1))
fmt.Println(White("闪烁字体:White", 1))
fmt.Println(Blue("闪烁字体:Blue", 1))
fmt.Println(LightBlue("闪烁字体:LightBlue", 1))
fmt.Println(Purple("闪烁字体:Purple", 1))
fmt.Println(LightPurple("闪烁字体:LightPurple", 1))
fmt.Println(Brown("闪烁字体:Brown", 1))

//带下划线效果的字体显示
fmt.Println(Green("闪烁且带下划线字体:Green", 1, 1, 1))
fmt.Println(LightGreen("闪烁且带下划线字体:LightGreen", 1, 1))
fmt.Println(Cyan("闪烁且带下划线字体:Cyan", 1, 1))
fmt.Println(LightCyan("闪烁且带下划线字体:LightCyan", 1, 1))
fmt.Println(Red("闪烁且带下划线字体:Red", 1, 1))
fmt.Println(LightRed("闪烁且带下划线字体:LightRed", 1, 1))
fmt.Println(Yellow("闪烁且带下划线字体:Yellow", 1, 1))
fmt.Println(Black("闪烁且带下划线字体:Black", 1, 1))
fmt.Println(DarkGray("闪烁且带下划线字体:DarkGray", 1, 1))
fmt.Println(LightGray("闪烁且带下划线字体:LightGray", 1, 1))
fmt.Println(White("闪烁且带下划线字体:White", 1, 1))
fmt.Println(Blue("闪烁且带下划线字体:Blue", 1, 1))
fmt.Println(LightBlue("闪烁且带下划线字体:LightBlue", 1, 1))
fmt.Println(Purple("闪烁且带下划线字体:Purple", 1, 1))
fmt.Println(LightPurple("闪烁且带下划线字体:LightPurple", 1, 1))
fmt.Println(Brown("闪烁且带下划线字体:Brown", 1, 1))
fmt.Println(Green("Blink Green", 1, 1))
fmt.Println(LightGreen("Blink LightGreen", 1))
fmt.Println(Cyan("Blink Cyan", 1))
fmt.Println(LightCyan("Blink LightCyan", 1))
fmt.Println(Red("Blink Red", 1))
fmt.Println(LightRed("Blink LightRed", 1))
fmt.Println(Yellow("Blink Yellow", 1))
fmt.Println(Black("Blink Black", 1))
fmt.Println(DarkGray("Blink DarkGray", 1))
fmt.Println(LightGray("Blink LightGray", 1))
fmt.Println(White("Blink White", 1))
fmt.Println(Blue("Blink Blue", 1))
fmt.Println(LightBlue("Blink LightBlue", 1))
fmt.Println(Purple("Blink Purple", 1))
fmt.Println(LightPurple("Blink LightPurple", 1))
fmt.Println(Brown("Blink Brown", 1))

fmt.Println(Green("Blink & Underline Green", 1, 1, 1))
fmt.Println(LightGreen("Blink & Underline LightGreen", 1, 1))
fmt.Println(Cyan("Blink & Underline Cyan", 1, 1))
fmt.Println(LightCyan("Blink & Underline LightCyan", 1, 1))
fmt.Println(Red("Blink & Underline Red", 1, 1))
fmt.Println(LightRed("Blink & Underline LightRed", 1, 1))
fmt.Println(Yellow("Blink & Underline Yellow", 1, 1))
fmt.Println(Black("Blink & Underline Black", 1, 1))
fmt.Println(DarkGray("Blink & Underline DarkGray", 1, 1))
fmt.Println(LightGray("Blink & Underline LightGray", 1, 1))
fmt.Println(White("Blink & Underline White", 1, 1))
fmt.Println(Blue("Blink & Underline Blue", 1, 1))
fmt.Println(LightBlue("Blink & Underline LightBlue", 1, 1))
fmt.Println(Purple("Blink & Underline Purple", 1, 1))
fmt.Println(LightPurple("Blink & Underline LightPurple", 1, 1))
fmt.Println(Brown("Blink & Underline Brown", 1, 1))
}

0 comments on commit 75d940b

Please sign in to comment.