Skip to content

Commit

Permalink
refactor(cmd/driver): renamed select to config and prepare to `…
Browse files Browse the repository at this point in the history
…install`.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Nov 9, 2023
1 parent 31fb5e5 commit 9803df6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
29 changes: 14 additions & 15 deletions cmd/driver/select/select.go → cmd/driver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package driverselect
package driverconfig

import (
"fmt"
Expand All @@ -30,8 +30,7 @@ import (
"github.com/falcosecurity/falcoctl/pkg/options"
)

// driverSelectOptions contains the options for the driver prepare command.
type driverSelectOptions struct {
type driverConfigOptions struct {
*options.Common
Type string
Version string
Expand All @@ -41,19 +40,19 @@ type driverSelectOptions struct {
Update bool
}

// NewDriverSelectCmd selects a driver and stores it in config.
func NewDriverSelectCmd(ctx context.Context, opt *options.Common) *cobra.Command {
o := driverSelectOptions{
// NewDriverConfigCmd configures a driver and stores it in config.
func NewDriverConfigCmd(ctx context.Context, opt *options.Common) *cobra.Command {
o := driverConfigOptions{
Common: opt,
}

cmd := &cobra.Command{
Use: "select [flags]",
Use: "config [flags]",
DisableFlagsInUseLine: true,
Short: "Select a driver",
Long: "Select a driver for future usages with other driver subcommands",
Short: "Configure a driver",
Long: "Configure a driver for future usages with other driver subcommands",
RunE: func(cmd *cobra.Command, args []string) error {
return o.RunDriverSelect(ctx)
return o.RunDriverConfig(ctx)
},
}

Expand All @@ -66,8 +65,8 @@ func NewDriverSelectCmd(ctx context.Context, opt *options.Common) *cobra.Command
return cmd
}

// RunDriverSelect implements the driver selection command.
func (o *driverSelectOptions) RunDriverSelect(_ context.Context) error {
// RunDriverConfig implements the driver configuration command.
func (o *driverConfigOptions) RunDriverConfig(_ context.Context) error {
var (
dType drivertype.DriverType
err error
Expand All @@ -91,7 +90,7 @@ func (o *driverSelectOptions) RunDriverSelect(_ context.Context) error {
driverCfg.HostRoot = o.HostRoot
}
if viper.IsSet("type") {
o.Printer.Logger.Info("Running falcoctl driver select", o.Printer.Logger.Args(
o.Printer.Logger.Info("Running falcoctl driver config", o.Printer.Logger.Args(
"driver type", o.Type,
"autoselection", "disabled"))

Expand All @@ -107,7 +106,7 @@ func (o *driverSelectOptions) RunDriverSelect(_ context.Context) error {
return err
}

o.Printer.Logger.Info("Running falcoctl driver select", o.Printer.Logger.Args(
o.Printer.Logger.Info("Running falcoctl driver config", o.Printer.Logger.Args(
"arch", info.Architecture.ToNonDeb(),
"kernel release", info.String(),
"kernel version", info.KernelVersion,
Expand All @@ -131,5 +130,5 @@ func (o *driverSelectOptions) RunDriverSelect(_ context.Context) error {
return err
}
}
return config.SelectDriver(&driverCfg, o.ConfigFile)
return config.StoreDriver(&driverCfg, o.ConfigFile)
}
4 changes: 2 additions & 2 deletions cmd/driver/prepare/doc.go → cmd/driver/config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package driverprepare defines the options and prepare logic for the driver cmd.
package driverprepare
// Package driverconfig defines the configure logic for the driver cmd.
package driverconfig
8 changes: 4 additions & 4 deletions cmd/driver/driver_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/spf13/cobra"

drivercleanup "github.com/falcosecurity/falcoctl/cmd/driver/cleanup"
driverprepare "github.com/falcosecurity/falcoctl/cmd/driver/prepare"
driverconfig "github.com/falcosecurity/falcoctl/cmd/driver/config"
driverinstall "github.com/falcosecurity/falcoctl/cmd/driver/install"
driverprintenv "github.com/falcosecurity/falcoctl/cmd/driver/printenv"
driverselect "github.com/falcosecurity/falcoctl/cmd/driver/select"
"github.com/falcosecurity/falcoctl/internal/config"
commonoptions "github.com/falcosecurity/falcoctl/pkg/options"
)
Expand All @@ -44,8 +44,8 @@ func NewDriverCmd(ctx context.Context, opt *commonoptions.Common) *cobra.Command
},
}

cmd.AddCommand(driverprepare.NewDriverPrepareCmd(ctx, opt))
cmd.AddCommand(driverselect.NewDriverSelectCmd(ctx, opt))
cmd.AddCommand(driverinstall.NewDriverInstallCmd(ctx, opt))
cmd.AddCommand(driverconfig.NewDriverConfigCmd(ctx, opt))
cmd.AddCommand(drivercleanup.NewDriverCleanupCmd(ctx, opt))
cmd.AddCommand(driverprintenv.NewDriverPrintenvCmd(ctx, opt))
return cmd
Expand Down
4 changes: 2 additions & 2 deletions cmd/driver/select/doc.go → cmd/driver/install/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package driverselect defines the options and selection logic for the driver cmd.
package driverselect
// Package driverinstall defines the installation logic for the driver cmd.
package driverinstall
25 changes: 12 additions & 13 deletions cmd/driver/prepare/prepare.go → cmd/driver/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package driverprepare
package driverinstall

import (
"crypto/tls"
Expand All @@ -36,8 +36,7 @@ type driverDownloadOptions struct {
HTTPTimeout time.Duration
}

// DriverPrepareOptions contains the options for the driver prepare command.
type driverPrepareOptions struct {
type driverInstallOptions struct {
*options.Common
Download bool
Compile bool
Expand All @@ -46,26 +45,26 @@ type driverPrepareOptions struct {
driverDownloadOptions
}

// NewDriverPrepareCmd returns the driver prepare command.
func NewDriverPrepareCmd(ctx context.Context, opt *options.Common) *cobra.Command {
o := driverPrepareOptions{
// NewDriverInstallCmd returns the driver install command.
func NewDriverInstallCmd(ctx context.Context, opt *options.Common) *cobra.Command {
o := driverInstallOptions{
Common: opt,
// Defaults to downloading or building if needed
Download: true,
Compile: true,
}

cmd := &cobra.Command{
Use: "prepare [flags]",
Use: "install [flags]",
DisableFlagsInUseLine: true,
Short: "Prepare previously selected driver",
Long: "Prepare previously selected driver, either downloading it or attempting a build",
Short: "Install previously configured driver",
Long: "Install previously configured driver, either downloading it or attempting a build",
RunE: func(cmd *cobra.Command, args []string) error {
driver, err := config.Driverer()
if err != nil {
return err
}
dest, err := o.RunDriverPrepare(ctx, &driver)
dest, err := o.RunDriverInstall(ctx, &driver)
if dest != "" {
// We don't care about errors at this stage
// Fallback: try to load any available driver if leaving with an error.
Expand Down Expand Up @@ -98,14 +97,14 @@ func setDefaultHTTPClientOpts(downloadOptions driverDownloadOptions) {
http.DefaultClient.Timeout = downloadOptions.HTTPTimeout
}

// RunDriverPrepare implements the driver prepare command.
func (o *driverPrepareOptions) RunDriverPrepare(_ context.Context, driver *config.Driver) (string, error) {
// RunDriverInstall implements the driver install command.
func (o *driverInstallOptions) RunDriverInstall(_ context.Context, driver *config.Driver) (string, error) {
kr, err := driverkernel.FetchInfo(o.DriverKernelRelease, o.DriverKernelVersion)
if err != nil {
return "", err
}

o.Printer.Logger.Info("Running falcoctl driver prepare", o.Printer.Logger.Args(
o.Printer.Logger.Info("Running falcoctl driver install", o.Printer.Logger.Args(
"driver version", driver.Version,
"driver type", driver.Type,
"driver name", driver.Name,
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ func Driverer() (Driver, error) {
return drvCfg, nil
}

// SelectDriver stores a driver as selected in config file.
func SelectDriver(driver *Driver, configFile string) error {
// StoreDriver stores a driver conf in config file.
func StoreDriver(driver *Driver, configFile string) error {
if err := UpdateConfigFile(DriverKey, driver, configFile); err != nil {
return fmt.Errorf("unable to update driver in the config file %q: %w", configFile, err)
}
Expand Down

0 comments on commit 9803df6

Please sign in to comment.