mirror of
https://github.com/chenasraf/sofmani.git
synced 2026-05-17 17:28:04 +00:00
refactor: improve platform map parsing
This commit is contained in:
26
Makefile
26
Makefile
@@ -17,3 +17,29 @@ install: build
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
rm -f ~/.local/bin/sofmani
|
||||
|
||||
.PHONY: precommit-install
|
||||
precommit-install:
|
||||
@echo "Installing pre-commit hooks..."
|
||||
@echo "#!/bin/sh\n\nmake precommit" > .git/hooks/pre-commit
|
||||
@chmod +x .git/hooks/pre-commit
|
||||
@echo "Pre-commit hooks installed."
|
||||
|
||||
.PHONY: precommit
|
||||
precommit:
|
||||
@STAGED_FILES=$$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.go$$'); \
|
||||
if [ -z "$$STAGED_FILES" ]; then \
|
||||
echo "No staged Go files to check."; \
|
||||
else \
|
||||
echo "Running pre-commit checks..."; \
|
||||
echo "go fmt"; \
|
||||
go fmt ./...; \
|
||||
echo "go vet"; \
|
||||
go vet ./...; \
|
||||
echo "gofmt"; \
|
||||
gofmt -w ./...; \
|
||||
echo "golangci-lint"; \
|
||||
golangci-lint run ./...; \
|
||||
echo "go test"; \
|
||||
go test -v ./...; \
|
||||
fi
|
||||
|
||||
@@ -112,12 +112,8 @@ func (i *DockerInstaller) GetOpts() *DockerOpts {
|
||||
if flags, ok := (*i.Info.Opts)["flags"].(string); ok {
|
||||
opts.Flags = &flags
|
||||
}
|
||||
if platformMap, ok := (*i.Info.Opts)["platform"].(map[string]*string); ok {
|
||||
opts.Platform = &platform.PlatformMap[string]{
|
||||
MacOS: platformMap["macos"],
|
||||
Linux: platformMap["linux"],
|
||||
Windows: platformMap["windows"],
|
||||
}
|
||||
if raw, ok := (*i.Info.Opts)["platform"]; ok && raw != nil {
|
||||
opts.Platform = platform.NewPlatformMap[string](raw)
|
||||
}
|
||||
}
|
||||
if skip, ok := (*i.Info.Opts)["skip_if_unavailable"].(bool); ok {
|
||||
|
||||
@@ -298,18 +298,8 @@ func (i *GitHubReleaseInstaller) GetOpts() *GitHubReleaseOpts {
|
||||
destination = utils.GetRealPath(i.GetData().Environ(), destination)
|
||||
opts.Destination = &destination
|
||||
}
|
||||
if filename, ok := (*info.Opts)["download_filename"].(string); ok {
|
||||
opts.DownloadFilename = &platform.PlatformMap[string]{
|
||||
MacOS: &filename,
|
||||
Linux: &filename,
|
||||
Windows: &filename,
|
||||
}
|
||||
} else if filenameMap, ok := (*info.Opts)["download_filename"].(map[string]*string); ok {
|
||||
opts.DownloadFilename = &platform.PlatformMap[string]{
|
||||
MacOS: filenameMap["macos"],
|
||||
Linux: filenameMap["linux"],
|
||||
Windows: filenameMap["windows"],
|
||||
}
|
||||
if filename, ok := (*info.Opts)["download_filename"]; ok {
|
||||
opts.DownloadFilename = platform.NewPlatformMap[string](filename)
|
||||
}
|
||||
if strategy, ok := (*info.Opts)["strategy"].(string); ok {
|
||||
strat := GitHubReleaseInstallStrategy(strings.ToLower(strategy))
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/chenasraf/sofmani/appconfig"
|
||||
"github.com/chenasraf/sofmani/logger"
|
||||
"github.com/chenasraf/sofmani/platform"
|
||||
"maps"
|
||||
)
|
||||
|
||||
// InstallerWithDefaults applies default configurations to an installer data object.
|
||||
@@ -23,37 +24,27 @@ func InstallerWithDefaults(
|
||||
if override.Opts != nil {
|
||||
source := *override.Opts
|
||||
target := *data.Opts
|
||||
for k, v := range source {
|
||||
target[k] = v
|
||||
}
|
||||
maps.Copy(target, source)
|
||||
}
|
||||
if override.Env != nil {
|
||||
source := *override.Env
|
||||
target := *data.Env
|
||||
for k, v := range source {
|
||||
target[k] = v
|
||||
}
|
||||
maps.Copy(target, source)
|
||||
}
|
||||
if override.PlatformEnv != nil {
|
||||
source := *override.PlatformEnv
|
||||
targetBase := *data.PlatformEnv
|
||||
if source.MacOS != nil && targetBase.MacOS != nil {
|
||||
target := *targetBase.MacOS
|
||||
for k, v := range *source.MacOS {
|
||||
target[k] = v
|
||||
}
|
||||
maps.Copy(target, *source.MacOS)
|
||||
}
|
||||
if source.Linux != nil && targetBase.Linux != nil {
|
||||
target := *targetBase.Linux
|
||||
for k, v := range *source.Linux {
|
||||
target[k] = v
|
||||
}
|
||||
maps.Copy(target, *source.Linux)
|
||||
}
|
||||
if source.Windows != nil && targetBase.Windows != nil {
|
||||
target := *targetBase.Windows
|
||||
for k, v := range *source.Windows {
|
||||
target[k] = v
|
||||
}
|
||||
maps.Copy(target, *source.Windows)
|
||||
}
|
||||
}
|
||||
if override.EnvShell != nil {
|
||||
|
||||
@@ -132,8 +132,17 @@ var DockerOSMap = PlatformMap[string]{
|
||||
Windows: strPtr("windows"),
|
||||
}
|
||||
|
||||
// NewPlatformMap creates a new PlatformMap from a map of platform strings to values.
|
||||
func NewPlatformMap[T any](values map[string]T) *PlatformMap[T] {
|
||||
// ParsePlatformSingleValue creates a new PlatformMap with the value for all platforms
|
||||
func ParsePlatformSingleValue[T any](value T) *PlatformMap[T] {
|
||||
p := &PlatformMap[T]{}
|
||||
p.MacOS = &value
|
||||
p.Linux = &value
|
||||
p.Windows = &value
|
||||
return p
|
||||
}
|
||||
|
||||
// ParselatformMap creates a PlatformMap from a map of platform strings to values.
|
||||
func ParselatformMap[T any](values map[string]T) *PlatformMap[T] {
|
||||
p := &PlatformMap[T]{}
|
||||
for k, v := range values {
|
||||
val := v // capture value for pointer
|
||||
@@ -150,3 +159,30 @@ func NewPlatformMap[T any](values map[string]T) *PlatformMap[T] {
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// NewPlatformMap creates a new PlatformMap from either a single value or a map.
|
||||
func NewPlatformMap[T any](input any) *PlatformMap[T] {
|
||||
switch v := input.(type) {
|
||||
case T:
|
||||
return ParsePlatformSingleValue(v)
|
||||
case *T:
|
||||
if v != nil {
|
||||
return ParsePlatformSingleValue(*v)
|
||||
}
|
||||
return nil
|
||||
case map[string]T:
|
||||
return ParselatformMap(v)
|
||||
case map[string]*T:
|
||||
flat := make(map[string]T)
|
||||
for k, ptr := range v {
|
||||
if ptr != nil {
|
||||
flat[k] = *ptr
|
||||
}
|
||||
}
|
||||
return ParselatformMap(flat)
|
||||
case nil:
|
||||
return nil
|
||||
default:
|
||||
panic(fmt.Sprintf("NewPlatformMap: unsupported input type %T", input))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user