Move workspace path functions to sub-package of cmd

Signed-off-by: Daniel Weiße <dw@edgeless.systems>
This commit is contained in:
Daniel Weiße 2023-08-08 15:42:06 +02:00 committed by Daniel Weiße
parent 99c579b45a
commit 89b342900f
21 changed files with 213 additions and 189 deletions

View file

@ -0,0 +1,38 @@
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
/*
Package pathprefix is used to print correct filepaths for a configured workspace.
The default workspace is the current working directory.
Users may override the default workspace using the --workspace flag.
The functions defined here should be used when printing any filepath to the user,
as they might otherwise be incorrect if the user has changed the workspace.
The prefixer MUST not be used when accessing files, as the workspace is changed
using os.Chdir() before the command is executed.
*/
package pathprefix
import (
"path/filepath"
)
// PathPrefixer is used to prefix paths with the configured workspace.
type PathPrefixer struct {
workspace string
}
// New returns a new PathPrefixer.
func New(workspace string) PathPrefixer {
return PathPrefixer{workspace: workspace}
}
// PrefixPath prefixes the given path with the configured workspace.
func (p PathPrefixer) PrefixPath(path string) string {
return filepath.Clean(filepath.Join(p.workspace, path))
}