2023-07-20 09:26:25 +00:00
|
|
|
package workdir
|
|
|
|
|
2024-07-25 16:44:58 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2024-07-25 16:44:58 +00:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateWorkdir(ctx context.Context, path string) (workdir string, err error) {
|
|
|
|
log, err := logr.FromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
if len(path) > 0 {
|
2024-07-25 16:44:58 +00:00
|
|
|
log.Info("Creating a new directory", "path", path)
|
2023-07-20 09:26:25 +00:00
|
|
|
// Create a dir using the path
|
|
|
|
if err := os.Mkdir(path, 0777); err != nil {
|
|
|
|
return path, err
|
|
|
|
}
|
|
|
|
// TODO(@allanger): I've got a feeling that it doesn't have to look that bad
|
|
|
|
workdir = path
|
|
|
|
} else {
|
2024-07-25 16:44:58 +00:00
|
|
|
log.Info("Path is not set, creating a temporary directory")
|
2023-07-20 09:26:25 +00:00
|
|
|
// Create a temporary dir
|
|
|
|
workdir, err = os.MkdirTemp("", "shoebill")
|
|
|
|
if err != nil {
|
|
|
|
return workdir, err
|
|
|
|
}
|
2024-07-25 17:34:10 +00:00
|
|
|
}
|
2023-07-20 09:26:25 +00:00
|
|
|
|
2024-07-25 17:34:10 +00:00
|
|
|
if err := os.Mkdir(workdir+"/.charts", os.ModePerm); err != nil {
|
|
|
|
return "", err
|
2023-07-20 09:26:25 +00:00
|
|
|
}
|
|
|
|
return workdir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveWorkdir(path string) (err error) {
|
|
|
|
return os.RemoveAll(path)
|
|
|
|
}
|