2023-01-19 14:34:17 +01:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"pikman/alpine"
|
|
|
|
"pikman/arch"
|
|
|
|
"pikman/fedora"
|
|
|
|
"pikman/flatpak"
|
2023-01-19 14:45:24 +01:00
|
|
|
"pikman/types"
|
2023-01-19 14:34:17 +01:00
|
|
|
"pikman/ubuntu"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-01-19 17:19:14 +01:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:45:24 +01:00
|
|
|
func ProcessCommand(command string, osType types.OSType, containerName string, packageName []string) error {
|
2023-01-19 14:34:17 +01:00
|
|
|
var err error
|
2023-01-19 14:45:24 +01:00
|
|
|
if osType != types.Ubuntu && osType != types.Flatpak && containerName != "" {
|
2023-01-19 17:19:14 +01:00
|
|
|
packageName = append([]string{"--name " + containerName}, packageName...)
|
2023-01-19 14:34:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 17:19:14 +01:00
|
|
|
commandToExecute, err := getCommand(command, osType, packageName)
|
2023-01-19 14:34:17 +01:00
|
|
|
cmd := exec.Command("/bin/sh", "-c", commandToExecute)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
err = cmd.Run()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:19:14 +01:00
|
|
|
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
|
2023-01-19 14:34:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-19 17:19:14 +01:00
|
|
|
return "", fmt.Errorf("%s: is not a valid command for this distro", command)
|
2023-01-19 14:34:17 +01:00
|
|
|
}
|