logging: Replace functions by native equivalents

This commit is contained in:
Tom Wiesing 2023-03-13 13:19:32 +01:00
parent 4fc937841a
commit d7847b5d69
No known key found for this signature in database
10 changed files with 28 additions and 40 deletions

View file

@ -7,7 +7,6 @@ import (
wisski_distillery "github.com/FAU-CDI/wisski-distillery" wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/internal/cli" "github.com/FAU-CDI/wisski-distillery/internal/cli"
"github.com/FAU-CDI/wisski-distillery/internal/wisski" "github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/status" "github.com/tkw1536/goprogram/status"
@ -46,7 +45,7 @@ func (upc updateprefixconfig) Run(context wisski_distillery.Context) (err error)
} }
return status.WriterGroup(context.Stderr, upc.Parallel, func(instance *wisski.WissKI, writer io.Writer) error { return status.WriterGroup(context.Stderr, upc.Parallel, func(instance *wisski.WissKI, writer io.Writer) error {
logging.Progress(writer, context.Context, "reading prefixes") io.WriteString(writer, "reading prefixes")
err := instance.Prefixes().Update(context.Context) err := instance.Prefixes().Update(context.Context)
if err != nil { if err != nil {
return err return err

View file

@ -7,7 +7,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx" "github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -90,7 +89,7 @@ func (bc *stagingContext) sendPath(path string) {
return return
} }
logging.Progress(bc.progress, bc.ctx, dst) io.WriteString(bc.progress, dst)
bc.manifest <- dst bc.manifest <- dst
} }

View file

@ -3,6 +3,7 @@ package exporter
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"io/fs" "io/fs"
"os" "os"
@ -71,8 +72,8 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
if !task.StagingOnly && archivePath == "" { if !task.StagingOnly && archivePath == "" {
archivePath = exporter.NewArchivePath(Slug) archivePath = exporter.NewArchivePath(Slug)
} }
logging.ProgressF(progress, ctx, "Staging Directory: %s\n", stagingDir) fmt.Fprintf(progress, "Staging Directory: %s\n", stagingDir)
logging.ProgressF(progress, ctx, "Archive Path: %s\n", archivePath) fmt.Fprintf(progress, "Archive Path: %s\n", archivePath)
// create the staging directory // create the staging directory
logging.LogMessage(progress, ctx, "Creating staging directory") logging.LogMessage(progress, ctx, "Creating staging directory")
@ -111,7 +112,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
// find the report path // find the report path
reportPath := filepath.Join(stagingDir, "report.txt") reportPath := filepath.Join(stagingDir, "report.txt")
logging.ProgressF(progress, ctx, reportPath) fmt.Fprintln(progress, reportPath)
// create the path // create the path
report, err := fsx.Create(reportPath, fsx.DefaultFilePerm) report, err := fsx.Create(reportPath, fsx.DefaultFilePerm)
@ -129,21 +130,21 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
// if we only requested staging // if we only requested staging
// all that is left is to write the log entry // all that is left is to write the log entry
if task.StagingOnly { if task.StagingOnly {
logging.LogMessage(progress, ctx, "Writing Log Entry") fmt.Fprintln(progress, "Writing Log Entry")
// write out the log entry // write out the log entry
entry.Path = stagingDir entry.Path = stagingDir
entry.Packed = false entry.Packed = false
exporter.Dependencies.ExporterLogger.Add(ctx, entry) exporter.Dependencies.ExporterLogger.Add(ctx, entry)
logging.ProgressF(progress, ctx, "Wrote %s\n", stagingDir) fmt.Fprintf(progress, "Wrote %s\n", stagingDir)
return nil return nil
} }
// package everything up as an archive! // package everything up as an archive!
if err := logging.LogOperation(func() error { if err := logging.LogOperation(func() error {
var count int64 var count int64
defer func() { logging.ProgressF(progress, ctx, "Wrote %d byte(s) to %s\n", count, archivePath) }() defer func() { fmt.Fprintf(progress, "Wrote %d byte(s) to %s\n", count, archivePath) }()
st := status.NewWithCompat(progress, 1) st := status.NewWithCompat(progress, 1)
st.Start() st.Start()

View file

@ -2,12 +2,11 @@ package exporter
import ( import (
"context" "context"
"fmt"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
) )
// ShouldPrune determines if a file with the provided modification time should be // ShouldPrune determines if a file with the provided modification time should be
@ -45,7 +44,7 @@ func (exporter *Exporter) PruneExports(ctx context.Context, progress io.Writer)
// assemble path, and then remove the file! // assemble path, and then remove the file!
path := filepath.Join(sPath, entry.Name()) path := filepath.Join(sPath, entry.Name())
logging.ProgressF(progress, ctx, "Removing %s cause it is older than %d days\n", path, exporter.Config.MaxBackupAge) fmt.Fprintf(progress, "Removing %s cause it is older than %d days\n", path, exporter.Config.MaxBackupAge)
if err := os.Remove(path); err != nil { if err := os.Remove(path); err != nil {
return err return err

View file

@ -49,8 +49,8 @@ func (snapshots *Exporter) NewSnapshot(ctx context.Context, instance *wisski.Wis
logging.LogMessage(progress, ctx, "Locking instance") logging.LogMessage(progress, ctx, "Locking instance")
if !instance.Locker().TryLock(ctx) { if !instance.Locker().TryLock(ctx) {
err := locker.Locked err := locker.Locked
logging.ProgressF(progress, ctx, "%v", err) fmt.Fprintln(progress, err)
logging.LogMessage(progress, ctx, "Aborting snapshot creation") fmt.Fprintln(progress, "Aborting snapshot creation")
return Snapshot{ return Snapshot{
ErrPanic: err, ErrPanic: err,

View file

@ -3,9 +3,9 @@ package instances
import ( import (
"context" "context"
"embed" "embed"
"fmt"
"io" "io"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack" "github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/exit"
) )
@ -23,7 +23,7 @@ var runtimeResources embed.FS
// Update installs or updates runtime components needed by this component. // Update installs or updates runtime components needed by this component.
func (instances *Instances) Update(ctx context.Context, progress io.Writer) error { func (instances *Instances) Update(ctx context.Context, progress io.Writer) error {
err := unpack.InstallDir(instances.Config.Paths.RuntimeDir(), "runtime", runtimeResources, func(dst, src string) { err := unpack.InstallDir(instances.Config.Paths.RuntimeDir(), "runtime", runtimeResources, func(dst, src string) {
logging.ProgressF(progress, ctx, "[copy] %s\n", dst) fmt.Fprintln(progress, ctx, "[copy] %s\n", dst)
}) })
if err != nil { if err != nil {
return errBootstrapFailedRuntime.Wrap(err) return errBootstrapFailedRuntime.Wrap(err)

View file

@ -7,12 +7,12 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt"
"io" "io"
"io/fs" "io/fs"
"os" "os"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx" "github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/gliderlabs/ssh" "github.com/gliderlabs/ssh"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -82,7 +82,7 @@ func (ssh2 *SSH2) ReadOrMakeHostKey(progress io.Writer, ctx context.Context, pri
// loadHostKey loadsa host key // loadHostKey loadsa host key
func (ssh2 *SSH2) loadHostKey(progress io.Writer, ctx context.Context, key HostKey, path string) (err error) { func (ssh2 *SSH2) loadHostKey(progress io.Writer, ctx context.Context, key HostKey, path string) (err error) {
logging.ProgressF(progress, ctx, "Loading hostkey (algorithm %s) from %q", key.Algorithm(), path) fmt.Fprintf(progress, "Loading hostkey (algorithm %s) from %q\n", key.Algorithm(), path)
// read all the bytes from the file // read all the bytes from the file
privateKeyBytes, err := os.ReadFile(path) privateKeyBytes, err := os.ReadFile(path)
@ -108,7 +108,7 @@ func (ssh2 *SSH2) loadHostKey(progress io.Writer, ctx context.Context, key HostK
// makeHostKey makes a new host key // makeHostKey makes a new host key
func (ssh2 *SSH2) makeHostKey(progress io.Writer, ctx context.Context, key HostKey, path string) error { func (ssh2 *SSH2) makeHostKey(progress io.Writer, ctx context.Context, key HostKey, path string) error {
logging.ProgressF(progress, ctx, "Writing hostkey (algorithm %s) to %q", key.Algorithm(), path) fmt.Fprintf(progress, "Writing hostkey (algorithm %s) to %q\n", key.Algorithm(), path)
if err := key.Generate(ctx, 0, nil); err != nil { if err := key.Generate(ctx, 0, nil); err != nil {
return errors.Wrap(err, "Failed to generate key") return errors.Wrap(err, "Failed to generate key")

View file

@ -3,6 +3,7 @@ package component
import ( import (
"context" "context"
"fmt"
"io" "io"
"io/fs" "io/fs"
"os" "os"
@ -11,7 +12,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/compose" "github.com/FAU-CDI/wisski-distillery/pkg/compose"
"github.com/FAU-CDI/wisski-distillery/pkg/execx" "github.com/FAU-CDI/wisski-distillery/pkg/execx"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx" "github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack" "github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
@ -187,7 +187,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
is.ContextPath, is.ContextPath,
is.Resources, is.Resources,
func(dst, src string) { func(dst, src string) {
logging.ProgressF(progress, ctx, "[install] %s\n", dst) fmt.Fprintf(progress, "[install] %s\n", dst)
}, },
); err != nil { ); err != nil {
return err return err
@ -203,7 +203,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
err := (func() error { err := (func() error {
// find the file to install! // find the file to install!
dst := filepath.Join(is.Dir, "docker-compose.yml") dst := filepath.Join(is.Dir, "docker-compose.yml")
defer logging.ProgressF(progress, ctx, "[install] %s\n", dst) defer fmt.Fprintf(progress, "[install] %s\n", dst)
// create the file // create the file
yml, err := fsx.Create(dst, fsx.DefaultFilePerm) yml, err := fsx.Create(dst, fsx.DefaultFilePerm)
@ -235,7 +235,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// configure .env // configure .env
envDest := filepath.Join(is.Dir, ".env") envDest := filepath.Join(is.Dir, ".env")
if is.EnvPath != "" && is.EnvContext != nil { if is.EnvPath != "" && is.EnvContext != nil {
logging.ProgressF(progress, ctx, "[config] %s\n", envDest) fmt.Fprintf(progress, "[config] %s\n", envDest)
if err := unpack.InstallTemplate( if err := unpack.InstallTemplate(
envDest, envDest,
is.EnvContext, is.EnvContext,
@ -251,7 +251,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// find the destination! // find the destination!
dst := filepath.Join(is.Dir, name) dst := filepath.Join(is.Dir, name)
logging.ProgressF(progress, ctx, "[make] %s\n", dst) fmt.Fprintf(progress, "[make] %s\n", dst)
if is.MakeDirsPerm == fs.FileMode(0) { if is.MakeDirsPerm == fs.FileMode(0) {
is.MakeDirsPerm = fsx.DefaultDirPerm is.MakeDirsPerm = fsx.DefaultDirPerm
} }
@ -272,7 +272,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
dst := filepath.Join(is.Dir, name) dst := filepath.Join(is.Dir, name)
// copy over file from context // copy over file from context
logging.ProgressF(progress, ctx, "[copy] %s (from %s)\n", dst, src) fmt.Fprintf(progress, "[copy] %s (from %s)\n", dst, src)
if err := fsx.CopyFile(ctx, dst, src); err != nil { if err := fsx.CopyFile(ctx, dst, src); err != nil {
return errors.Wrapf(err, "Unable to copy file %s", src) return errors.Wrapf(err, "Unable to copy file %s", src)
} }
@ -283,7 +283,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// find the destination! // find the destination!
dst := filepath.Join(is.Dir, name) dst := filepath.Join(is.Dir, name)
logging.ProgressF(progress, ctx, "[touch] %s\n", dst) fmt.Fprintf(progress, "[touch] %s\n", dst)
if err := fsx.Touch(dst, is.TouchFilesPerm); err != nil { if err := fsx.Touch(dst, is.TouchFilesPerm); err != nil {
return err return err
} }
@ -291,7 +291,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// check that the stack can be loaded // check that the stack can be loaded
{ {
logging.ProgressF(progress, ctx, "[checking]") fmt.Fprintln(progress, "[checking]")
_, err := compose.Open(is.Dir) _, err := compose.Open(is.Dir)
if err != nil { if err != nil {
return err return err

View file

@ -2,6 +2,7 @@ package drush
import ( import (
"context" "context"
"fmt"
"time" "time"
"io" "io"
@ -9,7 +10,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/phpx" "github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status" "github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient" "github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/goprogram/exit" "github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/stream" "github.com/tkw1536/goprogram/stream"
) )
@ -23,7 +23,7 @@ func (drush *Drush) Cron(ctx context.Context, progress io.Writer) error {
code := drush.Dependencies.Barrel.Shell(ctx, stream.NonInteractive(progress), "/runtime/cron.sh")() code := drush.Dependencies.Barrel.Shell(ctx, stream.NonInteractive(progress), "/runtime/cron.sh")()
if code != 0 { if code != 0 {
// keep going, because we want to run as many crons as possible // keep going, because we want to run as many crons as possible
logging.ProgressF(progress, ctx, "%v", errCronFailed.WithMessageF(drush.Slug, code)) fmt.Fprintf(progress, "%v", errCronFailed.WithMessageF(drush.Slug, code))
} }
return nil return nil

View file

@ -18,16 +18,6 @@ func LogOperation(operation func() error, progress io.Writer, ctx context.Contex
return operation() return operation()
} }
// Progress writes a progress message to the given progress writer.
func Progress(progress io.Writer, ctx context.Context, message string) {
io.WriteString(progress, message)
}
// ProgressF is like progress, but uses fmt.Sprintf()
func ProgressF(progress io.Writer, ctx context.Context, format string, args ...interface{}) {
Progress(progress, ctx, fmt.Sprintf(format, args...))
}
// LogMessage logs a message that is displayed to the user // LogMessage logs a message that is displayed to the user
func LogMessage(progress io.Writer, ctx context.Context, format string, args ...interface{}) (int, error) { func LogMessage(progress io.Writer, ctx context.Context, format string, args ...interface{}) (int, error) {
return logOperation(progress, ctx, getIndent(progress), format, args...) return logOperation(progress, ctx, getIndent(progress), format, args...)