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:
parent
39207a1cb5
commit
45540ab253
20 changed files with 52 additions and 120 deletions
|
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
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")
|
||||
exe, err := env.Executable()
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return errBoostrapFailedToCopyExe.WithMessageF(err)
|
||||
}
|
||||
|
|
@ -185,7 +186,7 @@ func (bs cBootstrap) Run(context wisski_distillery.Context) error {
|
|||
|
||||
// re-read the configuration and print it!
|
||||
logging.LogMessage(context.Stderr, context.Context, "Configuration is now complete")
|
||||
f, err := env.Open(cfgPath)
|
||||
f, err := os.Open(cfgPath)
|
||||
if err != nil {
|
||||
return errBootstrapOpenConfig.WithMessageF(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
|
||||
"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)
|
||||
|
||||
// open the legacy file
|
||||
file, err := env.Open(c.Positionals.Input)
|
||||
file, err := os.Open(c.Positionals.Input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package exporter
|
|||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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 {
|
||||
defer func() {
|
||||
logging.LogMessage(progress, ctx, "Removing staging directory")
|
||||
exporter.Environment.RemoveAll(stagingDir)
|
||||
os.RemoveAll(stagingDir)
|
||||
}()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package logger
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"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!
|
||||
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)
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package exporter
|
|||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
|
|
@ -20,7 +21,7 @@ func (exporter *Exporter) PruneExports(ctx context.Context, progress io.Writer)
|
|||
sPath := exporter.ArchivePath()
|
||||
|
||||
// list all the files
|
||||
entries, err := exporter.Still.Environment.ReadDir(sPath)
|
||||
entries, err := os.ReadDir(sPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -46,7 +47,7 @@ func (exporter *Exporter) PruneExports(ctx context.Context, progress io.Writer)
|
|||
path := filepath.Join(sPath, entry.Name())
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"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
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -18,7 +19,7 @@ func (home *Home) loadRedirect(ctx context.Context) (redirect Redirect, err erro
|
|||
redirect.Permanent = false
|
||||
|
||||
// load the overrides file
|
||||
overrides, err := home.Environment.Open(home.Config.Paths.OverridesJSON)
|
||||
overrides, err := os.Open(home.Config.Paths.OverridesJSON)
|
||||
if err != nil {
|
||||
return redirect, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import (
|
|||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
"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) {
|
||||
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)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "Unable to generate new host key")
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ func (ds Stack) compose(ctx context.Context, io stream.IOStream, args ...string)
|
|||
var err error
|
||||
ds.DockerExecutable, err = execx.LookPathAbs("docker")
|
||||
if err != nil {
|
||||
return environment.ExecCommandErrorFunc
|
||||
return execx.CommandErrorFunc
|
||||
}
|
||||
}
|
||||
return execx.Exec(ctx, io, ds.Dir, ds.DockerExecutable, append([]string{"compose"}, args...)...)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package dis
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/cli"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/config"
|
||||
"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.Triplestore = "triplestore:7200"
|
||||
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
|
||||
|
|
@ -57,7 +59,7 @@ func NewDistillery(params cli.Params, flags cli.Flags, req cli.Requirements) (di
|
|||
}
|
||||
|
||||
// open the config file!
|
||||
f, err := dis.Still.Environment.Open(params.ConfigPath)
|
||||
f, err := os.Open(params.ConfigPath)
|
||||
if err != nil {
|
||||
return nil, errOpenConfig.WithMessageF(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package extras
|
|||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
|
|
@ -82,7 +83,7 @@ func (wisski *Prefixes) database(ctx context.Context, server *phpx.Server) (pref
|
|||
func (prefixes *Prefixes) blocked() ([]string, error) {
|
||||
// open the resolver block file
|
||||
// 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 {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -123,7 +124,7 @@ func (wisski *Prefixes) filePrefixes() (prefixes []string, err error) {
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
file, err := wisski.Malt.Environment.Open(path)
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package environment
|
|||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Environment represents an environment that a program can run it.
|
||||
|
|
@ -11,31 +10,9 @@ import (
|
|||
type Environment interface {
|
||||
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)
|
||||
|
||||
Mkdir(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 {
|
||||
|
|
|
|||
|
|
@ -8,15 +8,6 @@ import (
|
|||
"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
|
||||
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]
|
||||
func ReadFile(env Environment, path string) ([]byte, error) {
|
||||
// open the file!
|
||||
file, err := env.Open(path)
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package environment
|
|||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
|
@ -26,46 +25,6 @@ func (n *Native) resetMask() {
|
|||
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) {
|
||||
n.ulock.Lock()
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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
|
||||
srcFile, err := env.Open(src)
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -64,20 +65,20 @@ func CopyLink(ctx context.Context, env environment.Environment, dst, src string)
|
|||
}
|
||||
|
||||
// read the link target
|
||||
target, err := env.Readlink(src)
|
||||
target, err := os.Readlink(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// delete it if it already exists
|
||||
if Exists(env, dst) {
|
||||
if err := env.Remove(dst); err != nil {
|
||||
if err := os.Remove(dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// make the symbolic link!
|
||||
return env.Symlink(target, dst)
|
||||
return os.Symlink(target, dst)
|
||||
}
|
||||
|
||||
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 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
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package fsx
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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) {
|
||||
{
|
||||
// stat both files
|
||||
info1, err1 := env.Stat(path1)
|
||||
info2, err2 := env.Stat(path2)
|
||||
info1, err1 := os.Stat(path1)
|
||||
info2, err2 := os.Stat(path2)
|
||||
|
||||
// both files exist => check using env.SameFile
|
||||
// the result is always authorative
|
||||
if err1 == nil && err2 == nil {
|
||||
same = env.SameFile(info1, info2)
|
||||
same = os.SameFile(info1, info2)
|
||||
authorative = true
|
||||
return
|
||||
}
|
||||
|
|
@ -68,8 +69,8 @@ func couldBeSameFile(env environment.Environment, path1, path2 string) (same, au
|
|||
|
||||
{
|
||||
// resolve paths absolutely
|
||||
rpath1, err1 := env.Abs(path1)
|
||||
rpath2, err2 := env.Abs(path2)
|
||||
rpath1, err1 := filepath.Abs(path1)
|
||||
rpath2, err2 := filepath.Abs(path2)
|
||||
|
||||
// if either path could not be resolved absolutely
|
||||
// fallback to just using clean!
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package fsx
|
|||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"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 {
|
||||
perm = environment.DefaultFilePerm
|
||||
}
|
||||
_, err := env.Stat(path)
|
||||
_, err := os.Stat(path)
|
||||
switch {
|
||||
case environment.IsNotExist(err):
|
||||
f, err := env.Create(path, perm)
|
||||
|
|
@ -29,6 +30,6 @@ func Touch(env environment.Environment, path string, perm fs.FileMode) error {
|
|||
return err
|
||||
default:
|
||||
now := time.Now().Local()
|
||||
return env.Chtimes(path, now, now)
|
||||
return os.Chtimes(path, now, now)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,30 +3,31 @@ package fsx
|
|||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
// Exists checks if the given path exists
|
||||
func Exists(env environment.Environment, path string) bool {
|
||||
_, err := env.Lstat(path)
|
||||
_, err := os.Lstat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsDirectory checks if the provided path exists and is a directory
|
||||
func IsDirectory(env environment.Environment, path string) bool {
|
||||
info, err := env.Stat(path)
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.Mode().IsDir()
|
||||
}
|
||||
|
||||
// IsFile checks if the provided path exists and is a regular file
|
||||
func IsFile(env environment.Environment, path string) bool {
|
||||
info, err := env.Stat(path)
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.Mode().IsRegular()
|
||||
}
|
||||
|
||||
// IsLink checks if the provided path exists and is a symlink
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"compress/gzip"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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()
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ func Package(env environment.Environment, dst, src string, onCopy func(rel strin
|
|||
}
|
||||
|
||||
// open the file
|
||||
handle, err := env.Open(path)
|
||||
handle, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package unpack
|
|||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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 {
|
||||
// create the destination
|
||||
dstStat, dstErr := env.Stat(dst)
|
||||
dstStat, dstErr := os.Stat(dst)
|
||||
switch {
|
||||
case environment.IsNotExist(dstErr):
|
||||
if err := env.MkdirAll(dst, srcInfo.Mode()); err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue