Skip to content
Open
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
6 changes: 3 additions & 3 deletions cmd/rhc/canonical_facts_cmd.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main

import (
"context"
"encoding/json"
"fmt"

"github.com/urfave/cli/v2"

"github.com/redhatinsights/rhc/internal/canonical_facts"
"github.com/urfave/cli/v3"
)

// canonicalFactAction tries to gather canonical facts about system,
// and it prints JSON with facts to stdout.
func canonicalFactAction(_ *cli.Context) error {
func canonicalFactAction(_ context.Context, _ *cli.Command) error {
// NOTE: CLI context is not useful for anything
facts, err := canonical_facts.GetCanonicalFacts()
if err != nil {
Expand Down
53 changes: 27 additions & 26 deletions cmd/rhc/collector_cmd.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net"
"strings"

"github.com/emersion/go-varlink"
"github.com/urfave/cli/v2"

"github.com/redhatinsights/rhc/internal/collector"
"github.com/redhatinsights/rhc/internal/ui"
"github.com/redhatinsights/rhc/varlink/collectorapi"
"github.com/urfave/cli/v3"
)

const rhcServerSocket = "/run/rhc/com.redhat.rhc"
Expand All @@ -36,19 +37,19 @@ func newCollectorClient() (*collectorapi.Client, func(), error) {
}

// beforeCollectorInfoAction validates the collector info command arguments and configuration.
func beforeCollectorInfoAction(ctx *cli.Context) error {
return validateCollectorCommand(ctx, true, true)
func beforeCollectorInfoAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
return ctx, validateCollectorCommand(cmd, true, true)
}

// collectorInfoAction retrieves and displays information for a specific collector.
func collectorInfoAction(ctx *cli.Context) error {
logCommandStart(ctx)
func collectorInfoAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)
client, cleanup, err := newCollectorClient()
if err != nil {
return cli.Exit(fmt.Sprintf("failed to connect to rhc-server: %v", err), ExitCodeErr)
}
defer cleanup()
response, err := client.Info(&collectorapi.InfoIn{Id: ctx.Args().First()})
response, err := client.Info(&collectorapi.InfoIn{Id: cmd.Args().First()})
if err != nil {
return cli.Exit(fmt.Sprintf("failed to get collector info: %v", err), ExitCodeErr)
}
Expand All @@ -57,13 +58,13 @@ func collectorInfoAction(ctx *cli.Context) error {
}

// beforeCollectorListAction validates the collector list command configuration.
func beforeCollectorListAction(ctx *cli.Context) error {
return validateCollectorCommand(ctx, false, true)
func beforeCollectorListAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
return ctx, validateCollectorCommand(cmd, false, true)
}

// collectorListAction retrieves and displays a list of all available collectors.
func collectorListAction(ctx *cli.Context) error {
logCommandStart(ctx)
func collectorListAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)

client, cleanup, err := newCollectorClient()
if err != nil {
Expand Down Expand Up @@ -100,13 +101,13 @@ func collectorListAction(ctx *cli.Context) error {
}

// beforeCollectorTimersAction validates the collector timers command configuration.
func beforeCollectorTimersAction(ctx *cli.Context) error {
return validateCollectorCommand(ctx, false, true)
func beforeCollectorTimersAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
return ctx, validateCollectorCommand(cmd, false, true)
}

// collectorTimersAction retrieves and displays timer information for all collectors.
func collectorTimersAction(ctx *cli.Context) error {
logCommandStart(ctx)
func collectorTimersAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)

client, cleanup, err := newCollectorClient()
if err != nil {
Expand All @@ -129,15 +130,15 @@ func collectorTimersAction(ctx *cli.Context) error {
}

// beforeCollectorEnableAction validates the collector enable command arguments.
func beforeCollectorEnableAction(ctx *cli.Context) error {
return validateCollectorCommand(ctx, true, false)
func beforeCollectorEnableAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
return ctx, validateCollectorCommand(cmd, true, false)
}

// collectorEnableAction enables a collector timer and optionally triggers immediate collection.
func collectorEnableAction(ctx *cli.Context) error {
logCommandStart(ctx)
collectorId := ctx.Args().First()
nowFlag := ctx.Bool("now")
func collectorEnableAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)
collectorId := cmd.Args().First()
nowFlag := cmd.Bool("now")
conn, timerName, err := collector.ValidateCollectorAndConnect(collectorId)
if err != nil {
return cli.Exit(fmt.Sprintf("%v", err), ExitCodeErr)
Expand Down Expand Up @@ -166,15 +167,15 @@ func collectorEnableAction(ctx *cli.Context) error {
}

// beforeCollectorDisableAction validates the collector disable command arguments.
func beforeCollectorDisableAction(ctx *cli.Context) error {
return validateCollectorCommand(ctx, true, false)
func beforeCollectorDisableAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
return ctx, validateCollectorCommand(cmd, true, false)
}

// collectorDisableAction disables a collector timer and optionally stops immediate collection.
func collectorDisableAction(ctx *cli.Context) error {
logCommandStart(ctx)
collectorId := ctx.Args().First()
nowFlag := ctx.Bool("now")
func collectorDisableAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)
collectorId := cmd.Args().First()
nowFlag := cmd.Bool("now")
conn, timerName, err := collector.ValidateCollectorAndConnect(collectorId)
if err != nil {
return cli.Exit(fmt.Sprintf("%v", err), ExitCodeErr)
Expand Down
89 changes: 45 additions & 44 deletions cmd/rhc/configure_features_cmd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"
"text/tabwriter"

"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"

"github.com/redhatinsights/rhc/internal/rhsm"
"github.com/redhatinsights/rhc/pkg/feature"
Expand All @@ -21,30 +22,30 @@ import (
// TODO Use ui.Icons.Ok when we have UTF-8 capable tabwriter

// beforeFeaturesStatusAction validates inputs before executing the status action.
func beforeFeaturesStatusAction(ctx *cli.Context) error {
err := checkFormatFlag(ctx)
func beforeFeaturesStatusAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
err := checkFormatFlag(cmd)
if err != nil {
return err
return ctx, err
}
configureUI(ctx)
return checkForUnknownArgs(ctx)
configureUI(cmd)
return ctx, checkForUnknownArgs(cmd)
}

// featuresStatusAction displays the current status or preferences of all features.
func featuresStatusAction(ctx *cli.Context) error {
logCommandStart(ctx)
func featuresStatusAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)
isRegistered, err := rhsm.IsRHSMRegistered()
if err != nil {
return err
}

if isRegistered {
return featuresStatusActionRegistered(ctx)
return featuresStatusActionRegistered(ctx, cmd)
}
return featuresStatusActionNotRegistered(ctx)
return featuresStatusActionNotRegistered(ctx, cmd)
}

func featuresStatusActionNotRegistered(_ *cli.Context) error {
func featuresStatusActionNotRegistered(_ context.Context, _ *cli.Command) error {
cache, err := prefcache.LoadCache(ConnectFeaturesPrefsPath)
if err != nil {
return err
Expand All @@ -68,7 +69,7 @@ func featuresStatusActionNotRegistered(_ *cli.Context) error {
return nil
}

func featuresStatusActionRegistered(_ *cli.Context) error {
func featuresStatusActionRegistered(_ context.Context, _ *cli.Command) error {
fmt.Println("Connected to Red Hat.")
fmt.Println("")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
Expand All @@ -88,39 +89,39 @@ func featuresStatusActionRegistered(_ *cli.Context) error {
}

// beforeFeaturesEnableAction validates inputs before executing the enable action.
func beforeFeaturesEnableAction(ctx *cli.Context) error {
err := checkFormatFlag(ctx)
func beforeFeaturesEnableAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
err := checkFormatFlag(cmd)
if err != nil {
return err
return ctx, err
}
configureUI(ctx)
configureUI(cmd)

if ctx.Args().Len() != 1 {
return cli.Exit("this command requires a single FEATURE argument", ExitCodeUsage)
if cmd.Args().Len() != 1 {
return ctx, cli.Exit("this command requires a single FEATURE argument", ExitCodeUsage)
}
if _, err = feature.Get(ctx.Args().First()); err != nil {
return cli.Exit(err.Error(), ExitCodeDataErr)
if _, err = feature.Get(cmd.Args().First()); err != nil {
return ctx, cli.Exit(err.Error(), ExitCodeDataErr)
}
return nil
return ctx, nil
}

// featuresEnableAction enables a single feature.
func featuresEnableAction(ctx *cli.Context) error {
logCommandStart(ctx)
func featuresEnableAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)
isRegistered, err := rhsm.IsRHSMRegistered()
if err != nil {
return cli.Exit(fmt.Sprintf("failed to check registration status: %v", err), ExitCodeSoftware)
}

requestedFeature := ctx.Args().First()
requestedFeature := cmd.Args().First()
if isRegistered {
return featuresEnableActionRegistered(ctx, requestedFeature)
return featuresEnableActionRegistered(ctx, cmd, requestedFeature)
}
return featuresEnableActionNotRegistered(ctx, requestedFeature)
return featuresEnableActionNotRegistered(ctx, cmd, requestedFeature)
}

// featuresEnableActionNotRegistered handles enabling a feature on a non-registered system.
func featuresEnableActionNotRegistered(_ *cli.Context, targetName string) error {
func featuresEnableActionNotRegistered(_ context.Context, _ *cli.Command, targetName string) error {
cache, err := prefcache.LoadCache(ConnectFeaturesPrefsPath)
if err != nil {
return cli.Exit(fmt.Sprintf("failed to load feature preferences: %v", err), ExitCodeSoftware)
Expand Down Expand Up @@ -164,7 +165,7 @@ func featuresEnableActionNotRegistered(_ *cli.Context, targetName string) error
}

// featuresEnableActionRegistered handles enabling a feature on a registered system.
func featuresEnableActionRegistered(_ *cli.Context, targetName string) error {
func featuresEnableActionRegistered(_ context.Context, _ *cli.Command, targetName string) error {
target := feature.MustGet(targetName)

// enable required features
Expand Down Expand Up @@ -203,39 +204,39 @@ func featuresEnableActionRegistered(_ *cli.Context, targetName string) error {
}

// beforeFeaturesDisableAction validates inputs before executing the disable action.
func beforeFeaturesDisableAction(ctx *cli.Context) error {
err := checkFormatFlag(ctx)
func beforeFeaturesDisableAction(ctx context.Context, cmd *cli.Command) (context.Context, error) {
err := checkFormatFlag(cmd)
if err != nil {
return err
return ctx, err
}
configureUI(ctx)
configureUI(cmd)

if ctx.Args().Len() != 1 {
return cli.Exit("this command requires a single FEATURE argument", ExitCodeUsage)
if cmd.Args().Len() != 1 {
return ctx, cli.Exit("this command requires a single FEATURE argument", ExitCodeUsage)
}
if _, err = feature.Get(ctx.Args().First()); err != nil {
return cli.Exit(err.Error(), ExitCodeDataErr)
if _, err = feature.Get(cmd.Args().First()); err != nil {
return ctx, cli.Exit(err.Error(), ExitCodeDataErr)
}
return nil
return ctx, nil
}

// featuresDisableAction disables a single feature.
func featuresDisableAction(ctx *cli.Context) error {
logCommandStart(ctx)
func featuresDisableAction(ctx context.Context, cmd *cli.Command) error {
logCommandStart(cmd)
isRegistered, err := rhsm.IsRHSMRegistered()
if err != nil {
return cli.Exit(fmt.Sprintf("failed to check registration status: %v", err), ExitCodeSoftware)
}

requestedFeature := ctx.Args().First()
requestedFeature := cmd.Args().First()
if isRegistered {
return featuresDisableActionRegistered(ctx, requestedFeature)
return featuresDisableActionRegistered(ctx, cmd, requestedFeature)
}
return featuresDisableActionNotRegistered(ctx, requestedFeature)
return featuresDisableActionNotRegistered(ctx, cmd, requestedFeature)
}

// featuresDisableActionNotRegistered handles disabling a feature on a non-registered system.
func featuresDisableActionNotRegistered(_ *cli.Context, targetName string) error {
func featuresDisableActionNotRegistered(_ context.Context, _ *cli.Command, targetName string) error {
cache, err := prefcache.LoadCache(ConnectFeaturesPrefsPath)
if err != nil {
return cli.Exit(fmt.Sprintf("failed to load feature preferences: %v", err), ExitCodeSoftware)
Expand Down Expand Up @@ -279,7 +280,7 @@ func featuresDisableActionNotRegistered(_ *cli.Context, targetName string) error
}

// featuresDisableActionRegistered handles disabling a feature on a registered system.
func featuresDisableActionRegistered(_ *cli.Context, targetName string) error {
func featuresDisableActionRegistered(_ context.Context, _ *cli.Command, targetName string) error {
target := feature.MustGet(targetName)

// disable dependent features
Expand Down
Loading
Loading