cli: Refactor out log to the top level

Remove some cruft at the same time.
This commit is contained in:
James Shubin
2024-03-22 00:54:31 -04:00
parent b8a3c39984
commit 3e180eafb4
6 changed files with 51 additions and 58 deletions

View File

@@ -32,7 +32,6 @@ package cli
import (
"context"
"fmt"
"log"
"os"
"os/signal"
@@ -106,7 +105,7 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error
program, version := data.Program, data.Version
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 ?
@@ -226,7 +225,7 @@ func (obj *DeployArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error
Debug: data.Flags.Debug,
Logf: func(format string, v ...interface{}) {
// TODO: is this a sane prefix to use here?
log.Printf("cli: "+format, v...)
data.Flags.Logf("cli: "+format, v...)
},
}

View File

@@ -32,7 +32,6 @@ package cli
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"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.Program, main.Version = data.Program, data.Version
main.Flags = lib.Flags{
Debug: data.Flags.Debug,
Verbose: data.Flags.Verbose,
}
main.Debug, main.Logf = data.Flags.Debug, data.Flags.Logf // no prefix
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!
@@ -138,9 +134,9 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
},
Fs: standaloneFs,
Debug: main.Flags.Debug,
Debug: data.Flags.Debug,
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 {
// 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!
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 {
@@ -188,20 +184,20 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
select {
case sig := <-signals: // any signal will do
if sig != os.Interrupt {
log.Printf("interrupted by signal")
data.Flags.Logf("interrupted by signal")
main.Interrupt(fmt.Errorf("killed by %v", sig))
return
}
switch count {
case 0:
log.Printf("interrupted by ^C")
data.Flags.Logf("interrupted by ^C")
main.Exit(nil)
case 1:
log.Printf("interrupted by ^C (fast pause)")
data.Flags.Logf("interrupted by ^C (fast pause)")
main.FastExit(nil)
case 2:
log.Printf("interrupted by ^C (hard interrupt)")
data.Flags.Logf("interrupted by ^C (hard interrupt)")
main.Interrupt(nil)
}
count++
@@ -215,14 +211,14 @@ func (obj *RunArgs) Run(ctx context.Context, data *cliUtil.Data) (bool, error) {
reterr := main.Run()
if reterr != nil {
// log the error message returned
if main.Flags.Debug {
log.Printf("main: %+v", reterr)
if data.Flags.Debug {
data.Flags.Logf("main: %+v", reterr)
}
}
if err := main.Close(); err != nil {
if main.Flags.Debug {
log.Printf("main: Close: %+v", err)
if data.Flags.Debug {
data.Flags.Logf("main: Close: %+v", err)
}
if reterr == nil {
return false, err

View File

@@ -40,6 +40,7 @@ import (
func Hello(program, version string, flags Flags) {
var start = time.Now().UnixNano()
// TODO: Move these log package initialization steps to the top main.go?
logFlags := log.LstdFlags
if flags.Debug {
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("Copyright (C) 2013-2024+ James Shubin 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)
}

View File

@@ -54,10 +54,9 @@ func CliParseError(err error) error {
}
// Flags are some constant flags which are used throughout the program.
// TODO: Unify this with Debug and Logf ?
type Flags struct {
Debug bool // add additional log messages
Verbose bool // add extra log message output
Debug bool // add additional log messages
Logf func(format string, v ...interface{})
}
// Data is a struct of values that we usually pass to the main CLI function.