changed PackageOperation to enums

This commit is contained in:
2025-02-28 17:39:08 -06:00
parent 6db5f73bc0
commit 8900bd70a4
8 changed files with 94 additions and 70 deletions

View File

@ -68,7 +68,7 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
} else {
// Handle package operations
if command.Type == PackageCT && command.PackageOperation == "checkVersion" {
if command.Type == PackageCT && command.PackageOperation == PackOppCheckVersion {
cmdCtxLogger.Info().Str("package", command.PackageName).Msg("Checking package versions")
// Execute the package version command

View File

@ -585,7 +585,7 @@ func processCmds(opts *ConfigOpts) error {
if cmd.PackageManager == "" {
return fmt.Errorf("package manager is required for package command %s", cmd.PackageName)
}
if cmd.PackageOperation == "" {
if cmd.PackageOperation.String() == "" {
return fmt.Errorf("package operation is required for package command %s", cmd.PackageName)
}
if cmd.PackageName == "" {
@ -594,15 +594,16 @@ func processCmds(opts *ConfigOpts) error {
var err error
// Validate the operation
switch cmd.PackageOperation {
case "install", "remove", "upgrade", "checkVersion":
if cmd.PackageOperation.IsAPackageOperation() {
cmd.pkgMan, err = pkgman.PackageManagerFactory(cmd.PackageManager, pkgman.WithoutAuth())
if err != nil {
return err
}
default:
} else {
return fmt.Errorf("unsupported package operation %s for command %s", cmd.PackageOperation, cmd.Name)
}
}
// Parse user commands

View File

@ -538,7 +538,7 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts)
case ScriptFileCT:
return command.runScriptFile(commandSession, cmdCtxLogger, &cmdOutBuf)
case PackageCT:
if command.PackageOperation == "checkVersion" {
if command.PackageOperation == PackOppCheckVersion {
commandSession.Stderr = nil
// Execute the package version command remotely
// Parse the output of package version command

View File

@ -106,7 +106,7 @@ type (
PackageVersion string `yaml:"packageVersion,omitempty"`
// PackageOperation specifies the action for package-related commands (e.g., "install" or "remove")
PackageOperation string `yaml:"packageOperation,omitempty"`
PackageOperation PackageOperation `yaml:"packageOperation,omitempty"`
pkgMan pkgman.PackageManager
@ -293,7 +293,10 @@ type (
ListName string // Name of the command list
Error error // Error encountered, if any
}
CommandType int
// use ints so we can use enums
CommandType int
PackageOperation int
)
//go:generate go run github.com/dmarkham/enumer -linecomment -yaml -text -json -type=CommandType
@ -305,3 +308,14 @@ const (
PackageCT // package
UserCT // user
)
//go:generate go run github.com/dmarkham/enumer -linecomment -yaml -text -json -type=PackageOperation
const (
DefaultPO PackageOperation = iota //
PackOppInstall // install
PackOppUpgrade // upgrade
PackOppPurge // purge
PackOppRemove // remove
PackOppCheckVersion // checkVersion
PackOppIsInstalled // isInstalled
)

View File

@ -277,13 +277,13 @@ func getCommandTypeAndSetCommandInfo(command *Command) *Command {
if command.Type == PackageCT && !command.packageCmdSet {
command.packageCmdSet = true
switch command.PackageOperation {
case "install":
case PackOppInstall:
command.Cmd, command.Args = command.pkgMan.Install(command.PackageName, command.PackageVersion, command.Args)
case "remove":
case PackOppRemove:
command.Cmd, command.Args = command.pkgMan.Remove(command.PackageName, command.Args)
case "upgrade":
case PackOppUpgrade:
command.Cmd, command.Args = command.pkgMan.Upgrade(command.PackageName, command.PackageVersion)
case "checkVersion":
case PackOppCheckVersion:
command.Cmd, command.Args = command.pkgMan.CheckVersion(command.PackageName, command.PackageVersion)
}
}