From 31b0945a153bd2337f948b4f382cfcf2e13b94cf Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sun, 21 May 2023 20:31:47 +0000 Subject: [PATCH] fix: use the default config dirs provider --- container/container.go | 20 +++----------------- images.go | 16 +++------------- kernel/kernel.go | 20 ++++---------------- main.go | 11 ++--------- preload.go | 11 ++++------- 5 files changed, 16 insertions(+), 62 deletions(-) diff --git a/container/container.go b/container/container.go index 53ec791..5cf56d0 100644 --- a/container/container.go +++ b/container/container.go @@ -10,7 +10,6 @@ import ( "fmt" "os" "os/exec" - "os/user" "path" "path/filepath" "regexp" @@ -99,22 +98,9 @@ func New(name string, timeout time.Duration) (c Container, err error) { c.name = name c.timeout = timeout - usr, err := user.Current() - if err != nil { - return - } - - c.Volumes.LibModules = fmt.Sprintf( - "%s/.out-of-tree/volumes/%s/lib/modules", usr.HomeDir, name) - os.MkdirAll(c.Volumes.LibModules, 0777) - - c.Volumes.UsrSrc = fmt.Sprintf( - "%s/.out-of-tree/volumes/%s/usr/src", usr.HomeDir, name) - os.MkdirAll(c.Volumes.UsrSrc, 0777) - - c.Volumes.Boot = fmt.Sprintf( - "%s/.out-of-tree/volumes/%s/boot", usr.HomeDir, name) - os.MkdirAll(c.Volumes.Boot, 0777) + c.Volumes.LibModules = config.Dir("volumes", name, "lib", "modules") + c.Volumes.LibModules = config.Dir("volumes", name, "usr", "src") + c.Volumes.LibModules = config.Dir("volumes", name, "boot") return } diff --git a/images.go b/images.go index ef96b38..8662620 100644 --- a/images.go +++ b/images.go @@ -8,7 +8,7 @@ import ( "errors" "fmt" "os" - "os/user" + "path/filepath" "strings" "time" @@ -25,12 +25,7 @@ type ImageCmd struct { type ImageListCmd struct{} func (cmd *ImageListCmd) Run(g *Globals) (err error) { - usr, err := user.Current() - if err != nil { - return - } - - entries, err := os.ReadDir(usr.HomeDir + "/.out-of-tree/images/") + entries, err := os.ReadDir(config.Dir("images")) if err != nil { return } @@ -48,12 +43,7 @@ type ImageEditCmd struct { } func (cmd *ImageEditCmd) Run(g *Globals) (err error) { - usr, err := user.Current() - if err != nil { - return - } - - image := usr.HomeDir + "/.out-of-tree/images/" + cmd.Name + image := filepath.Join(config.Dir("images"), cmd.Name) if !fs.PathExists(image) { fmt.Println("image does not exist") } diff --git a/kernel/kernel.go b/kernel/kernel.go index 0e5b499..c068e48 100644 --- a/kernel/kernel.go +++ b/kernel/kernel.go @@ -12,7 +12,7 @@ import ( "os" "os/exec" "os/signal" - "os/user" + "path/filepath" "regexp" "runtime" "strings" @@ -326,16 +326,10 @@ func findInitrdFile(files []os.FileInfo, kname string) (name string, err error) } func GenRootfsImage(d container.Image, download bool) (rootfs string, err error) { - usr, err := user.Current() - if err != nil { - return - } + imagesPath := config.Dir("images") imageFile := d.Name + ".img" - imagesPath := usr.HomeDir + "/.out-of-tree/images/" - os.MkdirAll(imagesPath, os.ModePerm) - - rootfs = imagesPath + imageFile + rootfs = filepath.Join(imagesPath, imageFile) if !fs.PathExists(rootfs) { if download { log.Info().Msgf("%v not available, start download", imageFile) @@ -395,13 +389,7 @@ func UpdateKernelsCfg(host, download bool) (err error) { buf = append([]byte("# Autogenerated\n# DO NOT EDIT\n\n"), buf...) - usr, err := user.Current() - if err != nil { - return - } - - // TODO move all cfg path values to one provider - kernelsCfgPath := usr.HomeDir + "/.out-of-tree/kernels.toml" + kernelsCfgPath := config.File("kernels.toml") err = ioutil.WriteFile(kernelsCfgPath, buf, 0644) if err != nil { return diff --git a/main.go b/main.go index d05cbef..24a6f79 100644 --- a/main.go +++ b/main.go @@ -11,8 +11,6 @@ import ( "net/url" "os" "os/exec" - "os/user" - "path/filepath" "runtime/debug" "strconv" "time" @@ -138,11 +136,6 @@ func main() { loglevel = zerolog.ErrorLevel } - usr, err := user.Current() - if err != nil { - return - } - consoleWriter = LevelWriter{Writer: zerolog.NewConsoleWriter( func(w *zerolog.ConsoleWriter) { w.Out = os.Stderr @@ -152,7 +145,7 @@ func main() { } fileWriter = LevelWriter{Writer: &lumberjack.Logger{ - Filename: usr.HomeDir + "/.out-of-tree/logs/out-of-tree.log", + Filename: config.File("logs/out-of-tree.log"), }, Level: zerolog.TraceLevel, } @@ -171,7 +164,7 @@ func main() { log.Debug().Msgf("%v", buildInfo.Settings) } - path := filepath.Join(usr.HomeDir, ".out-of-tree") + path := config.Dir() yes, err := fs.CaseInsensitive(path) if err != nil { log.Fatal().Err(err).Msg(path) diff --git a/preload.go b/preload.go index dad2745..6a8e448 100644 --- a/preload.go +++ b/preload.go @@ -9,7 +9,6 @@ import ( "encoding/hex" "errors" "os" - "os/user" "path/filepath" "time" @@ -115,12 +114,10 @@ func buildPreload(workPath, tmp string, ki config.KernelInfo, return } -func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, err error) { - usr, err := user.Current() - if err != nil { - return - } - base := filepath.Join(usr.HomeDir, "/.out-of-tree/preload/") +func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, + err error) { + + base := config.Dir("preload") workPath = filepath.Join(base, "/repos/", sha1sum(repo)) var r *git.Repository