Skip to content

Commit

Permalink
refactor: Use CleanPath for path sanitization
Browse files Browse the repository at this point in the history
While here break out the package enviroment map into a seperate
function, which we may use elsewhere in the future.
  • Loading branch information
lcook committed Sep 1, 2023
1 parent 5d0a895 commit 7154c6c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
12 changes: 2 additions & 10 deletions internal/fetcher/portscout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 10 additions & 21 deletions internal/mk/mk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 7154c6c

Please sign in to comment.