Skip to content

Commit

Permalink
refactor: Split Package enviroment functions into util package
Browse files Browse the repository at this point in the history
  • Loading branch information
lcook committed Sep 17, 2023
1 parent 83f9084 commit 1f6855d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
15 changes: 1 addition & 14 deletions internal/mk/mk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,8 @@ import (
"github.com/spf13/viper"
)

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": CleanPath(viper.GetString("base")) + pkg.Origin,
}
}

func run(pkg *Package, makefile string) {
for k, v := range pkgEnv(pkg) {
os.Setenv(k, v)
}
SetPkgEnv(pkg)
scriptDir := CleanPath(viper.GetString("scriptdir"))
args := strings.Fields("make -f" + scriptDir + makefile)
cmd := exec.Command(args[0], args[1:]...)
Expand Down
22 changes: 22 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"os"
"path/filepath"
"strings"

. "github.com/lcook/portsync/internal/_package"
"github.com/spf13/viper"
)

func CleanPath(path string) string {
Expand All @@ -17,3 +20,22 @@ func CleanPath(path string) string {
}
return tmp
}

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": CleanPath(viper.GetString("base")) + pkg.Origin,
"PACKAGE_ROOT": CleanPath(viper.GetString("base")),
}
}
SetPkgEnv = func(pkg *Package) {
for k, v := range PkgEnv(pkg) {
os.Setenv(k, v)
}
}
)

0 comments on commit 1f6855d

Please sign in to comment.