cli: Refactor out log to the top level
Remove some cruft at the same time.
This commit is contained in:
@@ -32,7 +32,6 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
@@ -106,7 +105,7 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error
|
|||||||
|
|
||||||
program, version := data.Program, data.Version
|
program, version := data.Program, data.Version
|
||||||
Logf := func(format string, v ...interface{}) {
|
Logf := func(format string, v ...interface{}) {
|
||||||
log.Printf("deploy: "+format, v...)
|
data.Flags.Logf("deploy: "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: consider adding a timeout based on an args.Timeout flag ?
|
// TODO: consider adding a timeout based on an args.Timeout flag ?
|
||||||
@@ -226,7 +225,7 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error
|
|||||||
Debug: data.Flags.Debug,
|
Debug: data.Flags.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
// TODO: is this a sane prefix to use here?
|
// TODO: is this a sane prefix to use here?
|
||||||
log.Printf("cli: "+format, v...)
|
data.Flags.Logf("cli: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
30
cli/run.go
30
cli/run.go
@@ -32,7 +32,6 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -111,12 +110,9 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
|
|||||||
main.Config = &obj.Config // pass in all the parsed data
|
main.Config = &obj.Config // pass in all the parsed data
|
||||||
|
|
||||||
main.Program, main.Version = data.Program, data.Version
|
main.Program, main.Version = data.Program, data.Version
|
||||||
main.Flags = lib.Flags{
|
main.Debug, main.Logf = data.Flags.Debug, data.Flags.Logf // no prefix
|
||||||
Debug: data.Flags.Debug,
|
|
||||||
Verbose: data.Flags.Verbose,
|
|
||||||
}
|
|
||||||
Logf := func(format string, v ...interface{}) {
|
Logf := func(format string, v ...interface{}) {
|
||||||
log.Printf("main: "+format, v...)
|
data.Flags.Logf("main: "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
cliUtil.Hello(main.Program, main.Version, data.Flags) // say hello!
|
cliUtil.Hello(main.Program, main.Version, data.Flags) // say hello!
|
||||||
@@ -138,9 +134,9 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
Fs: standaloneFs,
|
Fs: standaloneFs,
|
||||||
Debug: main.Flags.Debug,
|
Debug: data.Flags.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("cli: "+format, v...)
|
data.Flags.Logf("cli: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +155,7 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
|
|||||||
if main.Deploy == nil {
|
if main.Deploy == nil {
|
||||||
// nobody activated, but we'll still watch the etcd deploy chan,
|
// nobody activated, but we'll still watch the etcd deploy chan,
|
||||||
// and if there is deployed code that's ready to run, we'll run!
|
// and if there is deployed code that's ready to run, we'll run!
|
||||||
log.Printf("main: no frontend selected (no GAPI activated)")
|
data.Flags.Logf("main: no frontend selected (no GAPI activated)")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := main.Validate(); err != nil {
|
if err := main.Validate(); err != nil {
|
||||||
@@ -188,20 +184,20 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
|
|||||||
select {
|
select {
|
||||||
case sig := <-signals: // any signal will do
|
case sig := <-signals: // any signal will do
|
||||||
if sig != os.Interrupt {
|
if sig != os.Interrupt {
|
||||||
log.Printf("interrupted by signal")
|
data.Flags.Logf("interrupted by signal")
|
||||||
main.Interrupt(fmt.Errorf("killed by %v", sig))
|
main.Interrupt(fmt.Errorf("killed by %v", sig))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch count {
|
switch count {
|
||||||
case 0:
|
case 0:
|
||||||
log.Printf("interrupted by ^C")
|
data.Flags.Logf("interrupted by ^C")
|
||||||
main.Exit(nil)
|
main.Exit(nil)
|
||||||
case 1:
|
case 1:
|
||||||
log.Printf("interrupted by ^C (fast pause)")
|
data.Flags.Logf("interrupted by ^C (fast pause)")
|
||||||
main.FastExit(nil)
|
main.FastExit(nil)
|
||||||
case 2:
|
case 2:
|
||||||
log.Printf("interrupted by ^C (hard interrupt)")
|
data.Flags.Logf("interrupted by ^C (hard interrupt)")
|
||||||
main.Interrupt(nil)
|
main.Interrupt(nil)
|
||||||
}
|
}
|
||||||
count++
|
count++
|
||||||
@@ -215,14 +211,14 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
|
|||||||
reterr := main.Run()
|
reterr := main.Run()
|
||||||
if reterr != nil {
|
if reterr != nil {
|
||||||
// log the error message returned
|
// log the error message returned
|
||||||
if main.Flags.Debug {
|
if data.Flags.Debug {
|
||||||
log.Printf("main: %+v", reterr)
|
data.Flags.Logf("main: %+v", reterr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := main.Close(); err != nil {
|
if err := main.Close(); err != nil {
|
||||||
if main.Flags.Debug {
|
if data.Flags.Debug {
|
||||||
log.Printf("main: Close: %+v", err)
|
data.Flags.Logf("main: Close: %+v", err)
|
||||||
}
|
}
|
||||||
if reterr == nil {
|
if reterr == nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import (
|
|||||||
func Hello(program, version string, flags Flags) {
|
func Hello(program, version string, flags Flags) {
|
||||||
var start = time.Now().UnixNano()
|
var start = time.Now().UnixNano()
|
||||||
|
|
||||||
|
// TODO: Move these log package initialization steps to the top main.go?
|
||||||
logFlags := log.LstdFlags
|
logFlags := log.LstdFlags
|
||||||
if flags.Debug {
|
if flags.Debug {
|
||||||
logFlags = logFlags + log.Lshortfile
|
logFlags = logFlags + log.Lshortfile
|
||||||
@@ -55,5 +56,5 @@ func Hello(program, version string, flags Flags) {
|
|||||||
fmt.Println(fmt.Sprintf("This is: %s, version: %s", program, version))
|
fmt.Println(fmt.Sprintf("This is: %s, version: %s", program, version))
|
||||||
fmt.Println("Copyright (C) 2013-2024+ James Shubin and the project contributors")
|
fmt.Println("Copyright (C) 2013-2024+ James Shubin and the project contributors")
|
||||||
fmt.Println("Written by James Shubin <james@shubin.ca> and the project contributors")
|
fmt.Println("Written by James Shubin <james@shubin.ca> and the project contributors")
|
||||||
log.Printf("main: start: %v", start)
|
flags.Logf("main: start: %v", start)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,10 +54,9 @@ func CliParseError(err error) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Flags are some constant flags which are used throughout the program.
|
// Flags are some constant flags which are used throughout the program.
|
||||||
// TODO: Unify this with Debug and Logf ?
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
Debug bool // add additional log messages
|
Debug bool // add additional log messages
|
||||||
Verbose bool // add extra log message output
|
Logf func(format string, v ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data is a struct of values that we usually pass to the main CLI function.
|
// Data is a struct of values that we usually pass to the main CLI function.
|
||||||
|
|||||||
58
lib/main.go
58
lib/main.go
@@ -35,7 +35,6 @@ package lib
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
@@ -74,12 +73,6 @@ const (
|
|||||||
StoragePrefix = "/storage"
|
StoragePrefix = "/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Flags are some constant flags which are used throughout the program.
|
|
||||||
type Flags struct {
|
|
||||||
Debug bool // add additional log messages
|
|
||||||
Verbose bool // add extra log message output
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config is a struct of all the configuration values for the Main struct. By
|
// Config is a struct of all the configuration values for the Main struct. By
|
||||||
// including this as a separate struct, it can be used as part of the API. This
|
// including this as a separate struct, it can be used as part of the API. This
|
||||||
// API is not considered stable at this time, and is subject to change.
|
// API is not considered stable at this time, and is subject to change.
|
||||||
@@ -90,8 +83,11 @@ type Config struct {
|
|||||||
// Version is the version of this program, usually set at compile time.
|
// Version is the version of this program, usually set at compile time.
|
||||||
Version string `arg:"-"` // cli should ignore
|
Version string `arg:"-"` // cli should ignore
|
||||||
|
|
||||||
// Flags are some static global flags that are set at compile time.
|
// Debug represents if we're running in debug mode or not.
|
||||||
Flags Flags `arg:"-"` // cli should ignore
|
Debug bool `arg:"-"` // cli should ignore
|
||||||
|
|
||||||
|
// Logf is a logger which should be used.
|
||||||
|
Logf func(format string, v ...interface{}) `arg:"-"` // cli should ignore
|
||||||
|
|
||||||
// Hostname to use; nil if undefined. Useful for testing multiple
|
// Hostname to use; nil if undefined. Useful for testing multiple
|
||||||
// instances on same machine or for overriding a bad automatic hostname.
|
// instances on same machine or for overriding a bad automatic hostname.
|
||||||
@@ -300,7 +296,7 @@ func (obj *Main) Init() error {
|
|||||||
// Run is the main execution entrypoint to run mgmt.
|
// Run is the main execution entrypoint to run mgmt.
|
||||||
func (obj *Main) Run() error {
|
func (obj *Main) Run() error {
|
||||||
Logf := func(format string, v ...interface{}) {
|
Logf := func(format string, v ...interface{}) {
|
||||||
log.Printf("main: "+format, v...)
|
obj.Logf("main: "+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
exitCtx := obj.exit.Context() // local exit signal
|
exitCtx := obj.exit.Context() // local exit signal
|
||||||
@@ -509,9 +505,9 @@ func (obj *Main) Run() error {
|
|||||||
NS: NS, // namespace
|
NS: NS, // namespace
|
||||||
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "etcd")),
|
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "etcd")),
|
||||||
|
|
||||||
Debug: obj.Flags.Debug,
|
Debug: obj.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("etcd: "+format, v...)
|
obj.Logf("etcd: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err := obj.embdEtcd.Init(); err != nil {
|
if err := obj.embdEtcd.Init(); err != nil {
|
||||||
@@ -558,9 +554,9 @@ func (obj *Main) Run() error {
|
|||||||
}
|
}
|
||||||
simpleDeploy := &deployer.SimpleDeploy{
|
simpleDeploy := &deployer.SimpleDeploy{
|
||||||
Client: etcdClient,
|
Client: etcdClient,
|
||||||
Debug: obj.Flags.Debug,
|
Debug: obj.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("deploy: "+format, v...)
|
obj.Logf("deploy: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err := simpleDeploy.Init(); err != nil {
|
if err := simpleDeploy.Init(); err != nil {
|
||||||
@@ -577,9 +573,9 @@ func (obj *Main) Run() error {
|
|||||||
// implementation of the Local API (we only expect just this single one)
|
// implementation of the Local API (we only expect just this single one)
|
||||||
localAPI := (&local.API{
|
localAPI := (&local.API{
|
||||||
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "local")),
|
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "local")),
|
||||||
Debug: obj.Flags.Debug,
|
Debug: obj.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("local: api: "+format, v...)
|
obj.Logf("local: api: "+format, v...)
|
||||||
},
|
},
|
||||||
}).Init()
|
}).Init()
|
||||||
|
|
||||||
@@ -593,9 +589,9 @@ func (obj *Main) Run() error {
|
|||||||
MetadataPrefix: MetadataPrefix,
|
MetadataPrefix: MetadataPrefix,
|
||||||
StoragePrefix: StoragePrefix,
|
StoragePrefix: StoragePrefix,
|
||||||
StandaloneFs: obj.DeployFs, // used for static deploys
|
StandaloneFs: obj.DeployFs, // used for static deploys
|
||||||
Debug: obj.Flags.Debug,
|
Debug: obj.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("world: etcd: "+format, v...)
|
obj.Logf("world: etcd: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -608,9 +604,9 @@ func (obj *Main) Run() error {
|
|||||||
World: world,
|
World: world,
|
||||||
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "engine")),
|
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "engine")),
|
||||||
//Prometheus: prom, // TODO: implement this via a general Status API
|
//Prometheus: prom, // TODO: implement this via a general Status API
|
||||||
Debug: obj.Flags.Debug,
|
Debug: obj.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("engine: "+format, v...)
|
obj.Logf("engine: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -701,19 +697,19 @@ func (obj *Main) Run() error {
|
|||||||
//NoWatch: obj.NoWatch,
|
//NoWatch: obj.NoWatch,
|
||||||
NoStreamWatch: obj.NoStreamWatch,
|
NoStreamWatch: obj.NoStreamWatch,
|
||||||
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "gapi")),
|
Prefix: fmt.Sprintf("%s/", path.Join(prefix, "gapi")),
|
||||||
Debug: obj.Flags.Debug,
|
Debug: obj.Debug,
|
||||||
Logf: func(format string, v ...interface{}) {
|
Logf: func(format string, v ...interface{}) {
|
||||||
log.Printf("gapi: "+format, v...)
|
obj.Logf("gapi: "+format, v...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("gapi: init...")
|
Logf("gapi: init...")
|
||||||
}
|
}
|
||||||
if err := gapiImpl.Init(data); err != nil {
|
if err := gapiImpl.Init(data); err != nil {
|
||||||
Logf("gapi: init failed: %+v", err)
|
Logf("gapi: init failed: %+v", err)
|
||||||
// TODO: consider running previous GAPI?
|
// TODO: consider running previous GAPI?
|
||||||
} else {
|
} else {
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("gapi: next...")
|
Logf("gapi: next...")
|
||||||
}
|
}
|
||||||
// this must generate at least one event for it to work
|
// this must generate at least one event for it to work
|
||||||
@@ -723,7 +719,7 @@ func (obj *Main) Run() error {
|
|||||||
|
|
||||||
case next, ok := <-gapiChan:
|
case next, ok := <-gapiChan:
|
||||||
if !ok { // channel closed
|
if !ok { // channel closed
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("gapi exited")
|
Logf("gapi exited")
|
||||||
}
|
}
|
||||||
gapiChan = nil // disable it
|
gapiChan = nil // disable it
|
||||||
@@ -766,7 +762,7 @@ func (obj *Main) Run() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
Logf("new graph took: %s", time.Since(timing))
|
Logf("new graph took: %s", time.Since(timing))
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("new graph: %+v", newGraph)
|
Logf("new graph: %+v", newGraph)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -862,7 +858,7 @@ func (obj *Main) Run() error {
|
|||||||
continue // we'll catch the error later!
|
continue // we'll catch the error later!
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("SendRecv: %s", res) // receiving here
|
Logf("SendRecv: %s", res) // receiving here
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1013,7 +1009,7 @@ func (obj *Main) Run() error {
|
|||||||
select {
|
select {
|
||||||
case deployChan <- deploy:
|
case deployChan <- deploy:
|
||||||
// send
|
// send
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("deploy: sending new gapi")
|
Logf("deploy: sending new gapi")
|
||||||
}
|
}
|
||||||
case <-exitchan:
|
case <-exitchan:
|
||||||
@@ -1071,7 +1067,7 @@ func (obj *Main) Run() error {
|
|||||||
obj.exit.Done(errwrap.Wrapf(err, "deploy: watch error"))
|
obj.exit.Done(errwrap.Wrapf(err, "deploy: watch error"))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("deploy: got activity")
|
Logf("deploy: got activity")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1123,7 +1119,7 @@ func (obj *Main) Run() error {
|
|||||||
select {
|
select {
|
||||||
case deployChan <- deploy:
|
case deployChan <- deploy:
|
||||||
// send
|
// send
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("deploy: sending empty deploy")
|
Logf("deploy: sending empty deploy")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1145,7 +1141,7 @@ func (obj *Main) Run() error {
|
|||||||
case deployChan <- deploy:
|
case deployChan <- deploy:
|
||||||
last = latest // update last deployed
|
last = latest // update last deployed
|
||||||
// send
|
// send
|
||||||
if obj.Flags.Debug {
|
if obj.Debug {
|
||||||
Logf("deploy: sent new gapi")
|
Logf("deploy: sent new gapi")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -33,6 +33,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/cli"
|
"github.com/purpleidea/mgmt/cli"
|
||||||
@@ -48,7 +49,6 @@ import (
|
|||||||
const (
|
const (
|
||||||
tagline = "next generation config management"
|
tagline = "next generation config management"
|
||||||
debug = false // add additional log messages
|
debug = false // add additional log messages
|
||||||
verbose = false // add extra log message output
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// set at compile time
|
// set at compile time
|
||||||
@@ -81,7 +81,9 @@ func main() {
|
|||||||
Tagline: tagline,
|
Tagline: tagline,
|
||||||
Flags: cliUtil.Flags{
|
Flags: cliUtil.Flags{
|
||||||
Debug: debug,
|
Debug: debug,
|
||||||
Verbose: verbose,
|
Logf: func(format string, v ...interface{}) {
|
||||||
|
log.Printf(format, v...) // the top-level log!
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Args: os.Args,
|
Args: os.Args,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user