Skip to content

Commit

Permalink
As PersistenRuns are not chained
Browse files Browse the repository at this point in the history
spf13/cobra#216
Added setting of loglevel in PersistentRun
for all cctl sub commands
Migrate command must also set the log level. Just that it should
not call InitState()
  • Loading branch information
puneetguptanitj committed Oct 19, 2018
1 parent da963ae commit 79f4e61
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 12 deletions.
5 changes: 5 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ var backupCmd = &cobra.Command{
Short: "Create an archive with the current cctl state and an etcd snapshot from the cluster.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
archivePath, err := cmd.Flags().GetString("archive")
Expand Down
7 changes: 7 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"fmt"

log "github.com/platform9/cctl/pkg/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -12,6 +14,11 @@ var createCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("create called")
Expand Down
6 changes: 6 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

log "github.com/platform9/cctl/pkg/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -13,6 +14,11 @@ var deleteCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("delete called")
Expand Down
6 changes: 6 additions & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

log "github.com/platform9/cctl/pkg/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -13,6 +14,11 @@ var deployCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Deploy called")
Expand Down
5 changes: 5 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ var getCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
log.Printf("Unknown resource %q. Use --help to print available options", args[0])
Expand Down
7 changes: 7 additions & 0 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ import (
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrate the state file to the current version",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
version, err := stateutil.VersionFromFile(stateFilename)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/recover.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
log "github.com/platform9/cctl/pkg/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -11,6 +12,11 @@ var recoverCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ var restoreCmd = &cobra.Command{
Short: "Restore the cctl state and etcd snapshot from an archive.",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
archivePath, err := cmd.Flags().GetString("archive")
Expand Down
8 changes: 0 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

log "github.com/platform9/cctl/pkg/logrus"
cctlstate "github.com/platform9/cctl/pkg/state/v2"
"github.com/sirupsen/logrus"

spclientfake "github.com/platform9/ssh-provider/pkg/client/clientset_generated/clientset/fake"
"github.com/spf13/cobra"
Expand All @@ -20,13 +19,6 @@ var LogLevel string

var rootCmd = &cobra.Command{
Use: "cctl",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logLevel, err := logrus.ParseLevel(LogLevel)
if err != nil {
log.Fatalf("Could not parse log level %v", logLevel)
}
log.SetLogLevel(logLevel)
},
PreRun: func(cmd *cobra.Command, args []string) {
InitState()
},
Expand Down
6 changes: 6 additions & 0 deletions cmd/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
log "github.com/platform9/cctl/pkg/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -10,6 +11,11 @@ var snapshotCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
},
Expand Down
6 changes: 6 additions & 0 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"

log "github.com/platform9/cctl/pkg/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -13,6 +14,11 @@ var upgradeCmd = &cobra.Command{
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, args []string) {
InitState()
// PersistentPreRuns are not chained https://github.com/spf13/cobra/issues/216
// Therefore LogLevel must be set in all the PersistentPreRuns
if err := log.SetLogLevelUsingString(LogLevel); err != nil {
log.Fatalf("Unable to parse log level %s", LogLevel)
}
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Upgrade called")
Expand Down
19 changes: 15 additions & 4 deletions pkg/logrus/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logrus
import (
"os"

"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)

Expand All @@ -11,17 +12,27 @@ var (
stdOut = log.New()
// used for error, fatal, panic
stdError = log.New()
logLevel log.Level
level log.Level
)

// LogLevel returns the current log level
func LogLevel() log.Level {
return logLevel
return level
}

// SetLogLevelUsingString sets level for both loggers
func SetLogLevelUsingString(inputLevel string) error {
level, err := logrus.ParseLevel(inputLevel)
if err != nil {
return err
}
SetLogLevel(level)
return nil
}

// SetLogLevel sets level for both loggers
func SetLogLevel(level log.Level) {
logLevel = level
func SetLogLevel(inputLevel log.Level) {
level = inputLevel
stdOut.Out = os.Stdout
stdError.Out = os.Stderr
// used only for levels >= log.InfoLevel
Expand Down

0 comments on commit 79f4e61

Please sign in to comment.