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

View File

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

21
main.go
View File

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