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