Skip to content

Commit

Permalink
chore: log to standard output (#1128)
Browse files Browse the repository at this point in the history
* chore: log to standard output

The logger, accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], writes to standard error and includes the date and time for each logged message.

These logs are not errors, and this change directly affects the redirection of outputs and the creation of log files.

* test: Example test did not add any value

By changing the log redirection, the standard output changed. We do not
need this test.

* chore: create logger redirecting to stdout

We want to avoid reinventing the wheel

* chore: revert changes
  • Loading branch information
zucchinidev authored Oct 31, 2024
1 parent 9d06d4b commit f5b3313
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
9 changes: 4 additions & 5 deletions internal/brokerpak/packer/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package packer
import (
"crypto/md5"
"fmt"
"log"
"os"
"path"
"path/filepath"
Expand All @@ -17,16 +16,16 @@ func cachedFetchFile(getter func(source string, destination string) error, sourc

switch {
case cachePath == "":
log.Println("\t", source, "->", destination, "(no cache)")
logger.Println("\t", source, "->", destination, "(no cache)")
return getter(source, destination)
case exists(source):
log.Println("\t", source, "->", destination, "(local file)")
logger.Println("\t", source, "->", destination, "(local file)")
return copyLocalFile(source, destination)
case cacheDirHasContents(cacheKey):
log.Println("\t", source, "->", destination, "(from cache)")
logger.Println("\t", source, "->", destination, "(from cache)")
return cp.Copy(cacheKey, destination)
default:
log.Println("\t", source, "->", destination, "(stored to cache)")
logger.Println("\t", source, "->", destination, "(stored to cache)")
return getAndCache(getter, source, destination, cacheKey)
}
}
Expand Down
24 changes: 15 additions & 9 deletions internal/brokerpak/packer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ import (
const manifestName = "manifest.yml"
const binaryName = "tofu"

var logger *log.Logger

func init() {
logger = log.New(os.Stdout, "", log.LstdFlags)
}

func Pack(m *manifest.Manifest, base, dest, cachePath string, includeSource, compress bool) error {
// NOTE: we use "log" rather than Lager because this is used by the CLI and
// needs to be human-readable rather than JSON.
switch base {
case "":
log.Printf("Packing brokerpak version %q with CSB version %q...\n", m.Version, utils.Version)
logger.Printf("Packing brokerpak version %q with CSB version %q...\n", m.Version, utils.Version)
default:
log.Printf("Packing %q version %q with CSB version %q...\n", base, m.Version, utils.Version)
logger.Printf("Packing %q version %q with CSB version %q...\n", base, m.Version, utils.Version)
}

dir, err := os.MkdirTemp("", "brokerpak")
Expand All @@ -38,26 +44,26 @@ func Pack(m *manifest.Manifest, base, dest, cachePath string, includeSource, com
defer func(path string) {
_ = os.RemoveAll(path)
}(dir) // clean up
log.Println("Using temp directory:", dir)
logger.Println("Using temp directory:", dir)

if includeSource {
log.Println("Packing sources...")
logger.Println("Packing sources...")
if err := packSources(m, dir, cachePath); err != nil {
return err
}
}

log.Println("Packing binaries...")
logger.Println("Packing binaries...")
if err := packBinaries(m, dir, cachePath); err != nil {
return err
}

log.Println("Packing definitions...")
logger.Println("Packing definitions...")
if err := packDefinitions(m, dir, base); err != nil {
return err
}

log.Println("Creating archive:", dest)
logger.Println("Creating archive:", dest)
return zippy.Archive(dir, dest, compress)
}

Expand All @@ -68,7 +74,7 @@ func packSources(m *manifest.Manifest, tmp string, cachePath string) error {
}
destination := filepath.Join(tmp, "src", name+".zip")

log.Println("\t", source, "->", destination)
logger.Println("\t", source, "->", destination)
return cachedFetchFile(fetcher.FetchArchive, source, destination, cachePath)
}

Expand Down Expand Up @@ -159,7 +165,7 @@ func packDefinitions(m *manifest.Manifest, tmp, base string) error {
clearRefs(&defn.BindSettings)

packedName := fmt.Sprintf("service%d-%s.yml", i, defn.Name)
log.Printf("\t%s/%s -> %s/definitions/%s\n", base, sd, tmp, packedName)
logger.Printf("\t%s/%s -> %s/definitions/%s\n", base, sd, tmp, packedName)
if err := stream.Copy(stream.FromYaml(defn), stream.ToFile(tmp, "definitions", packedName)); err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/brokerpak/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package brokerpak

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -99,21 +98,17 @@ func fakeBrokerpak() (string, error) {
return Pack(dir, "", true, false, platform.Platform{})
}

func ExampleValidate() {
func TestValidate(t *testing.T) {
pk, err := fakeBrokerpak()
defer os.Remove(pk)

if err != nil {
panic(err)
t.Fatal(err)
}

if err := Validate(pk); err != nil {
panic(err)
} else {
fmt.Println("ok!")
t.Fatal(err)
}

// Output: ok!
}

func TestFinfo(t *testing.T) {
Expand Down

0 comments on commit f5b3313

Please sign in to comment.