Simplify loader code vastly
This commit is contained in:
parent
a23e8bab6b
commit
8e4495b99f
@ -13,15 +13,29 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var commandsMap = map[types.OSType]map[string]string{
|
||||||
|
types.Ubuntu: ubuntu.Commands,
|
||||||
|
types.Arch: arch.Commands,
|
||||||
|
types.Fedora: fedora.Commands,
|
||||||
|
types.Alpine: alpine.Commands,
|
||||||
|
types.Flatpak: flatpak.Commands,
|
||||||
|
}
|
||||||
|
|
||||||
|
var packageManagerMap = map[types.OSType]string{
|
||||||
|
types.Ubuntu: ubuntu.PackageManager,
|
||||||
|
types.Arch: arch.PackageManager,
|
||||||
|
types.Fedora: fedora.PackageManager,
|
||||||
|
types.Alpine: alpine.PackageManager,
|
||||||
|
types.Flatpak: flatpak.PackageManager,
|
||||||
|
}
|
||||||
|
|
||||||
func ProcessCommand(command string, osType types.OSType, containerName string, packageName []string) error {
|
func ProcessCommand(command string, osType types.OSType, containerName string, packageName []string) error {
|
||||||
var err error
|
var err error
|
||||||
if osType != types.Ubuntu && osType != types.Flatpak && containerName != "" {
|
if osType != types.Ubuntu && osType != types.Flatpak && containerName != "" {
|
||||||
containerName = "--name " + containerName
|
packageName = append([]string{"--name " + containerName}, packageName...)
|
||||||
} else {
|
|
||||||
containerName = ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
commandToExecute, err := getCommand(command, osType, containerName, packageName)
|
commandToExecute, err := getCommand(command, osType, packageName)
|
||||||
cmd := exec.Command("/bin/sh", "-c", commandToExecute)
|
cmd := exec.Command("/bin/sh", "-c", commandToExecute)
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
@ -35,44 +49,13 @@ func ProcessCommand(command string, osType types.OSType, containerName string, p
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCommand(command string, osType types.OSType, containerName string, packageName []string) (string, error) {
|
func getCommand(command string, osType types.OSType, packageName []string) (string, error) {
|
||||||
switch osType {
|
commandMap := commandsMap[osType]
|
||||||
case types.Arch:
|
cmd, ok := commandMap[command]
|
||||||
cmd, ok := arch.Commands[command]
|
|
||||||
if ok {
|
if ok {
|
||||||
return fmt.Sprintf("%s %s %s %s", arch.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil
|
return fmt.Sprintf("%s %s %s", packageManagerMap[osType], cmd, strings.Join(packageName, " ")), nil
|
||||||
} else {
|
|
||||||
return "", fmt.Errorf("%s: is not a valid command for Arch", command)
|
|
||||||
}
|
|
||||||
case types.Fedora:
|
|
||||||
cmd, ok := fedora.Commands[command]
|
|
||||||
if ok {
|
|
||||||
return fmt.Sprintf("%s %s %s %s", fedora.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil
|
|
||||||
} else {
|
|
||||||
return "", fmt.Errorf("%s: is not a valid command for Fedora", command)
|
|
||||||
}
|
|
||||||
case types.Flatpak:
|
|
||||||
cmd, ok := flatpak.Commands[command]
|
|
||||||
if ok {
|
|
||||||
return fmt.Sprintf("%s %s %s", flatpak.PackageManager, cmd, strings.Join(packageName, " ")), nil
|
|
||||||
} else {
|
|
||||||
return "", fmt.Errorf("%s: is not a valid command for Flatpak", command)
|
|
||||||
}
|
|
||||||
case types.Alpine:
|
|
||||||
cmd, ok := alpine.Commands[command]
|
|
||||||
if ok {
|
|
||||||
return fmt.Sprintf("%s %s %s %s", alpine.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil
|
|
||||||
} else {
|
|
||||||
return "", fmt.Errorf("%s: is not a valid command for Alpine", command)
|
|
||||||
}
|
|
||||||
case types.Ubuntu:
|
|
||||||
cmd, ok := ubuntu.Commands[command]
|
|
||||||
if ok {
|
|
||||||
return fmt.Sprintf("%s %s %s", ubuntu.PackageManager, cmd, strings.Join(packageName, " ")), nil
|
|
||||||
} else {
|
|
||||||
return "", fmt.Errorf("%s: is not a valid command for Ubuntu", command)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("%s: was passed without a valid backend", command)
|
return "", fmt.Errorf("%s: is not a valid command for this distro", command)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ func Test_getCommand(t *testing.T) {
|
|||||||
type args struct {
|
type args struct {
|
||||||
command string
|
command string
|
||||||
osType types.OSType
|
osType types.OSType
|
||||||
containerName string
|
|
||||||
packageName []string
|
packageName []string
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
@ -23,7 +22,6 @@ func Test_getCommand(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
command: "install",
|
command: "install",
|
||||||
osType: types.Ubuntu,
|
osType: types.Ubuntu,
|
||||||
containerName: "",
|
|
||||||
packageName: []string{"testPackage"},
|
packageName: []string{"testPackage"},
|
||||||
},
|
},
|
||||||
want: "sudo -S nala install testPackage",
|
want: "sudo -S nala install testPackage",
|
||||||
@ -34,7 +32,6 @@ func Test_getCommand(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
command: "install",
|
command: "install",
|
||||||
osType: types.Arch,
|
osType: types.Arch,
|
||||||
containerName: "",
|
|
||||||
packageName: []string{"testPackage"},
|
packageName: []string{"testPackage"},
|
||||||
},
|
},
|
||||||
want: "apx --aur install testPackage",
|
want: "apx --aur install testPackage",
|
||||||
@ -45,29 +42,16 @@ func Test_getCommand(t *testing.T) {
|
|||||||
args: args{
|
args: args{
|
||||||
command: "install",
|
command: "install",
|
||||||
osType: types.Arch,
|
osType: types.Arch,
|
||||||
containerName: "--name testName",
|
packageName: []string{"--name testName", "testPackage"},
|
||||||
packageName: []string{"testPackage"},
|
|
||||||
},
|
},
|
||||||
want: "apx --aur install --name testName testPackage",
|
want: "apx --aur install --name testName testPackage",
|
||||||
wantErr: false,
|
wantErr: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "Ubuntu single package, container name not used",
|
|
||||||
args: args{
|
|
||||||
command: "install",
|
|
||||||
osType: types.Ubuntu,
|
|
||||||
containerName: "--name testName",
|
|
||||||
packageName: []string{"testPackage"},
|
|
||||||
},
|
|
||||||
want: "sudo -S nala install testPackage",
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "Ubuntu invalid command should return nothing and error",
|
name: "Ubuntu invalid command should return nothing and error",
|
||||||
args: args{
|
args: args{
|
||||||
command: "init",
|
command: "init",
|
||||||
osType: types.Ubuntu,
|
osType: types.Ubuntu,
|
||||||
containerName: "",
|
|
||||||
packageName: []string{"testPackage"},
|
packageName: []string{"testPackage"},
|
||||||
},
|
},
|
||||||
want: "",
|
want: "",
|
||||||
@ -76,7 +60,7 @@ func Test_getCommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got, err := getCommand(tt.args.command, tt.args.osType, tt.args.containerName, tt.args.packageName)
|
got, err := getCommand(tt.args.command, tt.args.osType, tt.args.packageName)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("getCommand() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("getCommand() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user