mirror of
https://github.com/lxc/incus.git
synced 2026-02-05 09:46:19 +01:00
cmd/incus-user: unify logging, support --verbose and --debug
Refactor logging setup so that it matches the rest of the code base. Add support for --verbose and --debug, similarly to other commands. Signed-off-by: Maciek Borzecki <maciek.borzecki@gmail.com>
This commit is contained in:
committed by
Stéphane Graber
parent
079bbcfedf
commit
01b70f2651
@@ -1,19 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/lxc/incus/v6/internal/version"
|
||||
"github.com/lxc/incus/v6/shared/logger"
|
||||
)
|
||||
|
||||
type cmdGlobal struct {
|
||||
flagHelp bool
|
||||
flagVersion bool
|
||||
flagHelp bool
|
||||
flagVersion bool
|
||||
flagLogVerbose bool
|
||||
flagLogDebug bool
|
||||
}
|
||||
|
||||
func main() {
|
||||
// PreRun runs immediately prior to the main Run function.
|
||||
func (c *cmdGlobal) PreRun(cmd *cobra.Command, args []string) error {
|
||||
return logger.InitLogger("", "", c.flagLogVerbose, c.flagLogDebug, nil)
|
||||
}
|
||||
|
||||
func run() error {
|
||||
// daemon command (main)
|
||||
daemonCmd := cmdDaemon{}
|
||||
app := daemonCmd.Command()
|
||||
@@ -32,14 +41,22 @@ func main() {
|
||||
globalCmd := cmdGlobal{}
|
||||
app.PersistentFlags().BoolVar(&globalCmd.flagVersion, "version", false, "Print version number")
|
||||
app.PersistentFlags().BoolVarP(&globalCmd.flagHelp, "help", "h", false, "Print help")
|
||||
app.PersistentFlags().BoolVarP(&globalCmd.flagLogVerbose, "verbose", "v", false, "Show all information messages")
|
||||
app.PersistentFlags().BoolVarP(&globalCmd.flagLogDebug, "debug", "d", false, "Show debug messages")
|
||||
app.PersistentPreRunE = globalCmd.PreRun
|
||||
|
||||
// Version handling
|
||||
app.SetVersionTemplate("{{.Version}}\n")
|
||||
app.Version = version.Version
|
||||
|
||||
// Run the main command and handle errors
|
||||
err := app.Execute()
|
||||
return app.Execute()
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := run()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,12 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
incus "github.com/lxc/incus/v6/client"
|
||||
"github.com/lxc/incus/v6/internal/linux"
|
||||
internalUtil "github.com/lxc/incus/v6/internal/util"
|
||||
"github.com/lxc/incus/v6/shared/logger"
|
||||
)
|
||||
|
||||
var mu sync.RWMutex
|
||||
@@ -43,13 +43,6 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
|
||||
return fmt.Errorf("This must be run as root")
|
||||
}
|
||||
|
||||
// Setup logger.
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
})
|
||||
log.SetLevel(log.InfoLevel)
|
||||
log.SetOutput(os.Stdout)
|
||||
|
||||
// Create storage.
|
||||
err := os.MkdirAll(internalUtil.VarPath("users"), 0700)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
@@ -57,7 +50,7 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// Connect.
|
||||
log.Debug("Connecting to the daemon")
|
||||
logger.Debug("Connecting to the daemon")
|
||||
client, err := incus.ConnectIncusUnix("", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to connect to the daemon: %w", err)
|
||||
@@ -78,7 +71,7 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if !ok {
|
||||
log.Info("Performing initial configuration")
|
||||
logger.Info("Performing initial configuration")
|
||||
err = serverInitialConfiguration(client)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to apply initial configuration: %w", err)
|
||||
@@ -185,13 +178,13 @@ func (c *cmdDaemon) Run(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
// Start accepting requests.
|
||||
log.Info("Starting up the server")
|
||||
logger.Info("Starting up the server")
|
||||
|
||||
for {
|
||||
// Accept new connection.
|
||||
conn, err := listener.AcceptUnix()
|
||||
if err != nil {
|
||||
log.Error("Failed to accept new connection: %w", err)
|
||||
logger.Errorf("Failed to accept new connection: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user