From 7154c6c1ce0fc46091df4b06f08dc044da8551bd Mon Sep 17 00:00:00 2001 From: Lewis Cook Date: Fri, 1 Sep 2023 14:39:40 +0100 Subject: [PATCH] refactor: Use `CleanPath` for path sanitization While here break out the package enviroment map into a seperate function, which we may use elsewhere in the future. --- internal/fetcher/portscout.go | 12 ++---------- internal/mk/mk.go | 31 ++++++++++--------------------- internal/util/util.go | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 internal/util/util.go diff --git a/internal/fetcher/portscout.go b/internal/fetcher/portscout.go index 36251eb..e25a74c 100644 --- a/internal/fetcher/portscout.go +++ b/internal/fetcher/portscout.go @@ -10,12 +10,12 @@ import ( "fmt" "os" "os/exec" - "path/filepath" "slices" "strings" "sync" . "github.com/lcook/portsync/internal/_package" + . "github.com/lcook/portsync/internal/util" "github.com/mmcdole/gofeed" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -87,15 +87,7 @@ func (ps Portscout) Fetch(cmd *cobra.Command) (*Packages, error) { } func (ps Portscout) Transform(cmd *cobra.Command, pkg *Package) (*Package, error) { - base := viper.GetString("base") - if strings.HasPrefix(base, "~/") { - dir, _ := os.UserHomeDir() - base = filepath.Join(dir, base[2:]) - } - if !strings.HasSuffix(base, "/") { - base += "/" - } - pkgPath := base + pkg.Origin + pkgPath := CleanPath(viper.GetString("base")) + pkg.Origin if _, err := os.Stat(pkgPath); os.IsNotExist(err) { return &Package{}, err } diff --git a/internal/mk/mk.go b/internal/mk/mk.go index d5e2e36..b9681dd 100644 --- a/internal/mk/mk.go +++ b/internal/mk/mk.go @@ -9,40 +9,29 @@ package mk import ( "os" "os/exec" - "path/filepath" "strings" . "github.com/lcook/portsync/internal/_package" + . "github.com/lcook/portsync/internal/util" "github.com/spf13/viper" ) -func run(pkg *Package, makefile string) { - base := viper.GetString("base") - if strings.HasPrefix(base, "~/") { - dir, _ := os.UserHomeDir() - base = filepath.Join(dir, base[2:]) - } - if !strings.HasSuffix(base, "/") { - base += "/" - } - for k, v := range map[string]string{ +var pkgEnv = func(pkg *Package) map[string]string { + return map[string]string{ "PACKAGE_ORIGIN": pkg.Origin, "PACKAGE_VERSION": pkg.Version, "PACKAGE_LATEST": pkg.Latest, "PACKAGE_MAINTAINER": pkg.Maintainer, "PACKAGE_TYPE": pkg.Type, - "PACKAGE_DIR": base + pkg.Origin, - } { - os.Setenv(k, v) - } - scriptDir := viper.GetString("scriptdir") - if strings.HasPrefix(scriptDir, "~/") { - dir, _ := os.UserHomeDir() - base = filepath.Join(dir, scriptDir[2:]) + "PACKAGE_DIR": CleanPath(viper.GetString("base")) + pkg.Origin, } - if !strings.HasSuffix(scriptDir, "/") { - scriptDir += "/" +} + +func run(pkg *Package, makefile string) { + for k, v := range pkgEnv(pkg) { + os.Setenv(k, v) } + scriptDir := CleanPath(viper.GetString("scriptdir")) args := strings.Fields("make -f" + scriptDir + makefile) cmd := exec.Command(args[0], args[1:]...) cmd.Stdout = os.Stdout diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..bfef637 --- /dev/null +++ b/internal/util/util.go @@ -0,0 +1,19 @@ +package util + +import ( + "os" + "path/filepath" + "strings" +) + +func CleanPath(path string) string { + tmp := path + if strings.HasPrefix(path, "~/") { + dir, _ := os.UserHomeDir() + tmp = filepath.Join(dir, path[2:]) + } + if !strings.HasSuffix(path, "/") { + tmp += "/" + } + return tmp +}