Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the cli functions to use urfave/cli instead of cobra. #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/dev-helper/main.go
Expand Up @@ -79,12 +79,12 @@ func runCreateVMPlanTest(ctx context.Context, nodeNum int, sshKey string, logger
},
GRPCAPIEndpoint: "",
HTTPAPIEndpoint: "",
CtrSnapshotterKernel: defaults.ContainerdKernelSnapshotter,
CtrSnapshotterVolume: defaults.ContainerdVolumeSnapshotter,
CtrKernelSnapshotter: defaults.ContainerdKernelSnapshotter,
CtrVolumeSnapshotter: defaults.ContainerdVolumeSnapshotter,
CtrSocketPath: socketPath,
CtrNamespace: defaults.ContainerdNamespace,
FirecrackerBin: "/home/richard/bin/firecracker",
FirecrackerDetatch: false,
FirecrackerDetach: false,
FirecrackerUseAPI: true,
StateRootDir: defaults.StateRootDir,
ParentIface: "enp1s0",
Expand Down
13 changes: 6 additions & 7 deletions cmd/flintlockd/main.go
@@ -1,18 +1,17 @@
package main

import (
"log"
"fmt"
"os"

"github.com/weaveworks/flintlock/internal/command"
)

func main() {
rootCmd, err := command.NewRootCommand()
if err != nil {
log.Fatalln(err)
}
app := command.NewApp(os.Stdout)

if err := rootCmd.Execute(); err != nil {
log.Fatalln(err)
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "flintlockd: %s\n", err)
Copy link
Contributor

@jmickey jmickey Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be log.Error?

os.Exit(1)
}
}
15 changes: 5 additions & 10 deletions go.mod
Expand Up @@ -28,10 +28,8 @@ require (
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852
github.com/weaveworks/flintlock/api v0.0.0-00010101000000-000000000000
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
Expand All @@ -41,6 +39,7 @@ require (
)

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Microsoft/hcsshim v0.8.21 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
Expand All @@ -54,6 +53,7 @@ require (
github.com/containerd/ttrpc v1.0.2 // indirect
github.com/containernetworking/cni v0.8.1 // indirect
github.com/containernetworking/plugins v0.9.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
Expand All @@ -76,11 +76,8 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
Expand All @@ -95,9 +92,8 @@ require (
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
go.mongodb.org/mongo-driver v1.7.3 // indirect
go.opencensus.io v0.23.0 // indirect
Expand All @@ -106,7 +102,6 @@ require (
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20211021150943-2b146023228c // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
213 changes: 7 additions & 206 deletions go.sum

Large diffs are not rendered by default.

206 changes: 111 additions & 95 deletions internal/command/flags/flags.go
@@ -1,9 +1,8 @@
package flags

import (
"fmt"

"github.com/spf13/cobra"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"

"github.com/weaveworks/flintlock/internal/config"
"github.com/weaveworks/flintlock/pkg/defaults"
Expand All @@ -26,109 +25,126 @@ const (
)

// AddGRPCServerFlagsToCommand will add gRPC server flags to the supplied command.
func AddGRPCServerFlagsToCommand(cmd *cobra.Command, cfg *config.Config) {
cmd.Flags().StringVar(&cfg.GRPCAPIEndpoint,
grpcEndpointFlag,
defaults.GRPCAPIEndpoint,
"The endpoint for the gRPC server to listen on.")
func AddGRPCServerFlagsToCommand(cmd *cli.Command, cfg *config.Config) {
cmd.Flags = append(cmd.Flags, altsrc.NewStringFlag(&cli.StringFlag{
Name: grpcEndpointFlag,
Usage: "The endpoint for the gRPC server to listen on.",
Value: defaults.GRPCAPIEndpoint,
Destination: &cfg.GRPCAPIEndpoint,
}))
}

// AddGWServerFlagsToCommand will add gRPC HTTP gateway flags to the supplied command.
func AddGWServerFlagsToCommand(cmd *cobra.Command, cfg *config.Config) {
cmd.Flags().StringVar(&cfg.GRPCAPIEndpoint,
grpcEndpointFlag,
defaults.GRPCAPIEndpoint,
"The address of the gRPC server to act as a gateway for.")

cmd.Flags().StringVar(&cfg.HTTPAPIEndpoint,
httpEndpointFlag,
defaults.HTTPAPIEndpoint,
"The endpoint for the HTTP proxy to the gRPC service to listen on.")
func AddGWServerFlagsToCommand(cmd *cli.Command, cfg *config.Config) {
grpcFlag := altsrc.NewStringFlag(&cli.StringFlag{
Name: grpcEndpointFlag,
Usage: "The address of the gRPC server to act as a gateway for.",
Value: defaults.GRPCAPIEndpoint,
Destination: &cfg.GRPCAPIEndpoint,
})

httpFlag := altsrc.NewStringFlag(&cli.StringFlag{
Name: httpEndpointFlag,
Usage: "The endpoint for the HTTP proxy to the gRPC service to listen on.",
Value: defaults.HTTPAPIEndpoint,
Destination: &cfg.HTTPAPIEndpoint,
})

cmd.Flags = append(cmd.Flags, grpcFlag, httpFlag)
}

func AddNetworkFlagsToCommand(cmd *cobra.Command, cfg *config.Config) error {
cmd.Flags().StringVar(&cfg.ParentIface,
parentIfaceFlag,
"",
"The parent iface for the network interfaces. Note it could also be a bond")

if err := cmd.MarkFlagRequired(parentIfaceFlag); err != nil {
return fmt.Errorf("setting %s as required: %w", parentIfaceFlag, err)
}

return nil
func AddNetworkFlagsToCommand(cmd *cli.Command, cfg *config.Config) {
cmd.Flags = append(cmd.Flags, altsrc.NewStringFlag(&cli.StringFlag{
Name: parentIfaceFlag,
Usage: "The parent iface for the network interfaces. Note it could also be a bond.",
Value: "",
Destination: &cfg.ParentIface,
}))
}

func AddHiddenFlagsToCommand(cmd *cobra.Command, cfg *config.Config) error {
cmd.Flags().BoolVar(&cfg.DisableReconcile,
disableReconcileFlag,
false,
"Set to true to stop the reconciler running")

cmd.Flags().IntVar(&cfg.MaximumRetry,
maximumRetryFlag,
defaults.MaximumRetry,
"Number of times to retry failed reconciliation")

cmd.Flags().BoolVar(&cfg.DisableAPI,
disableAPIFlag,
false,
"Set to true to stop the api server running")

if err := cmd.Flags().MarkHidden(disableReconcileFlag); err != nil {
return fmt.Errorf("setting %s as hidden: %w", disableReconcileFlag, err)
}

if err := cmd.Flags().MarkHidden(maximumRetryFlag); err != nil {
return fmt.Errorf("setting %s as hidden: %w", maximumRetryFlag, err)
}

if err := cmd.Flags().MarkHidden(disableAPIFlag); err != nil {
return fmt.Errorf("setting %s as hidden: %w", disableAPIFlag, err)
}

return nil
func AddHiddenFlagsToCommand(cmd *cli.Command, cfg *config.Config) {
disableReconcileFlag := altsrc.NewBoolFlag(&cli.BoolFlag{
Name: disableReconcileFlag,
Usage: "Set to true to disable the reconciler.",
Value: false,
Hidden: true,
Destination: &cfg.DisableReconcile,
})

maximumRetryFlag := altsrc.NewIntFlag(&cli.IntFlag{
Name: maximumRetryFlag,
Usage: "The maximum number of times to retry a failed reconciliation.",
Value: defaults.MaximumRetry,
Hidden: true,
Destination: &cfg.MaximumRetry,
})

disableAPIFlag := altsrc.NewBoolFlag(&cli.BoolFlag{
Name: disableAPIFlag,
Usage: "Set to true to disable the API server.",
Value: false,
Hidden: true,
Destination: &cfg.DisableAPI,
})

cmd.Flags = append(cmd.Flags, disableReconcileFlag, maximumRetryFlag, disableAPIFlag)
}

// AddFirecrackerFlagsToCommand will add the firecracker provider specific flags to the supplied cobra command.
func AddFirecrackerFlagsToCommand(cmd *cobra.Command, cfg *config.Config) error {
cmd.Flags().StringVar(&cfg.FirecrackerBin,
firecrackerBinFlag,
defaults.FirecrackerBin,
"The path to the firecracker binary to use.")
cmd.Flags().BoolVar(&cfg.FirecrackerDetatch,
firecrackerDetachFlag,
defaults.FirecrackerDetach,
"If true the child firecracker processes will be detached from the parent flintlock process.")
cmd.Flags().BoolVar(&cfg.FirecrackerUseAPI,
firecrackerAPIFlag,
defaults.FirecrackerUseAPI,
"Indicates that the Firecracker API should be used to configure the microvm.")

return nil
func AddFirecrackerFlagsToCommand(cmd *cli.Command, cfg *config.Config) {
firecrackerBinFlag := altsrc.NewStringFlag(&cli.StringFlag{
Name: firecrackerBinFlag,
Usage: "The path to the firecracker binary.",
Value: defaults.FirecrackerBin,
Destination: &cfg.FirecrackerBin,
})

firecrackerDetachFlag := altsrc.NewBoolFlag(&cli.BoolFlag{
Name: firecrackerDetachFlag,
Usage: "Set to true to detach the firecracker process from the parent process.",
Value: defaults.FirecrackerDetach,
Destination: &cfg.FirecrackerDetach,
})

firecrackerAPIFlag := altsrc.NewBoolFlag(&cli.BoolFlag{
Name: firecrackerAPIFlag,
Usage: "Set to true to enable the firecracker API to configure the microvm.",
Value: defaults.FirecrackerUseAPI,
Destination: &cfg.FirecrackerUseAPI,
})

cmd.Flags = append(cmd.Flags, firecrackerBinFlag, firecrackerDetachFlag, firecrackerAPIFlag)
}

// AddContainerDFlagsToCommand will add the containerd specific flags to the supplied cobra command.
func AddContainerDFlagsToCommand(cmd *cobra.Command, cfg *config.Config) error {
cmd.Flags().StringVar(&cfg.CtrSocketPath,
containerdSocketFlag,
defaults.ContainerdSocket,
"The path to the containerd socket.")

cmd.Flags().StringVar(&cfg.CtrSnapshotterKernel,
kernelSnapshotterFlag,
defaults.ContainerdKernelSnapshotter,
"The name of the snapshotter to use with containerd for kernel/initrd images.")

cmd.Flags().StringVar(&cfg.CtrSnapshotterVolume,
volSnapshotterFlag,
defaults.ContainerdVolumeSnapshotter,
"The name of the snapshotter to use with containerd for volume images.")

cmd.Flags().StringVar(&cfg.CtrNamespace,
containerdNamespace,
defaults.ContainerdNamespace,
"The name of the containerd namespace to use.")

return nil
func AddContainerDFlagsToCommand(cmd *cli.Command, cfg *config.Config) {
containerdSocketFlag := altsrc.NewPathFlag(&cli.PathFlag{
Name: containerdSocketFlag,
Usage: "The path to the containerd socket.",
Value: defaults.ContainerdSocket,
Destination: &cfg.CtrSocketPath,
})

volSnapshotterFlag := altsrc.NewStringFlag(&cli.StringFlag{
Name: volSnapshotterFlag,
Usage: "The volume snapshotter to use.",
Value: defaults.ContainerdVolumeSnapshotter,
Destination: &cfg.CtrVolumeSnapshotter,
})

kernelSnapshotterFlag := altsrc.NewStringFlag(&cli.StringFlag{
Name: kernelSnapshotterFlag,
Usage: "The kernel snapshotter to use.",
Value: defaults.ContainerdKernelSnapshotter,
Destination: &cfg.CtrKernelSnapshotter,
})

containerdNamespaceFlag := altsrc.NewStringFlag(&cli.StringFlag{
Name: containerdNamespace,
Usage: "The containerd namespace to use.",
Value: defaults.ContainerdNamespace,
Destination: &cfg.CtrNamespace,
})

cmd.Flags = append(cmd.Flags, containerdSocketFlag, volSnapshotterFlag, kernelSnapshotterFlag, containerdNamespaceFlag)
}