From b5c8cdaba57777c17021562c13ab96b59c895476 Mon Sep 17 00:00:00 2001 From: Andrea Zucchini Date: Wed, 30 Oct 2024 17:50:24 +0000 Subject: [PATCH] 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. --- internal/brokerpak/manifest/manifest.go | 15 ++++++++------- internal/brokerpak/packer/cache.go | 9 ++++----- internal/brokerpak/packer/pack.go | 21 ++++++++++----------- internal/brokerpak/packer/printer.go | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 internal/brokerpak/packer/printer.go diff --git a/internal/brokerpak/manifest/manifest.go b/internal/brokerpak/manifest/manifest.go index 369215133..cf4cd9308 100644 --- a/internal/brokerpak/manifest/manifest.go +++ b/internal/brokerpak/manifest/manifest.go @@ -10,13 +10,14 @@ import ( // Manifest is the internal model for the brokerpak manifest type Manifest struct { - PackVersion int - Name string - Version string - Metadata map[string]string - Platforms []platform.Platform - TerraformVersions []TerraformVersion - TerraformProviders []TerraformProvider + PackVersion int + Name string + Version string + Metadata map[string]string + Platforms []platform.Platform + TerraformVersions []TerraformVersion + TerraformProviders []TerraformProvider + // TODO - Binaries array will be empty - Remove it - There are not brokerpaks with this configuration - Check internal/brokerpak/manifest/terraform_resource.go:37 Binaries []Binary ServiceDefinitions []string Parameters []Parameter diff --git a/internal/brokerpak/packer/cache.go b/internal/brokerpak/packer/cache.go index 1a9217ac7..a787684c1 100644 --- a/internal/brokerpak/packer/cache.go +++ b/internal/brokerpak/packer/cache.go @@ -3,7 +3,6 @@ package packer import ( "crypto/md5" "fmt" - "log" "os" "path" "path/filepath" @@ -17,16 +16,16 @@ func cachedFetchFile(getter func(source string, destination string) error, sourc switch { case cachePath == "": - log.Println("\t", source, "->", destination, "(no cache)") + Println("\t", source, "->", destination, "(no cache)") return getter(source, destination) case exists(source): - log.Println("\t", source, "->", destination, "(local file)") + Println("\t", source, "->", destination, "(local file)") return copyLocalFile(source, destination) case cacheDirHasContents(cacheKey): - log.Println("\t", source, "->", destination, "(from cache)") + Println("\t", source, "->", destination, "(from cache)") return cp.Copy(cacheKey, destination) default: - log.Println("\t", source, "->", destination, "(stored to cache)") + Println("\t", source, "->", destination, "(stored to cache)") return getAndCache(getter, source, destination, cacheKey) } } diff --git a/internal/brokerpak/packer/pack.go b/internal/brokerpak/packer/pack.go index 00c48a368..374467abc 100644 --- a/internal/brokerpak/packer/pack.go +++ b/internal/brokerpak/packer/pack.go @@ -4,7 +4,6 @@ package packer import ( "fmt" - "log" "os" "path/filepath" @@ -22,13 +21,13 @@ const manifestName = "manifest.yml" const binaryName = "tofu" 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 + // NOTE: we use "fmt" 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) + 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) + Printf("Packing %q version %q with CSB version %q...\n", base, m.Version, utils.Version) } dir, err := os.MkdirTemp("", "brokerpak") @@ -38,26 +37,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) + Println("Using temp directory:", dir) if includeSource { - log.Println("Packing sources...") + Println("Packing sources...") if err := packSources(m, dir, cachePath); err != nil { return err } } - log.Println("Packing binaries...") + Println("Packing binaries...") if err := packBinaries(m, dir, cachePath); err != nil { return err } - log.Println("Packing definitions...") + Println("Packing definitions...") if err := packDefinitions(m, dir, base); err != nil { return err } - log.Println("Creating archive:", dest) + Println("Creating archive:", dest) return zippy.Archive(dir, dest, compress) } @@ -68,7 +67,7 @@ func packSources(m *manifest.Manifest, tmp string, cachePath string) error { } destination := filepath.Join(tmp, "src", name+".zip") - log.Println("\t", source, "->", destination) + Println("\t", source, "->", destination) return cachedFetchFile(fetcher.FetchArchive, source, destination, cachePath) } @@ -159,7 +158,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) + 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 } diff --git a/internal/brokerpak/packer/printer.go b/internal/brokerpak/packer/printer.go new file mode 100644 index 000000000..6ef1cc44d --- /dev/null +++ b/internal/brokerpak/packer/printer.go @@ -0,0 +1,20 @@ +package packer + +import ( + "fmt" + "time" +) + +const layout = "2006/01/02 15:04:05" + +// Println prints a message with the current date and time. +func Println(v ...any) { + timestamp := time.Now().Format(layout) + fmt.Println(append([]any{timestamp}, v...)...) +} + +// Printf prints a formatted message with the current date and time. +func Printf(format string, v ...any) { + timestamp := time.Now().Format(layout) + fmt.Printf("%s "+format, append([]any{timestamp}, v...)...) +}