Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*.so
*.dylib

# Binary output at root (build output should be in bin/)
syspkg

# Test binary, built with `go test -c`
*.test

Expand Down
91 changes: 46 additions & 45 deletions cmd/syspkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
package main

import (
"context"
"fmt"
"log"
"os"
"strings"

// "github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"

"github.com/bluet/syspkg"
"github.com/bluet/syspkg/manager"
Expand Down Expand Up @@ -64,15 +65,15 @@ func main() {
}

// Set up the CLI application.
app := &cli.App{
app := &cli.Command{
Name: "syspkg",
Usage: "A universal system package manager",
EnableBashCompletion: true,
EnableShellCompletion: true,
UseShortOptionHandling: true,
Suggest: true,
// Action: func(c *cli.Context) error {
// var opts = getOptions(c)
// pms = filterPackageManager(pms, c)
// Action: func(ctx context.Context, cmd *cli.Command) error {
// var opts = getOptions(cmd)
// pms = filterPackageManager(pms, cmd)

// log.Printf("Listing upgradable packages for %T...\n", pms)
// listUpgradablePackages(pms, opts)
Expand All @@ -84,13 +85,13 @@ func main() {
Name: "install",
Aliases: []string{"i"},
Usage: "Install packages",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)

log.Printf("Installing packages for %T...\n", pms)

pkgNames := c.Args().Slice()
pkgNames := cmd.Args().Slice()
for _, pm := range pms {
log.Printf("Installing packages for %T...\n", pm)
packages, err := pm.Install(pkgNames, opts)
Expand All @@ -107,10 +108,10 @@ func main() {
Name: "delete",
Aliases: []string{"remove", "uninstall", "d", "rm", "un"},
Usage: "Delete packages",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
pkgNames := c.Args().Slice()
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)
pkgNames := cmd.Args().Slice()

log.Printf("Deleting packages... for %T\n", pms)

Expand All @@ -130,9 +131,9 @@ func main() {
Name: "refresh",
Aliases: []string{"update", "r", "re", "u", "up"},
Usage: "Refresh package list",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)

log.Printf("Refreshing package list... for %T\n", pms)
for _, pm := range pms {
Expand All @@ -151,9 +152,9 @@ func main() {
Name: "upgrade",
Aliases: []string{"U", "ug"},
Usage: "Upgrade packages",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)

log.Printf("Upgrading packages... for %T\n", pms)

Expand All @@ -178,10 +179,10 @@ func main() {
Name: "find",
Aliases: []string{"search", "f"},
Usage: "Find matching packages",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
keywords := c.Args().Slice()
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)
keywords := cmd.Args().Slice()

if len(keywords) == 0 {
fmt.Println("Please specify keywords to search.")
Expand Down Expand Up @@ -209,14 +210,14 @@ func main() {
Aliases: []string{"s"},
Usage: "Please specify a subcommand. " + "Use `syspkg show --help` to see the subcommands.",
Description: `Show information. Please specify a subcommand. Use ` + "`syspkg show --help`" + ` to see the subcommands. Usage: ` + "`syspkg show [subcommand]`",
Subcommands: []*cli.Command{
Commands: []*cli.Command{
{
Name: "upgradable",
Aliases: []string{"u"},
Usage: "Show upgradable packages",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)

log.Println("Showing upgradable packages...")

Expand All @@ -228,10 +229,10 @@ func main() {
Name: "package",
Aliases: []string{"p"},
Usage: "Show package information",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
pkgNames := c.Args().Slice()
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)
pkgNames := cmd.Args().Slice()

if len(pkgNames) != 1 {
fmt.Println("Please specify one and only one package name.")
Expand All @@ -258,9 +259,9 @@ func main() {
Name: "installed",
Aliases: []string{"i"},
Usage: "Show installed packages",
Action: func(c *cli.Context) error {
var opts = getOptions(c)
pms = filterPackageManager(pms, c)
Action: func(_ context.Context, cmd *cli.Command) error {
var opts = getOptions(cmd)
pms = filterPackageManager(pms, cmd)

log.Println("Showing installed packages...")

Expand Down Expand Up @@ -358,20 +359,20 @@ func main() {
}

// Run the CLI application.
err = app.Run(os.Args)
err = app.Run(context.Background(), os.Args)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}

// getOptions extracts options from the CLI context and returns a manager.Options struct.
func getOptions(c *cli.Context) *manager.Options {
// getOptions extracts options from the CLI command and returns a manager.Options struct.
func getOptions(cmd *cli.Command) *manager.Options {
var opts manager.Options
opts.Verbose = c.Bool("verbose")
opts.DryRun = c.Bool("dry-run")
opts.Interactive = c.Bool("interactive")
opts.Debug = c.Bool("debug")
opts.Verbose = cmd.Bool("verbose")
opts.DryRun = cmd.Bool("dry-run")
opts.Interactive = cmd.Bool("interactive")
opts.Debug = cmd.Bool("debug")

if !opts.Interactive {
opts.AssumeYes = true
Expand All @@ -381,19 +382,19 @@ func getOptions(c *cli.Context) *manager.Options {
}

// filterPackageManager filters the available package managers based on user input.
func filterPackageManager(availablePMs map[string]syspkg.PackageManager, c *cli.Context) map[string]syspkg.PackageManager {
func filterPackageManager(availablePMs map[string]syspkg.PackageManager, cmd *cli.Command) map[string]syspkg.PackageManager {
if len(availablePMs) == 0 {
log.Fatal("No package managers available!")
}

// if no specific package manager is specified, use all available
if !c.Bool("apt") && !c.Bool("flatpak") && !c.Bool("snap") && !c.Bool("yum") && !c.Bool("dnf") && !c.Bool("pacman") && !c.Bool("apk") && !c.Bool("zypper") {
if !cmd.Bool("apt") && !cmd.Bool("flatpak") && !cmd.Bool("snap") && !cmd.Bool("yum") && !cmd.Bool("dnf") && !cmd.Bool("pacman") && !cmd.Bool("apk") && !cmd.Bool("zypper") {
return availablePMs
}

var wantedPMs = make(map[string]syspkg.PackageManager)
for name, pm := range availablePMs {
if c.Bool(name) {
if cmd.Bool(name) {
wantedPMs[name] = pm
}
}
Expand Down
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@ module github.com/bluet/syspkg

go 1.23

require github.com/urfave/cli/v2 v2.27.7

require (
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
)
require github.com/urfave/cli/v3 v3.8.0
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/urfave/cli/v3 v3.8.0 h1:XqKPrm0q4P0q5JpoclYoCAv0/MIvH/jZ2umzuf8pNTI=
github.com/urfave/cli/v3 v3.8.0/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading