diff --git a/loader/loader.go b/loader/loader.go index b706bf6..031c664 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -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 diff --git a/loader/loader_test.go b/loader/loader_test.go index 20bad87..4dcd88c 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -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"}, }, diff --git a/main.go b/main.go index 38d8212..15176df 100644 --- a/main.go +++ b/main.go @@ -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 }, diff --git a/types/type.go b/types/type.go new file mode 100644 index 0000000..16a02a0 --- /dev/null +++ b/types/type.go @@ -0,0 +1,11 @@ +package types + +type OSType = int + +const ( + Ubuntu OSType = iota + Arch + Fedora + Alpine + Flatpak +)