Skip to content

Commit

Permalink
Added io.Writer to Logger config
Browse files Browse the repository at this point in the history
  • Loading branch information
sreeram-venkitesh committed Sep 1, 2024
1 parent eb392e1 commit 6b64b5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
20 changes: 14 additions & 6 deletions pkg/kn/commands/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,29 @@ package logger
import (
"fmt"
"io"
"os"
)

var outWriter io.Writer

type Config struct {
Quiet bool
Out io.Writer
}

func NewLogger(config Config) io.Writer {
func InitLogger(config Config) io.Writer {
if config.Quiet {
return io.Discard
outWriter = io.Discard
} else {
outWriter = config.Out
}

return io.Writer(os.Stdout)
return outWriter
}

func Fprintf(format string, args ...interface{}) {
fmt.Fprintf(outWriter, format, args...)
}

func Log(writer io.Writer, message string) {
fmt.Fprintf(writer, message)
func Fprintln(args ...interface{}) {
fmt.Fprintln(outWriter, args...)
}
13 changes: 7 additions & 6 deletions pkg/kn/commands/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ func NewServiceCreateCommand(p *commands.KnParams) *cobra.Command {
return err
}

out := logger.NewLogger(logger.Config{
out := logger.InitLogger(logger.Config{
Quiet: p.Quiet,
Out: cmd.OutOrStdout(),
})

if serviceExists {
Expand Down Expand Up @@ -192,18 +193,18 @@ func replaceService(ctx context.Context, client clientservingv1.KnServingClient,
return err
}
if !changed {
fmt.Fprintf(out, "Service '%s' replaced in namespace '%s' (unchanged).\n", service.Name, client.Namespace())
logger.Fprintf("Service '%s' replaced in namespace '%s' (unchanged).\n", service.Name, client.Namespace())
return nil
}
return waitIfRequested(ctx, client, waitFlags, service.Name, "Replacing", "replaced", targetFlag, out)
}

func waitIfRequested(ctx context.Context, client clientservingv1.KnServingClient, waitFlags commands.WaitFlags, serviceName string, verbDoing string, verbDone string, targetFlag string, out io.Writer) error {
if !waitFlags.Wait || targetFlag != "" {
logger.Log(out, fmt.Sprintf("Service '%s' %s in namespace '%s'.\n", serviceName, verbDone, client.Namespace()))
logger.Fprintf("Service '%s' %s in namespace '%s'.\n", serviceName, verbDone, client.Namespace())
return nil
}
logger.Log(out, fmt.Sprintf("%s service '%s' in namespace '%s':\n", verbDoing, serviceName, client.Namespace()))
logger.Fprintf("%s service '%s' in namespace '%s':\n", verbDoing, serviceName, client.Namespace())
wconfig := clientservingv1.WaitConfig{
Timeout: time.Duration(waitFlags.TimeoutInSeconds) * time.Second,
ErrorWindow: time.Duration(waitFlags.ErrorWindowInSeconds) * time.Second,
Expand Down Expand Up @@ -242,12 +243,12 @@ func prepareAndUpdateService(ctx context.Context, client clientservingv1.KnServi
}

func waitForServiceToGetReady(ctx context.Context, client clientservingv1.KnServingClient, name string, wconfig clientservingv1.WaitConfig, verbDone string, out io.Writer) error {
logger.Log(out, fmt.Sprintln(""))
logger.Fprintln("")
err := waitForService(ctx, client, name, out, wconfig)
if err != nil {
return err
}
logger.Log(out, fmt.Sprintln(""))
logger.Fprintln("")
return showUrl(ctx, client, name, "", verbDone, out)
}

Expand Down

0 comments on commit 6b64b5e

Please sign in to comment.