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"
"github.com/FAU-CDI/wisski-distillery/internal/cli"
"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/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 {
logging.Progress(writer, context.Context, "reading prefixes")
io.WriteString(writer, "reading prefixes")
err := instance.Prefixes().Update(context.Context)
if err != nil {
return err

View file

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

View file

@ -3,6 +3,7 @@ package exporter
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
@ -71,8 +72,8 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
if !task.StagingOnly && archivePath == "" {
archivePath = exporter.NewArchivePath(Slug)
}
logging.ProgressF(progress, ctx, "Staging Directory: %s\n", stagingDir)
logging.ProgressF(progress, ctx, "Archive Path: %s\n", archivePath)
fmt.Fprintf(progress, "Staging Directory: %s\n", stagingDir)
fmt.Fprintf(progress, "Archive Path: %s\n", archivePath)
// create the 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
reportPath := filepath.Join(stagingDir, "report.txt")
logging.ProgressF(progress, ctx, reportPath)
fmt.Fprintln(progress, reportPath)
// create the path
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
// all that is left is to write the log entry
if task.StagingOnly {
logging.LogMessage(progress, ctx, "Writing Log Entry")
fmt.Fprintln(progress, "Writing Log Entry")
// write out the log entry
entry.Path = stagingDir
entry.Packed = false
exporter.Dependencies.ExporterLogger.Add(ctx, entry)
logging.ProgressF(progress, ctx, "Wrote %s\n", stagingDir)
fmt.Fprintf(progress, "Wrote %s\n", stagingDir)
return nil
}
// package everything up as an archive!
if err := logging.LogOperation(func() error {
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.Start()

View file

@ -2,12 +2,11 @@ package exporter
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
)
// 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!
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 {
return err

View file

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

View file

@ -3,9 +3,9 @@ package instances
import (
"context"
"embed"
"fmt"
"io"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"github.com/tkw1536/goprogram/exit"
)
@ -23,7 +23,7 @@ var runtimeResources embed.FS
// Update installs or updates runtime components needed by this component.
func (instances *Instances) Update(ctx context.Context, progress io.Writer) error {
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 {
return errBootstrapFailedRuntime.Wrap(err)

View file

@ -7,12 +7,12 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/fs"
"os"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/gliderlabs/ssh"
"github.com/pkg/errors"
@ -82,7 +82,7 @@ func (ssh2 *SSH2) ReadOrMakeHostKey(progress io.Writer, ctx context.Context, pri
// loadHostKey loadsa host key
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
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
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 {
return errors.Wrap(err, "Failed to generate key")

View file

@ -3,6 +3,7 @@ package component
import (
"context"
"fmt"
"io"
"io/fs"
"os"
@ -11,7 +12,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/compose"
"github.com/FAU-CDI/wisski-distillery/pkg/execx"
"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/pkg/errors"
"github.com/tkw1536/goprogram/stream"
@ -187,7 +187,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
is.ContextPath,
is.Resources,
func(dst, src string) {
logging.ProgressF(progress, ctx, "[install] %s\n", dst)
fmt.Fprintf(progress, "[install] %s\n", dst)
},
); err != nil {
return err
@ -203,7 +203,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
err := (func() error {
// find the file to install!
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
yml, err := fsx.Create(dst, fsx.DefaultFilePerm)
@ -235,7 +235,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// configure .env
envDest := filepath.Join(is.Dir, ".env")
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(
envDest,
is.EnvContext,
@ -251,7 +251,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// find the destination!
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) {
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)
// 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 {
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!
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 {
return err
}
@ -291,7 +291,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// check that the stack can be loaded
{
logging.ProgressF(progress, ctx, "[checking]")
fmt.Fprintln(progress, "[checking]")
_, err := compose.Open(is.Dir)
if err != nil {
return err

View file

@ -2,6 +2,7 @@ package drush
import (
"context"
"fmt"
"time"
"io"
@ -9,7 +10,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"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/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")()
if code != 0 {
// 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

View file

@ -18,16 +18,6 @@ func LogOperation(operation func() error, progress io.Writer, ctx context.Contex
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
func LogMessage(progress io.Writer, ctx context.Context, format string, args ...interface{}) (int, error) {
return logOperation(progress, ctx, getIndent(progress), format, args...)