mirror of
https://github.com/gregtwallace/apc-p15-tool.git
synced 2025-01-22 08:14:08 +00:00
simplify logging
This commit is contained in:
parent
2c4e3df0a5
commit
201aedce1c
8 changed files with 36 additions and 93 deletions
6
go.mod
6
go.mod
|
@ -5,14 +5,10 @@ go 1.21
|
||||||
require (
|
require (
|
||||||
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4
|
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4
|
||||||
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3
|
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3
|
||||||
go.uber.org/zap v1.26.0
|
|
||||||
golang.org/x/crypto v0.18.0
|
golang.org/x/crypto v0.18.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require golang.org/x/sys v0.16.0 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
|
||||||
golang.org/x/sys v0.16.0 // indirect
|
|
||||||
)
|
|
||||||
|
|
||||||
replace apc-p15-tool/cmd => /cmd
|
replace apc-p15-tool/cmd => /cmd
|
||||||
|
|
||||||
|
|
14
go.sum
14
go.sum
|
@ -1,21 +1,9 @@
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
|
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
|
||||||
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||||
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4 h1:aiqS8aBlF9PsAKeMddMSfbwp3smONCn3UO8QfUg0Z7Y=
|
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4 h1:aiqS8aBlF9PsAKeMddMSfbwp3smONCn3UO8QfUg0Z7Y=
|
||||||
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4/go.mod h1:H/13DK46DKXy7EaIxPhk2Y0EC8aubKm35nBjBe8AAGc=
|
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4/go.mod h1:H/13DK46DKXy7EaIxPhk2Y0EC8aubKm35nBjBe8AAGc=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 h1:aQKxg3+2p+IFXXg97McgDGT5zcMrQoi0EICZs8Pgchs=
|
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 h1:aQKxg3+2p+IFXXg97McgDGT5zcMrQoi0EICZs8Pgchs=
|
||||||
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
|
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA=
|
||||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
|
||||||
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
|
|
||||||
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
|
|
||||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
|
||||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
|
||||||
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
|
|
||||||
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
|
|
||||||
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||||
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
||||||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||||
|
@ -24,5 +12,3 @@ golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
|
||||||
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
|
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
|
|
@ -3,11 +3,12 @@ package app
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/peterbourgon/ff/v4"
|
"github.com/peterbourgon/ff/v4"
|
||||||
"github.com/peterbourgon/ff/v4/ffhelp"
|
"github.com/peterbourgon/ff/v4/ffhelp"
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -16,21 +17,24 @@ const (
|
||||||
|
|
||||||
// struct for receivers to use common app pieces
|
// struct for receivers to use common app pieces
|
||||||
type app struct {
|
type app struct {
|
||||||
logger *zap.SugaredLogger
|
stdLogger *log.Logger
|
||||||
|
debugLogger *log.Logger
|
||||||
|
errLogger *log.Logger
|
||||||
cmd *ff.Command
|
cmd *ff.Command
|
||||||
config *config
|
config *config
|
||||||
}
|
}
|
||||||
|
|
||||||
// actual application start
|
// actual application start
|
||||||
func Start(args []string) {
|
func Start(args []string) {
|
||||||
// make app w/ initial logger pre-config
|
// make app w/ logger
|
||||||
initLogLevel := "debug"
|
|
||||||
app := &app{
|
app := &app{
|
||||||
logger: makeZapLogger(&initLogLevel),
|
stdLogger: log.New(os.Stdout, "", 0),
|
||||||
|
debugLogger: log.New(io.Discard, "", 0), // discard debug logging by default
|
||||||
|
errLogger: log.New(os.Stderr, "", 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
// log start
|
// log start
|
||||||
app.logger.Infof("apc-p15-tool v%s", appVersion)
|
app.stdLogger.Printf("apc-p15-tool v%s", appVersion)
|
||||||
|
|
||||||
// get os.Args if args unspecified in func
|
// get os.Args if args unspecified in func
|
||||||
if args == nil {
|
if args == nil {
|
||||||
|
@ -40,8 +44,8 @@ func Start(args []string) {
|
||||||
// get & parse config
|
// get & parse config
|
||||||
err := app.getConfig(args)
|
err := app.getConfig(args)
|
||||||
|
|
||||||
// re-init logger with configured log level
|
// if debug logging, make real debug logger
|
||||||
app.logger = makeZapLogger(app.config.logLevel)
|
app.debugLogger = log.New(os.Stdout, "debug: ", 0)
|
||||||
|
|
||||||
// deal with config err (after logger re-init)
|
// deal with config err (after logger re-init)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -49,7 +53,7 @@ func Start(args []string) {
|
||||||
|
|
||||||
if errors.Is(err, ff.ErrHelp) {
|
if errors.Is(err, ff.ErrHelp) {
|
||||||
// help explicitly requested
|
// help explicitly requested
|
||||||
app.logger.Info("\n\n", ffhelp.Command(app.cmd))
|
app.stdLogger.Printf("\n%s\n", ffhelp.Command(app.cmd))
|
||||||
|
|
||||||
} else if errors.Is(err, ff.ErrDuplicateFlag) ||
|
} else if errors.Is(err, ff.ErrDuplicateFlag) ||
|
||||||
errors.Is(err, ff.ErrUnknownFlag) ||
|
errors.Is(err, ff.ErrUnknownFlag) ||
|
||||||
|
@ -57,13 +61,13 @@ func Start(args []string) {
|
||||||
errors.Is(err, ErrExtraArgs) {
|
errors.Is(err, ErrExtraArgs) {
|
||||||
// other error that suggests user needs to see help
|
// other error that suggests user needs to see help
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
app.logger.Error(err)
|
app.errLogger.Print(err)
|
||||||
app.logger.Info("\n\n", ffhelp.Command(app.cmd))
|
app.stdLogger.Printf("\n%s\n", ffhelp.Command(app.cmd))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// any other error
|
// any other error
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
app.logger.Error(err)
|
app.errLogger.Print(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(exitCode)
|
os.Exit(exitCode)
|
||||||
|
@ -74,9 +78,14 @@ func Start(args []string) {
|
||||||
err = app.cmd.Run(context.Background())
|
err = app.cmd.Run(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
app.logger.Error(err)
|
app.errLogger.Print(err)
|
||||||
|
|
||||||
|
// if extra args, show help
|
||||||
|
if errors.Is(err, ErrExtraArgs) {
|
||||||
|
app.stdLogger.Printf("\n%s\n", ffhelp.Command(app.cmd))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
app.logger.Info("apc-p15-tool done")
|
app.stdLogger.Print("apc-p15-tool done")
|
||||||
os.Exit(exitCode)
|
os.Exit(exitCode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ func (app *app) cmdCreate(_ context.Context, args []string) error {
|
||||||
return fmt.Errorf("create: failed to write apc p15 file (%s)", err)
|
return fmt.Errorf("create: failed to write apc p15 file (%s)", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.logger.Infof("create: apc p15 file %s written to disk", fileName)
|
app.stdLogger.Printf("create: apc p15 file %s written to disk", fileName)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,8 +67,8 @@ func (app *app) cmdInstall(cmdCtx context.Context, args []string) error {
|
||||||
// log fingerprint for debugging
|
// log fingerprint for debugging
|
||||||
actualHashB64 := base64.RawStdEncoding.EncodeToString(actualHash)
|
actualHashB64 := base64.RawStdEncoding.EncodeToString(actualHash)
|
||||||
actualHashHex := hex.EncodeToString(actualHash)
|
actualHashHex := hex.EncodeToString(actualHash)
|
||||||
app.logger.Debugf("ssh: remote server key fingerprint (b64): %s", actualHashB64)
|
app.debugLogger.Printf("ssh: remote server key fingerprint (b64): %s", actualHashB64)
|
||||||
app.logger.Debugf("ssh: remote server key fingerprint (hex): %s", actualHashHex)
|
app.debugLogger.Printf("ssh: remote server key fingerprint (hex): %s", actualHashHex)
|
||||||
|
|
||||||
// allow base64 format
|
// allow base64 format
|
||||||
if actualHashB64 == *app.config.install.fingerprint {
|
if actualHashB64 == *app.config.install.fingerprint {
|
||||||
|
@ -120,7 +120,7 @@ func (app *app) cmdInstall(cmdCtx context.Context, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// done
|
// done
|
||||||
app.logger.Infof("install: apc p15 file installed on %s", *app.config.install.hostAndPort)
|
app.stdLogger.Printf("install: apc p15 file installed on %s", *app.config.install.hostAndPort)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ type keyCertPemCfg struct {
|
||||||
|
|
||||||
// app's config options from user
|
// app's config options from user
|
||||||
type config struct {
|
type config struct {
|
||||||
logLevel *string
|
debugLogging *bool
|
||||||
create struct {
|
create struct {
|
||||||
keyCertPemCfg
|
keyCertPemCfg
|
||||||
outFilePath *string
|
outFilePath *string
|
||||||
|
@ -54,8 +54,7 @@ func (app *app) getConfig(args []string) error {
|
||||||
// apc-p15-tool -- root command
|
// apc-p15-tool -- root command
|
||||||
rootFlags := ff.NewFlagSet("apc-p15-tool")
|
rootFlags := ff.NewFlagSet("apc-p15-tool")
|
||||||
|
|
||||||
cfg.logLevel = rootFlags.StringEnum('l', "loglevel", "log level: debug, info, warn, error, dpanic, panic, or fatal",
|
cfg.debugLogging = rootFlags.BoolLong("debug", "set this flag to enable additional debug logging messages")
|
||||||
"info", "debug", "warn", "error", "dpanic", "panic", "fatal")
|
|
||||||
|
|
||||||
rootCmd := &ff.Command{
|
rootCmd := &ff.Command{
|
||||||
Name: "apc-p15-tool",
|
Name: "apc-p15-tool",
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
)
|
|
||||||
|
|
||||||
// makeZapLogger creates a logger for the app; if log level is nil or does not parse
|
|
||||||
// the default 'Info' level will be used.
|
|
||||||
func makeZapLogger(logLevel *string) *zap.SugaredLogger {
|
|
||||||
// default info level
|
|
||||||
zapLevel := zapcore.InfoLevel
|
|
||||||
var parseErr error
|
|
||||||
|
|
||||||
// try to parse specified level (if there is one)
|
|
||||||
if logLevel != nil {
|
|
||||||
parseLevel, err := zapcore.ParseLevel(*logLevel)
|
|
||||||
if err != nil {
|
|
||||||
parseErr = err
|
|
||||||
} else {
|
|
||||||
zapLevel = parseLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// make zap config
|
|
||||||
config := zap.NewProductionEncoderConfig()
|
|
||||||
config.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
||||||
config.LineEnding = "\n"
|
|
||||||
|
|
||||||
// no stack trace
|
|
||||||
config.StacktraceKey = ""
|
|
||||||
|
|
||||||
// make logger
|
|
||||||
consoleEncoder := zapcore.NewConsoleEncoder(config)
|
|
||||||
core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapLevel)
|
|
||||||
|
|
||||||
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel)).Sugar()
|
|
||||||
|
|
||||||
// log deferred parse error if there was one
|
|
||||||
if logLevel != nil && parseErr != nil {
|
|
||||||
logger.Errorf("failed to parse requested log level \"%s\" (%s)", *logLevel, parseErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return logger
|
|
||||||
}
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
// pemToAPCP15 reads the specified pem files and returns the apc p15 bytes
|
// pemToAPCP15 reads the specified pem files and returns the apc p15 bytes
|
||||||
func (app *app) pemToAPCP15(keyPem, certPem []byte, parentCmdName string) ([]byte, error) {
|
func (app *app) pemToAPCP15(keyPem, certPem []byte, parentCmdName string) ([]byte, error) {
|
||||||
app.logger.Infof("%s: making apc p15 file from pem", parentCmdName)
|
app.stdLogger.Printf("%s: making apc p15 file from pem", parentCmdName)
|
||||||
|
|
||||||
// make p15 struct
|
// make p15 struct
|
||||||
p15, err := pkcs15.ParsePEMToPKCS15(keyPem, certPem)
|
p15, err := pkcs15.ParsePEMToPKCS15(keyPem, certPem)
|
||||||
|
@ -15,7 +15,7 @@ func (app *app) pemToAPCP15(keyPem, certPem []byte, parentCmdName string) ([]byt
|
||||||
return nil, fmt.Errorf("%s: failed to parse pem files (%w)", parentCmdName, err)
|
return nil, fmt.Errorf("%s: failed to parse pem files (%w)", parentCmdName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.logger.Infof("%s: successfully loaded pem files", parentCmdName)
|
app.stdLogger.Printf("%s: successfully loaded pem files", parentCmdName)
|
||||||
|
|
||||||
// make file bytes
|
// make file bytes
|
||||||
p15File, err := p15.ToP15File()
|
p15File, err := p15.ToP15File()
|
||||||
|
@ -32,7 +32,7 @@ func (app *app) pemToAPCP15(keyPem, certPem []byte, parentCmdName string) ([]byt
|
||||||
// combine header with file
|
// combine header with file
|
||||||
apcFile := append(apcHeader, p15File...)
|
apcFile := append(apcHeader, p15File...)
|
||||||
|
|
||||||
app.logger.Infof("%s: apc p15 file data succesfully generated", parentCmdName)
|
app.stdLogger.Printf("%s: apc p15 file data succesfully generated", parentCmdName)
|
||||||
|
|
||||||
return apcFile, nil
|
return apcFile, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue