Use fsx package and friends from pkglib

This commit is contained in:
Tom Wiesing 2023-04-08 17:51:17 +02:00
parent 1f8c55da7c
commit 0f6803f890
No known key found for this signature in database
35 changed files with 91 additions and 493 deletions

View file

@ -1,6 +1,7 @@
package auth
import (
"bytes"
"context"
"encoding/base64"
"fmt"
@ -14,7 +15,6 @@ import (
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"github.com/tkw1536/pkglib/password"
"github.com/tkw1536/pkglib/pools"
"github.com/tkw1536/pkglib/reflectx"
"golang.org/x/crypto/bcrypt"
)
@ -187,10 +187,9 @@ func TOTPLink(secret *otp.Key, width, height int) (string, error) {
}
// encode image as base64
buffer := pools.GetBuffer()
defer pools.ReleaseBuffer(buffer)
var buffer bytes.Buffer
if err := png.Encode(buffer, img); err != nil {
if err := png.Encode(&buffer, img); err != nil {
return "", err
}

View file

@ -6,8 +6,8 @@ import (
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/pkg/errors"
"github.com/tkw1536/pkglib/fsx/umaskfree"
)
// Backupable represents a component with a Backup method
@ -122,7 +122,7 @@ func (sc *stagingContext) AddDirectory(path string, op func(context.Context) err
}
// run the make directory
if err := fsx.Mkdir(dst, fsx.DefaultDirPerm); err != nil {
if err := umaskfree.Mkdir(dst, umaskfree.DefaultDirPerm); err != nil {
return err
}
@ -143,7 +143,7 @@ func (sc *stagingContext) CopyFile(dst, src string) error {
return err
}
sc.sendPath(dst)
return fsx.CopyFile(sc.ctx, dstPath, src)
return umaskfree.CopyFile(sc.ctx, dstPath, src)
}
func (sc *stagingContext) CopyDirectory(dst, src string) error {
@ -156,7 +156,7 @@ func (sc *stagingContext) CopyDirectory(dst, src string) error {
return err
}
return fsx.CopyDirectory(sc.ctx, dstPath, src, func(dst, src string) {
return umaskfree.CopyDirectory(sc.ctx, dstPath, src, func(dst, src string) {
sc.sendPath(dst)
})
}
@ -174,7 +174,7 @@ func (sc *stagingContext) AddFile(path string, op func(ctx context.Context, file
}
// create the file
file, err := fsx.Create(dst, fsx.DefaultFilePerm)
file, err := umaskfree.Create(dst, umaskfree.DefaultFilePerm)
if err != nil {
return err
}

View file

@ -10,8 +10,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"github.com/tkw1536/pkglib/status"
"golang.org/x/exp/slices"
)
@ -118,7 +118,7 @@ func (backup *Backup) run(ctx context.Context, progress io.Writer, exporter *Exp
defer st.Stop()
instancesBackupDir := filepath.Join(backup.Description.Dest, "instances")
if err := fsx.Mkdir(instancesBackupDir, fsx.DefaultDirPerm); err != nil {
if err := umaskfree.Mkdir(instancesBackupDir, umaskfree.DefaultDirPerm); err != nil {
backup.InstanceListErr = err
return nil
}
@ -139,7 +139,7 @@ func (backup *Backup) run(ctx context.Context, progress io.Writer, exporter *Exp
Handler: func(instance *wisski.WissKI, index int, writer io.Writer) Snapshot {
dir := filepath.Join(instancesBackupDir, instance.Slug)
if err := fsx.Mkdir(dir, fsx.DefaultDirPerm); err != nil {
if err := umaskfree.Mkdir(dir, umaskfree.DefaultDirPerm); err != nil {
return Snapshot{
ErrPanic: err,
}

View file

@ -13,7 +13,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
"github.com/FAU-CDI/wisski-distillery/internal/passwordx"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/tkw1536/pkglib/fsx"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"github.com/tkw1536/pkglib/password"
)
@ -75,7 +76,7 @@ func (*Exporter) newSnapshotName(prefix string) string {
func (dis *Exporter) NewStagingDir(prefix string) (path string, err error) {
for path == "" || errors.Is(err, fs.ErrExist) {
path = filepath.Join(dis.StagingPath(), dis.newSnapshotName(prefix))
err = fsx.Mkdir(path, fsx.DefaultFilePerm)
err = umaskfree.Mkdir(path, umaskfree.DefaultFilePerm)
}
if err != nil {
path = ""

View file

@ -12,10 +12,10 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/targz"
"github.com/tkw1536/pkglib/collection"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"github.com/tkw1536/pkglib/status"
)
@ -98,7 +98,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
// create the staging directory
logging.LogMessage(progress, "Creating staging directory")
err = fsx.Mkdir(stagingDir, fsx.DefaultDirPerm)
err = umaskfree.Mkdir(stagingDir, umaskfree.DefaultDirPerm)
if !errors.Is(err, fs.ErrExist) && err != nil {
return err
}
@ -136,7 +136,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
reportPath := filepath.Join(stagingDir, ReportMachinePath)
fmt.Fprintln(progress, reportPath)
report, err := fsx.Create(reportPath, fsx.DefaultFilePerm)
report, err := umaskfree.Create(reportPath, umaskfree.DefaultFilePerm)
if err != nil {
return err
}
@ -151,7 +151,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
reportPath := filepath.Join(stagingDir, ReportPlainPath)
fmt.Fprintln(progress, reportPath)
report, err := fsx.Create(reportPath, fsx.DefaultFilePerm)
report, err := umaskfree.Create(reportPath, umaskfree.DefaultFilePerm)
if err != nil {
return err
}

View file

@ -4,16 +4,15 @@ import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/tkw1536/pkglib/pools"
"github.com/tkw1536/pkglib/sequence"
)
func (snapshot Snapshot) String() string {
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
var builder strings.Builder
snapshot.ReportPlain(builder)
snapshot.ReportPlain(&builder)
return builder.String()
}
@ -68,10 +67,8 @@ func (snapshot Snapshot) ReportPlain(w io.Writer) error {
// Strings turns this backup into a string for the BackupReport.
func (backup Backup) String() string {
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
backup.ReportPlain(builder)
var builder strings.Builder
backup.ReportPlain(&builder)
return builder.String()
}

View file

@ -8,8 +8,8 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/wisski"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/tkw1536/pkglib/fsx"
)
type Provision struct {

View file

@ -11,7 +11,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/pools"
)
//go:embed "public.html"
@ -62,8 +61,7 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
}
// get a builder
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
var builder strings.Builder
// prepare about
pc.aboutContext.Logo = logoHTML
@ -72,7 +70,7 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
// render the about template
if err := about.Execute(builder, pc.aboutContext); err != nil {
if err := about.Execute(&builder, pc.aboutContext); err != nil {
return pc, nil
}

View file

@ -13,7 +13,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/assets"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/server/templating"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/pools"
"github.com/yuin/goldmark"
gmmeta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
@ -90,8 +89,7 @@ var newsFS embed.FS
// Items returns a list of all news items
func Items() ([]Item, error) {
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
var builder strings.Builder
files, err := fs.Glob(newsFS, "NEWS/*.md")
if err != nil {
@ -101,7 +99,7 @@ func Items() ([]Item, error) {
items := make([]Item, len(files))
for i, file := range files {
items[i].ID = file[len("NEWS/") : len(file)-len(".md")]
if err := items[i].parse(file, builder); err != nil {
if err := items[i].parse(file, &builder); err != nil {
return nil, err
}
}

View file

@ -8,12 +8,12 @@ import (
"html/template"
"net/http"
"reflect"
"strings"
"time"
"github.com/gorilla/csrf"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/httpx"
"github.com/tkw1536/pkglib/pools"
"github.com/tkw1536/pkglib/timex"
)
@ -251,9 +251,7 @@ func (ctx *tContext[C]) renderSafe(name string, t *template.Template, c any) (te
}
value, err, panicked := func() (value template.HTML, err error, panicked bool) {
// get a builder
builder := pools.GetBuilder()
defer pools.ReleaseBuilder(builder)
var builder strings.Builder
defer func() {
if panicked {
@ -267,7 +265,7 @@ func (ctx *tContext[C]) renderSafe(name string, t *template.Template, c any) (te
}()
panicked = true
err = t.Execute(builder, c)
err = t.Execute(&builder, c)
panicked = false
if err != nil {

View file

@ -6,7 +6,7 @@ import (
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/tkw1536/pkglib/fsx/umaskfree"
)
type SQL struct {
@ -50,7 +50,7 @@ func (sql *SQL) Stack() component.StackWithResources {
"HTTPS_ENABLED": sql.Config.HTTP.HTTPSEnabledEnv(),
},
MakeDirsPerm: fsx.DefaultDirPerm,
MakeDirsPerm: umaskfree.DefaultDirPerm,
MakeDirs: []string{
"data",
"imports",

View file

@ -12,8 +12,8 @@ import (
"io/fs"
"os"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/gliderlabs/ssh"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"github.com/pkg/errors"
gossh "golang.org/x/crypto/ssh"
@ -120,7 +120,7 @@ func (ssh2 *SSH2) makeHostKey(progress io.Writer, ctx context.Context, key HostK
}
// generate and write private key as PEM
privateKeyFile, err := fsx.Create(path, fsx.DefaultFilePerm)
privateKeyFile, err := umaskfree.Create(path, umaskfree.DefaultFilePerm)
if err != nil {
return err
}

View file

@ -11,9 +11,9 @@ 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/unpack"
"github.com/pkg/errors"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"github.com/tkw1536/pkglib/stream"
)
@ -193,7 +193,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
return err
}
} else {
if err := fsx.MkdirAll(is.Dir, fsx.DefaultDirPerm); err != nil {
if err := umaskfree.MkdirAll(is.Dir, umaskfree.DefaultDirPerm); err != nil {
return err
}
}
@ -206,7 +206,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
defer fmt.Fprintf(progress, "[install] %s\n", dst)
// create the file
yml, err := fsx.Create(dst, fsx.DefaultFilePerm)
yml, err := umaskfree.Create(dst, umaskfree.DefaultFilePerm)
if err != nil {
return err
}
@ -253,9 +253,9 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
fmt.Fprintf(progress, "[make] %s\n", dst)
if is.MakeDirsPerm == fs.FileMode(0) {
is.MakeDirsPerm = fsx.DefaultDirPerm
is.MakeDirsPerm = umaskfree.DefaultDirPerm
}
if err := fsx.MkdirAll(dst, is.MakeDirsPerm); err != nil {
if err := umaskfree.MkdirAll(dst, is.MakeDirsPerm); err != nil {
return err
}
}
@ -273,7 +273,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
// copy over file from context
fmt.Fprintf(progress, "[copy] %s (from %s)\n", dst, src)
if err := fsx.CopyFile(ctx, dst, src); err != nil {
if err := umaskfree.CopyFile(ctx, dst, src); err != nil {
return errors.Wrapf(err, "Unable to copy file %s", src)
}
}
@ -284,7 +284,7 @@ func (is StackWithResources) Install(ctx context.Context, progress io.Writer, co
dst := filepath.Join(is.Dir, name)
fmt.Fprintf(progress, "[touch] %s\n", dst)
if err := fsx.Touch(dst, is.TouchFilesPerm); err != nil {
if err := umaskfree.Touch(dst, is.TouchFilesPerm); err != nil {
return err
}
}

View file

@ -11,7 +11,6 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/tkw1536/pkglib/pools"
"github.com/tkw1536/pkglib/timex"
)
@ -39,12 +38,10 @@ func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body any,
// for "PUT" and "POST" we setup a body
if method == http.MethodPut || method == http.MethodPost {
if bodyName != "" {
// create a new buffer for the body
buffer := pools.GetBuffer()
defer pools.ReleaseBuffer(buffer)
var buffer bytes.Buffer
// write the file to it
writer := multipart.NewWriter(buffer)
writer := multipart.NewWriter(&buffer)
{
part, err := writer.CreateFormFile(bodyName, "filename.txt")
if err != nil {
@ -55,7 +52,7 @@ func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body any,
writer.Close()
// use it for the request
reader = buffer
reader = &buffer
contentType = writer.FormDataContentType()
} else {
mbytes, err := json.Marshal(body)

View file

@ -11,7 +11,6 @@ import (
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/pkglib/errorx"
"github.com/tkw1536/pkglib/pools"
)
var errTripleStoreFailedRepository = exit.Error{
@ -39,9 +38,9 @@ func (ts *Triplestore) CreateRepository(ctx context.Context, name, domain, user,
}
// prepare the create repo request
createRepo := pools.GetBuffer()
defer pools.ReleaseBuffer(createRepo)
err := unpack.WriteTemplate(createRepo, map[string]string{
var createRepo bytes.Buffer
err := unpack.WriteTemplate(&createRepo, map[string]string{
"GRAPHDB_REPO": name,
"INSTANCE_DOMAIN": domain,
}, bytes.NewReader(createRepoTTL))