pkg/environment: Remove some file-based functions

This commit removes certain file-based functions from 'pkg/environment',
continuing the migration to entirely remove the package.
This commit is contained in:
Tom Wiesing 2023-03-02 11:15:15 +01:00
parent 39207a1cb5
commit 45540ab253
No known key found for this signature in database
20 changed files with 52 additions and 120 deletions

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"io/fs" "io/fs"
"os"
"path/filepath" "path/filepath"
wisski_distillery "github.com/FAU-CDI/wisski-distillery" wisski_distillery "github.com/FAU-CDI/wisski-distillery"
@ -110,7 +111,7 @@ func (bs cBootstrap) Run(context wisski_distillery.Context) error {
{ {
logging.LogMessage(context.Stderr, context.Context, "Copying over wdcli executable") logging.LogMessage(context.Stderr, context.Context, "Copying over wdcli executable")
exe, err := env.Executable() exe, err := os.Executable()
if err != nil { if err != nil {
return errBoostrapFailedToCopyExe.WithMessageF(err) return errBoostrapFailedToCopyExe.WithMessageF(err)
} }
@ -185,7 +186,7 @@ func (bs cBootstrap) Run(context wisski_distillery.Context) error {
// re-read the configuration and print it! // re-read the configuration and print it!
logging.LogMessage(context.Stderr, context.Context, "Configuration is now complete") logging.LogMessage(context.Stderr, context.Context, "Configuration is now complete")
f, err := env.Open(cfgPath) f, err := os.Open(cfgPath)
if err != nil { if err != nil {
return errBootstrapOpenConfig.WithMessageF(err) return errBootstrapOpenConfig.WithMessageF(err)
} }

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os"
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"
@ -34,7 +35,7 @@ func (c cfgMigrate) Run(context wisski_distillery.Context) error {
env := new(environment.Native) env := new(environment.Native)
// open the legacy file // open the legacy file
file, err := env.Open(c.Positionals.Input) file, err := os.Open(c.Positionals.Input)
if err != nil { if err != nil {
return err return err
} }

View file

@ -3,6 +3,7 @@ package exporter
import ( import (
"context" "context"
"io" "io"
"os"
"path/filepath" "path/filepath"
"github.com/FAU-CDI/wisski-distillery/internal/models" "github.com/FAU-CDI/wisski-distillery/internal/models"
@ -83,7 +84,7 @@ func (exporter *Exporter) MakeExport(ctx context.Context, progress io.Writer, ta
if !task.StagingOnly { if !task.StagingOnly {
defer func() { defer func() {
logging.LogMessage(progress, ctx, "Removing staging directory") logging.LogMessage(progress, ctx, "Removing staging directory")
exporter.Environment.RemoveAll(stagingDir) os.RemoveAll(stagingDir)
}() }()
} }

View file

@ -2,6 +2,7 @@ package logger
import ( import (
"context" "context"
"os"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component" "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql" "github.com/FAU-CDI/wisski-distillery/internal/dis/component/sql"
@ -62,7 +63,7 @@ func (log *Logger) Log(ctx context.Context) ([]models.Export, error) {
// partition out the exports that have been deleted! // partition out the exports that have been deleted!
parts := collection.Partition(exports, func(s models.Export) bool { parts := collection.Partition(exports, func(s models.Export) bool {
_, err := log.Still.Environment.Stat(s.Path) _, err := os.Stat(s.Path)
return !environment.IsNotExist(err) return !environment.IsNotExist(err)
}) })

View file

@ -3,6 +3,7 @@ package exporter
import ( import (
"context" "context"
"io" "io"
"os"
"path/filepath" "path/filepath"
"time" "time"
@ -20,7 +21,7 @@ func (exporter *Exporter) PruneExports(ctx context.Context, progress io.Writer)
sPath := exporter.ArchivePath() sPath := exporter.ArchivePath()
// list all the files // list all the files
entries, err := exporter.Still.Environment.ReadDir(sPath) entries, err := os.ReadDir(sPath)
if err != nil { if err != nil {
return err return err
} }
@ -46,7 +47,7 @@ func (exporter *Exporter) PruneExports(ctx context.Context, progress io.Writer)
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) logging.ProgressF(progress, ctx, "Removing %s cause it is older than %d days\n", path, exporter.Config.MaxBackupAge)
if err := exporter.Still.Environment.Remove(path); err != nil { if err := os.Remove(path); err != nil {
return err return err
} }
} }

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"os"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component" "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/dis/component/instances"
@ -50,7 +51,7 @@ func (purger *Purger) Purge(ctx context.Context, out io.Writer, slug string) err
// remove the filesystem // remove the filesystem
logging.LogMessage(out, ctx, "Removing from filesystem %s", instance.FilesystemBase) logging.LogMessage(out, ctx, "Removing from filesystem %s", instance.FilesystemBase)
if err := purger.Environment.RemoveAll(instance.FilesystemBase); err != nil { if err := os.RemoveAll(instance.FilesystemBase); err != nil {
fmt.Fprintln(out, err) fmt.Fprintln(out, err)
} }

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"net/http" "net/http"
"os"
"strings" "strings"
) )
@ -18,7 +19,7 @@ func (home *Home) loadRedirect(ctx context.Context) (redirect Redirect, err erro
redirect.Permanent = false redirect.Permanent = false
// load the overrides file // load the overrides file
overrides, err := home.Environment.Open(home.Config.Paths.OverridesJSON) overrides, err := os.Open(home.Config.Paths.OverridesJSON)
if err != nil { if err != nil {
return redirect, err return redirect, err
} }

View file

@ -8,6 +8,7 @@ import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io" "io"
"os"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/logging" "github.com/FAU-CDI/wisski-distillery/pkg/logging"
@ -64,7 +65,7 @@ func (ssh2 *SSH2) UseOrMakeHostKey(progress io.Writer, ctx context.Context, serv
func (ssh2 *SSH2) ReadOrMakeHostKey(progress io.Writer, ctx context.Context, privateKeyPath string, algorithm HostKeyAlgorithm) (key gossh.Signer, err error) { func (ssh2 *SSH2) ReadOrMakeHostKey(progress io.Writer, ctx context.Context, privateKeyPath string, algorithm HostKeyAlgorithm) (key gossh.Signer, err error) {
hostKey := NewHostKey(algorithm) hostKey := NewHostKey(algorithm)
if _, e := ssh2.Environment.Lstat(privateKeyPath); environment.IsNotExist(e) { // path doesn't exist => generate a new key there! if _, e := os.Lstat(privateKeyPath); environment.IsNotExist(e) { // path doesn't exist => generate a new key there!
err = ssh2.makeHostKey(progress, ctx, hostKey, privateKeyPath) err = ssh2.makeHostKey(progress, ctx, hostKey, privateKeyPath)
if err != nil { if err != nil {
err = errors.Wrap(err, "Unable to generate new host key") err = errors.Wrap(err, "Unable to generate new host key")

View file

@ -173,7 +173,7 @@ func (ds Stack) compose(ctx context.Context, io stream.IOStream, args ...string)
var err error var err error
ds.DockerExecutable, err = execx.LookPathAbs("docker") ds.DockerExecutable, err = execx.LookPathAbs("docker")
if err != nil { if err != nil {
return environment.ExecCommandErrorFunc return execx.CommandErrorFunc
} }
} }
return execx.Exec(ctx, io, ds.Dir, ds.DockerExecutable, append([]string{"compose"}, args...)...) return execx.Exec(ctx, io, ds.Dir, ds.DockerExecutable, append([]string{"compose"}, args...)...)

View file

@ -1,6 +1,8 @@
package dis package dis
import ( import (
"os"
"github.com/FAU-CDI/wisski-distillery/internal/cli" "github.com/FAU-CDI/wisski-distillery/internal/cli"
"github.com/FAU-CDI/wisski-distillery/internal/config" "github.com/FAU-CDI/wisski-distillery/internal/config"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component" "github.com/FAU-CDI/wisski-distillery/internal/dis/component"
@ -39,7 +41,7 @@ func NewDistillery(params cli.Params, flags cli.Flags, req cli.Requirements) (di
dis.Upstream.SQL = "sql:3306" dis.Upstream.SQL = "sql:3306"
dis.Upstream.Triplestore = "triplestore:7200" dis.Upstream.Triplestore = "triplestore:7200"
dis.Upstream.Solr = "solr:8983" dis.Upstream.Solr = "solr:8983"
params.ConfigPath = dis.Still.Environment.GetEnv("CONFIG_PATH") params.ConfigPath = os.Getenv("CONFIG_PATH")
} }
// if we don't need to load the config, there is nothing to do // if we don't need to load the config, there is nothing to do
@ -57,7 +59,7 @@ func NewDistillery(params cli.Params, flags cli.Flags, req cli.Requirements) (di
} }
// open the config file! // open the config file!
f, err := dis.Still.Environment.Open(params.ConfigPath) f, err := os.Open(params.ConfigPath)
if err != nil { if err != nil {
return nil, errOpenConfig.WithMessageF(err) return nil, errOpenConfig.WithMessageF(err)
} }

View file

@ -3,6 +3,7 @@ package extras
import ( import (
"bufio" "bufio"
"context" "context"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -82,7 +83,7 @@ func (wisski *Prefixes) database(ctx context.Context, server *phpx.Server) (pref
func (prefixes *Prefixes) blocked() ([]string, error) { func (prefixes *Prefixes) blocked() ([]string, error) {
// open the resolver block file // open the resolver block file
// TODO: move this to the distillery // TODO: move this to the distillery
file, err := prefixes.Malt.Environment.Open(prefixes.Malt.Config.Paths.ResolverBlocks) file, err := os.Open(prefixes.Malt.Config.Paths.ResolverBlocks)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -123,7 +124,7 @@ func (wisski *Prefixes) filePrefixes() (prefixes []string, err error) {
return nil, nil return nil, nil
} }
file, err := wisski.Malt.Environment.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,7 +3,6 @@ package environment
import ( import (
"io" "io"
"io/fs" "io/fs"
"time"
) )
// Environment represents an environment that a program can run it. // Environment represents an environment that a program can run it.
@ -11,31 +10,9 @@ import (
type Environment interface { type Environment interface {
isEnv() isEnv()
GetEnv(name string) string
Stat(path string) (fs.FileInfo, error)
Lstat(path string) (fs.FileInfo, error)
Readlink(path string) (string, error)
Symlink(oldname, newname string) error
ReadDir(name string) ([]fs.DirEntry, error)
Open(path string) (fs.File, error)
Chtimes(name string, atime time.Time, mtime time.Time) error
SameFile(f1, f2 fs.FileInfo) bool
Create(path string, mode fs.FileMode) (WritableFile, error) Create(path string, mode fs.FileMode) (WritableFile, error)
Mkdir(path string, mode fs.FileMode) error Mkdir(path string, mode fs.FileMode) error
MkdirAll(path string, mode fs.FileMode) error MkdirAll(path string, mode fs.FileMode) error
Remove(path string) error
RemoveAll(path string) error
WalkDir(root string, fn fs.WalkDirFunc) error
Abs(path string) (string, error)
} }
type WritableFile interface { type WritableFile interface {

View file

@ -8,15 +8,6 @@ import (
"github.com/tkw1536/pkglib/pools" "github.com/tkw1536/pkglib/pools"
) )
// ExecCommandError is returned by Exec when a command could not be executed.
// This typically hints that the executable cannot be found, but may have other causes.
const ExecCommandError = 127
// ExecCommandErrorFunc always returns ExecCommandError.
func ExecCommandErrorFunc() int {
return ExecCommandError
}
// DefaultFilePerm is the default mode to use for files // DefaultFilePerm is the default mode to use for files
const DefaultFilePerm fs.FileMode = 0666 const DefaultFilePerm fs.FileMode = 0666
@ -51,7 +42,7 @@ func WriteFile(env Environment, path string, data []byte, mode fs.FileMode) erro
// ReadFile is like [os.ReadFile] // ReadFile is like [os.ReadFile]
func ReadFile(env Environment, path string) ([]byte, error) { func ReadFile(env Environment, path string) ([]byte, error) {
// open the file! // open the file!
file, err := env.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -3,7 +3,6 @@ package environment
import ( import (
"io/fs" "io/fs"
"os" "os"
"path/filepath"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -26,46 +25,6 @@ func (n *Native) resetMask() {
n.ulock.Unlock() n.ulock.Unlock()
} }
func (*Native) GetEnv(name string) string {
return os.Getenv(name)
}
func (*Native) Stat(path string) (fs.FileInfo, error) {
return os.Stat(path)
}
func (*Native) Lstat(path string) (fs.FileInfo, error) {
return os.Lstat(path)
}
func (*Native) Readlink(path string) (string, error) {
return os.Readlink(path)
}
func (*Native) Symlink(oldname, newname string) error {
return os.Symlink(oldname, newname)
}
func (*Native) ReadDir(name string) ([]fs.DirEntry, error) {
return os.ReadDir(name)
}
func (*Native) SameFile(f1, f2 fs.FileInfo) bool {
return os.SameFile(f1, f2)
}
func (*Native) WalkDir(path string, f fs.WalkDirFunc) error {
return filepath.WalkDir(path, f)
}
func (*Native) Executable() (string, error) {
return os.Executable() // TODO: not sure this works with the remote concepts
}
func (*Native) Open(path string) (fs.File, error) {
return os.Open(path)
}
func (n *Native) Create(path string, mode fs.FileMode) (WritableFile, error) { func (n *Native) Create(path string, mode fs.FileMode) (WritableFile, error) {
n.ulock.Lock() n.ulock.Lock()
defer n.ulock.Unlock() defer n.ulock.Unlock()
@ -90,15 +49,3 @@ func (n *Native) MkdirAll(path string, mode fs.FileMode) error {
return os.MkdirAll(path, fs.ModeDir|mode) return os.MkdirAll(path, fs.ModeDir|mode)
} }
func (*Native) Remove(path string) error {
return os.Remove(path)
}
func (*Native) RemoveAll(path string) error {
return os.RemoveAll(path)
}
func (*Native) Abs(path string) (string, error) {
return filepath.Abs(path)
}

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"io/fs" "io/fs"
"os"
"path/filepath" "path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -27,7 +28,7 @@ func CopyFile(ctx context.Context, env environment.Environment, dst, src string)
} }
// open the source // open the source
srcFile, err := env.Open(src) srcFile, err := os.Open(src)
if err != nil { if err != nil {
return err return err
} }
@ -64,20 +65,20 @@ func CopyLink(ctx context.Context, env environment.Environment, dst, src string)
} }
// read the link target // read the link target
target, err := env.Readlink(src) target, err := os.Readlink(src)
if err != nil { if err != nil {
return err return err
} }
// delete it if it already exists // delete it if it already exists
if Exists(env, dst) { if Exists(env, dst) {
if err := env.Remove(dst); err != nil { if err := os.Remove(dst); err != nil {
return err return err
} }
} }
// make the symbolic link! // make the symbolic link!
return env.Symlink(target, dst) return os.Symlink(target, dst)
} }
var ErrDstFile = errors.New("dst is a file") var ErrDstFile = errors.New("dst is a file")
@ -98,7 +99,7 @@ func CopyDirectory(ctx context.Context, env environment.Environment, dst, src st
return ErrDstFile return ErrDstFile
} }
return env.WalkDir(src, func(path string, d fs.DirEntry, err error) error { return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
// someone previously returned an error // someone previously returned an error
if err != nil { if err != nil {
return err return err

View file

@ -1,6 +1,7 @@
package fsx package fsx
import ( import (
"os"
"path/filepath" "path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -44,13 +45,13 @@ func SameFile(env environment.Environment, path1, path2 string) bool {
func couldBeSameFile(env environment.Environment, path1, path2 string) (same, authorative bool) { func couldBeSameFile(env environment.Environment, path1, path2 string) (same, authorative bool) {
{ {
// stat both files // stat both files
info1, err1 := env.Stat(path1) info1, err1 := os.Stat(path1)
info2, err2 := env.Stat(path2) info2, err2 := os.Stat(path2)
// both files exist => check using env.SameFile // both files exist => check using env.SameFile
// the result is always authorative // the result is always authorative
if err1 == nil && err2 == nil { if err1 == nil && err2 == nil {
same = env.SameFile(info1, info2) same = os.SameFile(info1, info2)
authorative = true authorative = true
return return
} }
@ -68,8 +69,8 @@ func couldBeSameFile(env environment.Environment, path1, path2 string) (same, au
{ {
// resolve paths absolutely // resolve paths absolutely
rpath1, err1 := env.Abs(path1) rpath1, err1 := filepath.Abs(path1)
rpath2, err2 := env.Abs(path2) rpath2, err2 := filepath.Abs(path2)
// if either path could not be resolved absolutely // if either path could not be resolved absolutely
// fallback to just using clean! // fallback to just using clean!

View file

@ -2,6 +2,7 @@ package fsx
import ( import (
"io/fs" "io/fs"
"os"
"time" "time"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -16,7 +17,7 @@ func Touch(env environment.Environment, path string, perm fs.FileMode) error {
if perm == 0 { if perm == 0 {
perm = environment.DefaultFilePerm perm = environment.DefaultFilePerm
} }
_, err := env.Stat(path) _, err := os.Stat(path)
switch { switch {
case environment.IsNotExist(err): case environment.IsNotExist(err):
f, err := env.Create(path, perm) f, err := env.Create(path, perm)
@ -29,6 +30,6 @@ func Touch(env environment.Environment, path string, perm fs.FileMode) error {
return err return err
default: default:
now := time.Now().Local() now := time.Now().Local()
return env.Chtimes(path, now, now) return os.Chtimes(path, now, now)
} }
} }

View file

@ -3,30 +3,31 @@ package fsx
import ( import (
"io/fs" "io/fs"
"os"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
) )
// Exists checks if the given path exists // Exists checks if the given path exists
func Exists(env environment.Environment, path string) bool { func Exists(env environment.Environment, path string) bool {
_, err := env.Lstat(path) _, err := os.Lstat(path)
return err == nil return err == nil
} }
// IsDirectory checks if the provided path exists and is a directory // IsDirectory checks if the provided path exists and is a directory
func IsDirectory(env environment.Environment, path string) bool { func IsDirectory(env environment.Environment, path string) bool {
info, err := env.Stat(path) info, err := os.Stat(path)
return err == nil && info.Mode().IsDir() return err == nil && info.Mode().IsDir()
} }
// IsFile checks if the provided path exists and is a regular file // IsFile checks if the provided path exists and is a regular file
func IsFile(env environment.Environment, path string) bool { func IsFile(env environment.Environment, path string) bool {
info, err := env.Stat(path) info, err := os.Stat(path)
return err == nil && info.Mode().IsRegular() return err == nil && info.Mode().IsRegular()
} }
// IsLink checks if the provided path exists and is a symlink // IsLink checks if the provided path exists and is a symlink
func IsLink(env environment.Environment, path string) bool { func IsLink(env environment.Environment, path string) bool {
info, err := env.Lstat(path) info, err := os.Lstat(path)
return err == nil && info.Mode()&fs.ModeSymlink != 0 return err == nil && info.Mode()&fs.ModeSymlink != 0
} }

View file

@ -6,6 +6,7 @@ import (
"compress/gzip" "compress/gzip"
"io" "io"
"io/fs" "io/fs"
"os"
"path/filepath" "path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -32,7 +33,7 @@ func Package(env environment.Environment, dst, src string, onCopy func(rel strin
defer tarHandle.Close() defer tarHandle.Close()
// and walk through it! // and walk through it!
err = env.WalkDir(src, func(path string, entry fs.DirEntry, err error) error { err = filepath.WalkDir(src, func(path string, entry fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -75,7 +76,7 @@ func Package(env environment.Environment, dst, src string, onCopy func(rel strin
} }
// open the file // open the file
handle, err := env.Open(path) handle, err := os.Open(path)
if err != nil { if err != nil {
return err return err
} }

View file

@ -3,6 +3,7 @@ package unpack
import ( import (
"io" "io"
"io/fs" "io/fs"
"os"
"path/filepath" "path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/environment" "github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -80,7 +81,7 @@ func installResource(env environment.Environment, dst string, src string, fsys f
func installDir(env environment.Environment, dst string, srcInfo fs.FileInfo, srcFile fs.ReadDirFile, src string, fsys fs.FS, onInstallFile func(dst, src string)) error { func installDir(env environment.Environment, dst string, srcInfo fs.FileInfo, srcFile fs.ReadDirFile, src string, fsys fs.FS, onInstallFile func(dst, src string)) error {
// create the destination // create the destination
dstStat, dstErr := env.Stat(dst) dstStat, dstErr := os.Stat(dst)
switch { switch {
case environment.IsNotExist(dstErr): case environment.IsNotExist(dstErr):
if err := env.MkdirAll(dst, srcInfo.Mode()); err != nil { if err := env.MkdirAll(dst, srcInfo.Mode()); err != nil {