Skip to content

Commit

Permalink
Final removal of legacy dbsteward logging and flow control
Browse files Browse the repository at this point in the history
  • Loading branch information
williammoran committed May 6, 2024
1 parent 3807562 commit abd4b4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
56 changes: 23 additions & 33 deletions lib/dbsteward.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ func (dbsteward *DBSteward) ArgParse() {
// XML file parameter sanity checks
if len(args.XmlFiles) > 0 {
if len(args.OldXmlFiles) > 0 {
dbsteward.Fatal("Parameter error: xml and oldxml options are not to be mixed. Did you mean newxml?")
dbsteward.fatal("Parameter error: xml and oldxml options are not to be mixed. Did you mean newxml?")
}
if len(args.NewXmlFiles) > 0 {
dbsteward.Fatal("Parameter error: xml and newxml options are not to be mixed. Did you mean oldxml?")
dbsteward.fatal("Parameter error: xml and newxml options are not to be mixed. Did you mean oldxml?")
}
}
if len(args.OldXmlFiles) > 0 && len(args.NewXmlFiles) == 0 {
dbsteward.Fatal("Parameter error: oldxml needs newxml specified for differencing to occur")
dbsteward.fatal("Parameter error: oldxml needs newxml specified for differencing to occur")
}
if len(args.NewXmlFiles) > 0 && len(args.OldXmlFiles) == 0 {
dbsteward.Fatal("Parameter error: oldxml needs newxml specified for differencing to occur")
dbsteward.fatal("Parameter error: oldxml needs newxml specified for differencing to occur")
}

// database connectivity values
Expand Down Expand Up @@ -199,20 +199,20 @@ func (dbsteward *DBSteward) ArgParse() {
// validate mode parameters
if mode == ModeXmlDataInsert {
if len(args.XmlFiles) == 0 {
dbsteward.Fatal("xmldatainsert needs xml parameter defined")
dbsteward.fatal("xmldatainsert needs xml parameter defined")
} else if len(args.XmlFiles) > 1 {
dbsteward.Fatal("xmldatainsert only supports one xml file")
dbsteward.fatal("xmldatainsert only supports one xml file")
}
}
if mode == ModeExtract || mode == ModeDbDataDiff {
if len(args.DbHost) == 0 {
dbsteward.Fatal("dbhost not specified")
dbsteward.fatal("dbhost not specified")
}
if len(args.DbName) == 0 {
dbsteward.Fatal("dbname not specified")
dbsteward.fatal("dbname not specified")
}
if len(args.DbUser) == 0 {
dbsteward.Fatal("dbuser not specified")
dbsteward.fatal("dbuser not specified")
}
if args.DbPassword == nil {
p, err := util.PromptPassword("[DBSteward] Enter password for postgres://%s@%s:%d/%s: ", args.DbUser, args.DbHost, args.DbPort, args.DbName)
Expand All @@ -222,35 +222,35 @@ func (dbsteward *DBSteward) ArgParse() {
}
if mode == ModeExtract || mode == ModeSqlDiff {
if len(args.OutputFile) == 0 {
dbsteward.Fatal("output file not specified")
dbsteward.fatal("output file not specified")
}
}
if mode == ModeXmlSlonyId {
if len(args.SlonyIdOut) > 0 {
if args.SlonyIdIn[0] == args.SlonyIdOut {
// TODO(go,nth) resolve filepaths to do this correctly
// TODO(go,nth) check all SlonyIdIn elements
dbsteward.Fatal("slonyidin and slonyidout file paths should not be the same")
dbsteward.fatal("slonyidin and slonyidout file paths should not be the same")
}
}
}

if len(args.OutputDir) > 0 {
if !util.IsDir(args.OutputDir) {
dbsteward.Fatal("outputdir is not a directory, must be a writable directory")
dbsteward.fatal("outputdir is not a directory, must be a writable directory")
}
dbsteward.fileOutputDirectory = args.OutputDir
}
dbsteward.fileOutputPrefix = args.OutputFilePrefix

if args.XmlCollectDataAddendums > 0 {
if mode != ModeDbDataDiff {
dbsteward.Fatal("--xmlcollectdataaddendums is only supported for fresh builds")
dbsteward.fatal("--xmlcollectdataaddendums is only supported for fresh builds")
}
// dammit go
// invalid operation: args.XmlCollectDataAddendums > len(args.XmlFiles) (mismatched types uint and int)
if int(args.XmlCollectDataAddendums) > len(args.XmlFiles) {
dbsteward.Fatal("Cannot collect more data addendums than files provided")
dbsteward.fatal("Cannot collect more data addendums than files provided")
}
}

Expand Down Expand Up @@ -295,7 +295,7 @@ func (dbsteward *DBSteward) ArgParse() {
case ModeSlonyDiff:
dbsteward.doSlonyDiff(args.SlonyDiffOld, args.SlonyDiffNew)
default:
dbsteward.Fatal("No operation specified")
dbsteward.fatal("No operation specified")
}
}

Expand All @@ -310,7 +310,7 @@ func (dbsteward *DBSteward) Logger() *slog.Logger {
return dbsteward.slogLogger
}

func (dbsteward *DBSteward) Fatal(s string, args ...interface{}) {
func (dbsteward *DBSteward) fatal(s string, args ...interface{}) {
dbsteward.logger.Fatal().Msgf(s, args...)
}
func (dbsteward *DBSteward) fatalIfError(err error, s string, args ...interface{}) {
Expand All @@ -319,20 +319,10 @@ func (dbsteward *DBSteward) fatalIfError(err error, s string, args ...interface{
}
}

func (dbsteward *DBSteward) Warning(s string, args ...interface{}) {
func (dbsteward *DBSteward) warning(s string, args ...interface{}) {
dbsteward.logger.Warn().Msgf(s, args...)
}

func (dbsteward *DBSteward) Error(s string, args ...interface{}) {
dbsteward.logger.Error().Msgf(s, args...)
}

func (dbsteward *DBSteward) ErrorIfError(err error, s string, args ...interface{}) {
if err != nil {
dbsteward.logger.Error().Err(err).Msgf(s, args...)
}
}

func (dbsteward *DBSteward) Info(s string, args ...interface{}) {
dbsteward.logger.Info().Msgf(s, args...)
}
Expand Down Expand Up @@ -382,7 +372,7 @@ func (dbsteward *DBSteward) reconcileSqlFormat(target, requested ir.SqlFormat) i
return target
}

dbsteward.Warning("XML is targeted for %s but you are forcing %s. Things will probably break!", target, requested)
dbsteward.warning("XML is targeted for %s but you are forcing %s. Things will probably break!", target, requested)
return requested
}

Expand Down Expand Up @@ -411,7 +401,7 @@ func (dbsteward *DBSteward) defineSqlFormatDefaultValues(SqlFormat ir.SqlFormat,

if SqlFormat != ir.SqlFormatPgsql8 {
if len(args.PgDataXml) > 0 {
dbsteward.Fatal("pgdataxml parameter is not supported by %s driver", SqlFormat)
dbsteward.fatal("pgdataxml parameter is not supported by %s driver", SqlFormat)
}
}
}
Expand Down Expand Up @@ -445,19 +435,19 @@ func (dbsteward *DBSteward) doXmlDataInsert(defFile string, dataFile string) {

dataRows := dataTable.Rows
if dataRows == nil {
dbsteward.Fatal("table %s in %s does not have a <rows> element", dataTable.Name, dataFile)
dbsteward.fatal("table %s in %s does not have a <rows> element", dataTable.Name, dataFile)
}

if len(dataRows.Columns) == 0 {
dbsteward.Fatal("Unexpected: no rows[columns] found in table %s in file %s", dataTable.Name, dataFile)
dbsteward.fatal("Unexpected: no rows[columns] found in table %s in file %s", dataTable.Name, dataFile)
}

if len(dataRows.Rows) > 1 {
dbsteward.Fatal("Unexpected: more than one rows->row found in table %s in file %s", dataTable.Name, dataFile)
dbsteward.fatal("Unexpected: more than one rows->row found in table %s in file %s", dataTable.Name, dataFile)
}

if len(dataRows.Rows[0].Columns) != len(dataRows.Columns) {
dbsteward.Fatal("Unexpected: Table %s in %s defines %d colums but has %d <col> elements",
dbsteward.fatal("Unexpected: Table %s in %s defines %d colums but has %d <col> elements",
dataTable.Name, dataFile, len(dataRows.Columns), len(dataRows.Rows[0].Columns))
}

Expand Down
2 changes: 1 addition & 1 deletion lib/loghandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (h *logHandler) Handle(ctx context.Context, r slog.Record) error {
default:
// Should be Error, but in case other levels get define at
// least nothing gets lost
h.dbsteward.Error(msg)
h.dbsteward.logger.Error().Msgf(msg)
}
h.output.Reset()
return nil
Expand Down

0 comments on commit abd4b4d

Please sign in to comment.