Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
feat: Switching backend to spf13/cobra (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirac-cisco authored Jun 30, 2023
1 parent 7ccbe55 commit 6da5cf0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
95 changes: 52 additions & 43 deletions backend/cmd/backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/urfave/cli"

"github.com/openclarity/vmclarity/backend/pkg/backend"
"github.com/openclarity/vmclarity/backend/pkg/config"
Expand All @@ -34,61 +34,70 @@ import (
const (
LogLevelFlag = "log-level"
LogLevelDefaultValue = "warning"
ExecutableName = "vmclarity-backend"
)

func run(cliCtx *cli.Context) {
log.InitLogger(cliCtx.String(LogLevelFlag), os.Stderr)

ctx := context.Background()
logger := logrus.WithContext(ctx)
ctx = log.SetLoggerForContext(ctx, logger)
backend.Run(ctx)
}

func versionCommand(_ *cli.Context) {
fmt.Printf("Version: %s \nCommit: %s\nBuild Time: %s",
version.Version, version.CommitHash, version.BuildTimestamp)
}
var (
logLevel = LogLevelDefaultValue
rootCmd = &cobra.Command{
Use: ExecutableName,
Short: "VMClarity Backend",
Long: "VMClarity Backend",
Version: fmt.Sprintf("Version: %s \nCommit: %s\nBuild Time: %s",
version.Version, version.CommitHash, version.BuildTimestamp),
SilenceUsage: true,
}
)

func main() {
func init() {
viper.SetDefault(config.HealthCheckAddress, ":8081")
viper.SetDefault(config.BackendRestPort, "8888")
viper.SetDefault(config.DatabaseDriver, databaseTypes.DBDriverTypeLocal)
viper.SetDefault(config.DisableOrchestrator, "false")
viper.SetDefault(config.UISitePath, "/app/site")
viper.AutomaticEnv()
app := cli.NewApp()
app.Usage = ""
app.Name = "VMClarity"
app.Version = version.Version

runCommand := cli.Command{
Name: "run",
Usage: "Starts VMClarity",
Action: run,
Flags: []cli.Flag{
cli.StringFlag{
Name: LogLevelFlag,
Value: LogLevelDefaultValue,
Usage: fmt.Sprintf("Set log level %s", logrus.AllLevels),
},
},
cmdRun := cobra.Command{
Use: "run",
Run: runCommand,
Short: "Starts the server",
Long: "Starts the VMClarity backend server",
Example: ExecutableName + " run",
}
runCommand.UsageText = runCommand.Name
cmdRun.PersistentFlags().StringVar(&logLevel,
LogLevelFlag,
LogLevelDefaultValue,
"Set log level [panic fatal error warning info debug trace]")

versionCommand := cli.Command{
Name: "version",
Usage: "VMClarity Version Details",
Action: versionCommand,
cmdVersion := cobra.Command{
Use: "version",
Run: versionCommand,
Short: "Displays the version",
Long: "Displays the version of the VMClarity backend server",
Example: ExecutableName + " version",
}
versionCommand.UsageText = versionCommand.Name

app.Commands = []cli.Command{
runCommand,
versionCommand,
}
rootCmd.AddCommand(&cmdRun)
rootCmd.AddCommand(&cmdVersion)
}

if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
func main() {
cobra.CheckErr(rootCmd.Execute())
}

// Main entry point for the backend, triggered by the
// `run` command in the CLI.
func runCommand(_ *cobra.Command, _ []string) {
log.InitLogger(logLevel, os.Stderr)

ctx := context.Background()
logger := logrus.WithContext(ctx)
ctx = log.SetLoggerForContext(ctx, logger)
backend.Run(ctx)
}

// Command to display the version.
func versionCommand(_ *cobra.Command, _ []string) {
fmt.Printf("Version: %s \nCommit: %s\nBuild Time: %s",
version.Version, version.CommitHash, version.BuildTimestamp)
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
github.com/urfave/cli v1.22.14
github.com/vulsio/go-exploitdb v0.4.5
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
google.golang.org/grpc v1.56.1
Expand Down Expand Up @@ -151,7 +150,6 @@ require (
github.com/containerd/typeurl v1.0.2 // indirect
github.com/containerd/typeurl/v2 v2.1.0 // indirect
github.com/containers/image/v5 v5.19.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-minhash v0.0.0-20170608043002-7fe510aff544 // indirect
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,6 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -1936,8 +1935,6 @@ github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand Down

0 comments on commit 6da5cf0

Please sign in to comment.