Skip to content

Commit

Permalink
move iocomb into pkg/log
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <jason@chainguard.dev>
  • Loading branch information
imjasonh committed Jan 11, 2024
1 parent 652c151 commit 932a6b3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 72 deletions.
4 changes: 2 additions & 2 deletions internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"net/http"
"os"

"chainguard.dev/apko/internal/iocomb"
alog "chainguard.dev/apko/pkg/log"
cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd"
"github.com/spf13/cobra"
"sigs.k8s.io/release-utils/version"
Expand All @@ -44,7 +44,7 @@ func New() *cobra.Command {
return fmt.Errorf("failed to change dir to %s: %w", workDir, err)
}
}
out, err := iocomb.Combine(logPolicy)
out, err := alog.Writer(logPolicy)
if err != nil {
return fmt.Errorf("failed to initialize logging: %w", err)
}
Expand Down
70 changes: 0 additions & 70 deletions internal/iocomb/iocomb.go

This file was deleted.

52 changes: 52 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ package log
import (
"context"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)

Expand All @@ -29,6 +33,8 @@ func Warnf(format string, args ...interface{}) { wrap(slog.LevelWarn, format, a
func Errorf(format string, args ...interface{}) { wrap(slog.LevelError, format, args...) }
func Fatalf(format string, args ...interface{}) { log.Fatal(fmt.Sprintf(format, args...)) }

func Printf(format string, args ...interface{}) { wrap(slog.LevelInfo, format, args...) }

// https://pkg.go.dev/log/slog#hdr-Writing_a_handler
func wrap(level slog.Level, format string, args ...interface{}) {
ctx := context.Background()
Expand All @@ -42,3 +48,49 @@ func wrap(level slog.Level, format string, args ...interface{}) {
_ = l.Handler().Handle(ctx, r)
}

// writerFromTarget returns a writer given a target specification.
func writerFromTarget(target string) (io.Writer, error) {
switch target {
case "builtin:stderr":
return os.Stderr, nil
case "builtin:stdout":
return os.Stdout, nil
case "builtin:discard":
return io.Discard, nil
default:
if strings.Contains(target, "/") {
parent := filepath.Dir(target)
if err := os.MkdirAll(parent, 0o755); err != nil {
return nil, err
}
}

log.Println("writing log file to", target)
out, err := os.OpenFile(target, os.O_RDWR|os.O_CREATE, 0o644)
if err != nil {
return nil, err
}

return out, nil
}
}

// Writer returns a writer which writes to multiple target specifications.
func Writer(targets []string) (io.Writer, error) {
writers := []io.Writer{}

if len(targets) == 1 {
return writerFromTarget(targets[0])
}

for _, target := range targets {
writer, err := writerFromTarget(target)
if err != nil {
return nil, err
}

writers = append(writers, writer)
}

return io.MultiWriter(writers...), nil
}

0 comments on commit 932a6b3

Please sign in to comment.