Remove duplicate of types and split into types packages

This commit is contained in:
ferrreo 2023-01-19 13:45:24 +00:00
parent 2626263e8c
commit 8291385905
4 changed files with 36 additions and 40 deletions

View File

@ -8,23 +8,14 @@ import (
"pikman/arch" "pikman/arch"
"pikman/fedora" "pikman/fedora"
"pikman/flatpak" "pikman/flatpak"
"pikman/types"
"pikman/ubuntu" "pikman/ubuntu"
"strings" "strings"
) )
type OSType = int func ProcessCommand(command string, osType types.OSType, containerName string, packageName []string) error {
const (
Ubuntu OSType = iota
Arch
Fedora
Alpine
Flatpak
)
func ProcessCommand(command string, osType OSType, containerName string, packageName []string) error {
var err error var err error
if osType != Ubuntu && osType != Flatpak && containerName != "" { if osType != types.Ubuntu && osType != types.Flatpak && containerName != "" {
containerName = "--name " + containerName containerName = "--name " + containerName
} else { } else {
containerName = "" containerName = ""
@ -44,37 +35,37 @@ func ProcessCommand(command string, osType OSType, containerName string, package
return nil return nil
} }
func getCommand(command string, osType OSType, containerName string, packageName []string) (string, error) { func getCommand(command string, osType types.OSType, containerName string, packageName []string) (string, error) {
switch osType { switch osType {
case Arch: case types.Arch:
cmd, ok := arch.Commands[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 %s", arch.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil
} else { } else {
return "", fmt.Errorf("%s: is not a valid command for Arch", command) return "", fmt.Errorf("%s: is not a valid command for Arch", command)
} }
case Fedora: case types.Fedora:
cmd, ok := fedora.Commands[command] cmd, ok := fedora.Commands[command]
if ok { if ok {
return fmt.Sprintf("%s %s %s %s", fedora.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil return fmt.Sprintf("%s %s %s %s", fedora.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil
} else { } else {
return "", fmt.Errorf("%s: is not a valid command for Fedora", command) return "", fmt.Errorf("%s: is not a valid command for Fedora", command)
} }
case Flatpak: case types.Flatpak:
cmd, ok := flatpak.Commands[command] cmd, ok := flatpak.Commands[command]
if ok { if ok {
return fmt.Sprintf("%s %s %s", flatpak.PackageManager, cmd, strings.Join(packageName, " ")), nil return fmt.Sprintf("%s %s %s", flatpak.PackageManager, cmd, strings.Join(packageName, " ")), nil
} else { } else {
return "", fmt.Errorf("%s: is not a valid command for Flatpak", command) return "", fmt.Errorf("%s: is not a valid command for Flatpak", command)
} }
case Alpine: case types.Alpine:
cmd, ok := alpine.Commands[command] cmd, ok := alpine.Commands[command]
if ok { if ok {
return fmt.Sprintf("%s %s %s %s", alpine.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil return fmt.Sprintf("%s %s %s %s", alpine.PackageManager, cmd, containerName, strings.Join(packageName, " ")), nil
} else { } else {
return "", fmt.Errorf("%s: is not a valid command for Alpine", command) return "", fmt.Errorf("%s: is not a valid command for Alpine", command)
} }
case Ubuntu: case types.Ubuntu:
cmd, ok := ubuntu.Commands[command] cmd, ok := ubuntu.Commands[command]
if ok { if ok {
return fmt.Sprintf("%s %s %s", ubuntu.PackageManager, cmd, strings.Join(packageName, " ")), nil return fmt.Sprintf("%s %s %s", ubuntu.PackageManager, cmd, strings.Join(packageName, " ")), nil

View File

@ -1,11 +1,14 @@
package loader package loader
import "testing" import (
"pikman/types"
"testing"
)
func Test_getCommand(t *testing.T) { func Test_getCommand(t *testing.T) {
type args struct { type args struct {
command string command string
osType OSType osType types.OSType
containerName string containerName string
packageName []string packageName []string
} }
@ -19,7 +22,7 @@ func Test_getCommand(t *testing.T) {
name: "Ubuntu single package", name: "Ubuntu single package",
args: args{ args: args{
command: "install", command: "install",
osType: Ubuntu, osType: types.Ubuntu,
containerName: "", containerName: "",
packageName: []string{"testPackage"}, packageName: []string{"testPackage"},
}, },
@ -30,7 +33,7 @@ func Test_getCommand(t *testing.T) {
name: "Arch single package", name: "Arch single package",
args: args{ args: args{
command: "install", command: "install",
osType: Arch, osType: types.Arch,
containerName: "", containerName: "",
packageName: []string{"testPackage"}, packageName: []string{"testPackage"},
}, },
@ -41,7 +44,7 @@ func Test_getCommand(t *testing.T) {
name: "Arch single package with container name", name: "Arch single package with container name",
args: args{ args: args{
command: "install", command: "install",
osType: Arch, osType: types.Arch,
containerName: "--name testName", containerName: "--name testName",
packageName: []string{"testPackage"}, packageName: []string{"testPackage"},
}, },
@ -52,7 +55,7 @@ func Test_getCommand(t *testing.T) {
name: "Ubuntu single package, container name not used", name: "Ubuntu single package, container name not used",
args: args{ args: args{
command: "install", command: "install",
osType: Ubuntu, osType: types.Ubuntu,
containerName: "--name testName", containerName: "--name testName",
packageName: []string{"testPackage"}, packageName: []string{"testPackage"},
}, },
@ -63,7 +66,7 @@ func Test_getCommand(t *testing.T) {
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: Ubuntu, osType: types.Ubuntu,
containerName: "", containerName: "",
packageName: []string{"testPackage"}, packageName: []string{"testPackage"},
}, },

21
main.go
View File

@ -5,21 +5,12 @@ import (
"log" "log"
"os" "os"
"pikman/loader" "pikman/loader"
) "pikman/types"
type OSType = int
const (
Ubuntu OSType = iota
Arch
Fedora
Alpine
Flatpak
) )
func main() { func main() {
osType := Ubuntu osType := types.Ubuntu
containerName := "" containerName := ""
cli.VersionFlag = &cli.BoolFlag{ cli.VersionFlag = &cli.BoolFlag{
@ -40,7 +31,7 @@ func main() {
Usage: "Install Arch packages (including from the AUR)", Usage: "Install Arch packages (including from the AUR)",
Action: func(cCtx *cli.Context, b bool) error { Action: func(cCtx *cli.Context, b bool) error {
if b { if b {
osType = Arch osType = types.Arch
} }
return nil return nil
}, },
@ -51,7 +42,7 @@ func main() {
Usage: "Install Fedora packages", Usage: "Install Fedora packages",
Action: func(cCtx *cli.Context, b bool) error { Action: func(cCtx *cli.Context, b bool) error {
if b { if b {
osType = Fedora osType = types.Fedora
} }
return nil return nil
}, },
@ -62,7 +53,7 @@ func main() {
Usage: "Install Alpine packages", Usage: "Install Alpine packages",
Action: func(cCtx *cli.Context, b bool) error { Action: func(cCtx *cli.Context, b bool) error {
if b { if b {
osType = Alpine osType = types.Alpine
} }
return nil return nil
}, },
@ -73,7 +64,7 @@ func main() {
Usage: "Install Flatpak packages", Usage: "Install Flatpak packages",
Action: func(cCtx *cli.Context, b bool) error { Action: func(cCtx *cli.Context, b bool) error {
if b { if b {
osType = Flatpak osType = types.Flatpak
} }
return nil return nil
}, },

11
types/type.go Normal file
View File

@ -0,0 +1,11 @@
package types
type OSType = int
const (
Ubuntu OSType = iota
Arch
Fedora
Alpine
Flatpak
)