Skip to content

Commit 272245f

Browse files
committed
Use Logger interface.
1 parent 8bd2aff commit 272245f

9 files changed

Lines changed: 65 additions & 41 deletions

File tree

cmd/deploy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Examples:
4343
}
4444

4545
func runDeploy(cmd *cobra.Command, args []string) error {
46-
log := logger.New()
46+
log := logger.NewWithTimestamps()
4747

4848
if env.RunningInContainer {
4949
log.Dim("Running containerized.")
@@ -156,7 +156,7 @@ func runDeploy(cmd *cobra.Command, args []string) error {
156156
}
157157

158158
// resolveAutoResources determines the appropriate resource tier based on cluster type
159-
func resolveAutoResources(clusterType env.ClusterType, log *logger.Logger) string {
159+
func resolveAutoResources(clusterType env.ClusterType, log logger.Logger) string {
160160
var resolvedResources string
161161

162162
switch clusterType {

cmd/subshell.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/stackrox/roxie/internal/logger"
1414
)
1515

16-
func spawnSubshell(d *deployer.Deployer, log *logger.Logger) error {
16+
func spawnSubshell(d *deployer.Deployer, log logger.Logger) error {
1717
shellPath := shell
1818
if shellPath == "" {
1919
shellPath = os.Getenv("ROXIE_USER_SHELL")
@@ -98,7 +98,7 @@ func spawnSubshell(d *deployer.Deployer, log *logger.Logger) error {
9898
return nil
9999
}
100100

101-
func startHAProxy(endpoint, caCertFile string, log *logger.Logger) (*exec.Cmd, string, error) {
101+
func startHAProxy(endpoint, caCertFile string, log logger.Logger) (*exec.Cmd, string, error) {
102102
configFile, err := os.CreateTemp("", "roxie-haproxy-*.cfg")
103103
if err != nil {
104104
return nil, "", fmt.Errorf("failed to create temp config: %w", err)

cmd/teardown.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func runTeardown(cmd *cobra.Command, args []string) error {
3131
component = args[0]
3232
}
3333

34-
log := logger.New()
34+
log := logger.NewWithTimestamps()
3535

3636
log.Infof("Tearing down %s", component)
3737

internal/clusterdefaults/clusterdefaults.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ type Applicator interface {
6262
type Manager struct {
6363
detector Detector
6464
applicator Applicator
65-
logger *logger.Logger
65+
logger logger.Logger
6666
}
6767

6868
// NewManager creates a new cluster defaults manager
69-
func NewManager(log *logger.Logger) *Manager {
69+
func NewManager(log logger.Logger) *Manager {
7070
return &Manager{
7171
detector: &defaultDetector{},
7272
applicator: &defaultApplicator{},

internal/deployer/deployer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929

3030
// Deployer is the base deployer for ACS
3131
type Deployer struct {
32-
logger *logger.Logger
32+
logger logger.Logger
3333
startTime time.Time
3434
dockerAuth *dockerauth.DockerAuth
3535
imageCache *imagecache.ImageCache
@@ -57,7 +57,7 @@ type Deployer struct {
5757
dockerCreds *dockerauth.Credentials
5858
}
5959

60-
func New(log *logger.Logger, overrideFile string, overrideSetExpressions []string) (*Deployer, error) {
60+
func New(log logger.Logger, overrideFile string, overrideSetExpressions []string) (*Deployer, error) {
6161
// Check required tools first
6262
if err := checkRequiredTools(); err != nil {
6363
return nil, err

internal/dockerauth/dockerauth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const (
1919

2020
// DockerAuth handles Docker authentication and pull secret management.
2121
type DockerAuth struct {
22-
logger *logger.Logger
22+
logger logger.Logger
2323
skipCredVerification bool
2424
}
2525

@@ -48,7 +48,7 @@ type Credentials struct {
4848
}
4949

5050
// New creates a new DockerAuth instance.
51-
func New(log *logger.Logger) *DockerAuth {
51+
func New(log logger.Logger) *DockerAuth {
5252
return &DockerAuth{
5353
logger: log,
5454
}

internal/imagecache/imagecache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type ImageCache struct {
1616
cacheFile string
1717
maxEntries int
1818
cache []string
19-
logger *logger.Logger
19+
logger logger.Logger
2020
mu sync.Mutex
2121
}
2222

@@ -26,7 +26,7 @@ type CacheData struct {
2626
}
2727

2828
// New creates a new ImageCache instance
29-
func New(log *logger.Logger, cacheFile string, maxEntries int) *ImageCache {
29+
func New(log logger.Logger, cacheFile string, maxEntries int) *ImageCache {
3030
if cacheFile == "" {
3131
home, err := os.UserHomeDir()
3232
if err != nil {

internal/logger/logger.go

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,86 +9,110 @@ import (
99
"github.com/fatih/color"
1010
)
1111

12-
// Logger provides timestamped logging functionality
13-
type Logger struct {
14-
startTime time.Time
15-
stdout io.Writer
16-
stderr io.Writer
12+
// Logger provides logging functionality.
13+
type Logger interface {
14+
Info(message string)
15+
Infof(format string, args ...interface{})
16+
Error(message string)
17+
Errorf(format string, args ...interface{})
18+
Success(message string)
19+
Successf(format string, args ...interface{})
20+
Warning(message string)
21+
Warningf(format string, args ...interface{})
22+
Dim(message string)
23+
Dimf(format string, args ...interface{})
1724
}
1825

19-
// New creates a new Logger instance
20-
func New() *Logger {
21-
return &Logger{
26+
type LoggerImpl struct {
27+
startTime time.Time
28+
stdout io.Writer
29+
stderr io.Writer
30+
withTimestamps bool
31+
}
32+
33+
// New creates a new Logger instance.
34+
func New() *LoggerImpl {
35+
return &LoggerImpl{
2236
startTime: time.Now(),
2337
stdout: os.Stdout,
2438
stderr: os.Stderr,
2539
}
2640
}
2741

28-
// getTimestamp returns the elapsed time since logger creation in MM:SS format
29-
func (l *Logger) getTimestamp() string {
42+
// NewWithTimestamps creates a new Logger instance with timestamps.
43+
func NewWithTimestamps() *LoggerImpl {
44+
logger := New()
45+
logger.withTimestamps = true
46+
return logger
47+
}
48+
49+
// getTimestamp returns the elapsed time since logger creation in MM:SS format.
50+
func (l *LoggerImpl) getTimestamp() string {
51+
if !l.withTimestamps {
52+
return ""
53+
}
3054
elapsed := time.Since(l.startTime)
3155
minutes := int(elapsed.Minutes())
3256
seconds := int(elapsed.Seconds()) % 60
33-
return fmt.Sprintf("%02d:%02d", minutes, seconds)
57+
return fmt.Sprintf("%02d:%02d ", minutes, seconds)
3458
}
3559

3660
// Info prints an info message with magenta styling
37-
func (l *Logger) Info(message string) {
61+
func (l *LoggerImpl) Info(message string) {
3862
timestamp := color.GreenString(l.getTimestamp())
3963
info := color.New(color.FgMagenta, color.Bold).Sprint(message)
40-
fmt.Fprintf(l.stdout, "%s %s\n", timestamp, info)
64+
fmt.Fprintf(l.stdout, "%s%s\n", timestamp, info)
4165
}
4266

4367
// Infof prints a formatted info message with magenta styling
44-
func (l *Logger) Infof(format string, args ...interface{}) {
68+
func (l *LoggerImpl) Infof(format string, args ...interface{}) {
4569
l.Info(fmt.Sprintf(format, args...))
4670
}
4771

4872
// Error prints an error message with red styling to stderr
49-
func (l *Logger) Error(message string) {
73+
func (l *LoggerImpl) Error(message string) {
5074
timestamp := color.GreenString(l.getTimestamp())
5175
errMsg := color.New(color.FgRed, color.Bold).Sprint(message)
52-
fmt.Fprintf(l.stderr, "%s %s\n", timestamp, errMsg)
76+
fmt.Fprintf(l.stderr, "%s%s\n", timestamp, errMsg)
5377
}
5478

5579
// Errorf prints a formatted error message with red styling to stderr
56-
func (l *Logger) Errorf(format string, args ...interface{}) {
80+
func (l *LoggerImpl) Errorf(format string, args ...interface{}) {
5781
l.Error(fmt.Sprintf(format, args...))
5882
}
5983

6084
// Success prints a success message with green styling
61-
func (l *Logger) Success(message string) {
85+
func (l *LoggerImpl) Success(message string) {
6286
timestamp := color.GreenString(l.getTimestamp())
6387
success := color.New(color.FgGreen, color.Bold).Sprint(message)
64-
fmt.Fprintf(l.stdout, "%s %s\n", timestamp, success)
88+
fmt.Fprintf(l.stdout, "%s%s\n", timestamp, success)
6589
}
6690

6791
// Successf prints a formatted success message with green styling
68-
func (l *Logger) Successf(format string, args ...interface{}) {
92+
func (l *LoggerImpl) Successf(format string, args ...interface{}) {
6993
l.Success(fmt.Sprintf(format, args...))
7094
}
7195

7296
// Warning prints a warning message with yellow styling
73-
func (l *Logger) Warning(message string) {
97+
func (l *LoggerImpl) Warning(message string) {
7498
timestamp := color.GreenString(l.getTimestamp())
7599
warning := color.New(color.FgYellow, color.Bold).Sprint(message)
76-
fmt.Fprintf(l.stdout, "%s %s\n", timestamp, warning)
100+
fmt.Fprintf(l.stdout, "%s%s\n", timestamp, warning)
77101
}
78102

79103
// Warning prints a formatted warning message with yellow styling
80-
func (l *Logger) Warningf(format string, args ...interface{}) {
104+
func (l *LoggerImpl) Warningf(format string, args ...interface{}) {
81105
l.Warning(fmt.Sprintf(format, args...))
82106
}
83107

84108
// Dim prints a dimmed message
85-
func (l *Logger) Dim(message string) {
109+
func (l *LoggerImpl) Dim(message string) {
86110
timestamp := color.GreenString(l.getTimestamp())
87111
dim := color.New(color.Faint).Sprint(message)
88-
fmt.Fprintf(l.stdout, "%s %s\n", timestamp, dim)
112+
fmt.Fprintf(l.stdout, "%s%s\n", timestamp, dim)
89113
}
90114

91115
// Dimf prints a formatted dimmed message
92-
func (l *Logger) Dimf(format string, args ...interface{}) {
116+
func (l *LoggerImpl) Dimf(format string, args ...interface{}) {
93117
l.Dim(fmt.Sprintf(format, args...))
94118
}

internal/portforward/portforward.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
// Manager manages a kubectl port-forward subprocess and exposes a localhost endpoint
1414
type Manager struct {
1515
kubectl string
16-
logger *logger.Logger
16+
logger logger.Logger
1717
proc *exec.Cmd
1818
localPort int
1919
}
2020

2121
// New creates a new PortForwardManager
22-
func New(kubectl string, log *logger.Logger) *Manager {
22+
func New(kubectl string, log logger.Logger) *Manager {
2323
return &Manager{
2424
kubectl: kubectl,
2525
logger: log,

0 commit comments

Comments
 (0)