Fix: Handle concurrent threads using mutex on the rainbowColor function

Signed-off-by: AhmedGrati <ahmedgrati1999@gmail.com>
This commit is contained in:
AhmedGrati 2023-01-25 17:08:16 +01:00 committed by Nicolas De loof
parent d8bf175cd4
commit df70735295
1 changed files with 4 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package formatter
import (
"fmt"
"strconv"
"sync"
"github.com/docker/compose/v2/pkg/api"
)
@ -88,8 +89,11 @@ func makeColorFunc(code string) colorFunc {
var nextColor = rainbowColor
var rainbow []colorFunc
var currentIndex = 0
var mutex sync.Mutex
func rainbowColor() colorFunc {
mutex.Lock()
defer mutex.Unlock()
result := rainbow[currentIndex]
currentIndex = (currentIndex + 1) % len(rainbow)
return result