2017-09-13 10:00:41 -04:00
|
|
|
package printer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2018-10-08 13:10:44 -07:00
|
|
|
"io"
|
2017-09-13 10:00:41 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-20 19:21:45 +02:00
|
|
|
"github.com/StackExchange/dnscontrol/v4/models"
|
2017-09-13 10:00:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CLI is an abstraction around the CLI.
|
|
|
|
type CLI interface {
|
|
|
|
Printer
|
|
|
|
StartDomain(domain string)
|
|
|
|
StartDNSProvider(name string, skip bool)
|
2023-03-14 13:45:32 -04:00
|
|
|
EndProvider(name string, numCorrections int, err error)
|
2024-05-07 14:14:20 -04:00
|
|
|
EndProvider2(name string, numCorrections int)
|
2017-09-13 10:00:41 -04:00
|
|
|
StartRegistrar(name string, skip bool)
|
|
|
|
|
|
|
|
PrintCorrection(n int, c *models.Correction)
|
2023-05-16 10:42:08 -04:00
|
|
|
PrintReport(n int, c *models.Correction) // Print corrections that are diff2.REPORT
|
2017-09-13 10:00:41 -04:00
|
|
|
EndCorrection(err error)
|
|
|
|
PromptToRun() bool
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// Printer is a simple abstraction for printing data. Can be passed to providers to give simple output capabilities.
|
2017-09-13 10:00:41 -04:00
|
|
|
type Printer interface {
|
|
|
|
Debugf(fmt string, args ...interface{})
|
2018-10-08 13:10:44 -07:00
|
|
|
Printf(fmt string, args ...interface{})
|
2022-06-18 15:01:02 +02:00
|
|
|
Println(lines ...string)
|
2017-09-13 10:00:41 -04:00
|
|
|
Warnf(fmt string, args ...interface{})
|
2022-06-18 15:01:02 +02:00
|
|
|
Errorf(fmt string, args ...interface{})
|
2024-03-27 13:54:36 -04:00
|
|
|
PrintfIf(print bool, fmt string, args ...interface{})
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
|
2018-10-08 13:10:44 -07:00
|
|
|
// Debugf is called to print/format debug information.
|
|
|
|
func Debugf(fmt string, args ...interface{}) {
|
|
|
|
DefaultPrinter.Debugf(fmt, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf is called to print/format information.
|
|
|
|
func Printf(fmt string, args ...interface{}) {
|
|
|
|
DefaultPrinter.Printf(fmt, args...)
|
|
|
|
}
|
|
|
|
|
2022-06-18 15:01:02 +02:00
|
|
|
// Println is called to print/format information.
|
|
|
|
func Println(lines ...string) {
|
|
|
|
DefaultPrinter.Println(lines...)
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:10:44 -07:00
|
|
|
// Warnf is called to print/format a warning.
|
|
|
|
func Warnf(fmt string, args ...interface{}) {
|
|
|
|
DefaultPrinter.Warnf(fmt, args...)
|
|
|
|
}
|
|
|
|
|
2022-06-18 15:01:02 +02:00
|
|
|
// Errorf is called to print/format an error.
|
2024-03-03 11:48:54 -05:00
|
|
|
// func Errorf(fmt string, args ...interface{}) {
|
|
|
|
// DefaultPrinter.Errorf(fmt, args...)
|
|
|
|
// }
|
2022-06-18 15:01:02 +02:00
|
|
|
|
2024-03-27 13:54:36 -04:00
|
|
|
// PrintfIf is called to optionally print something.
|
|
|
|
func PrintfIf(print bool, fmt string, args ...interface{}) {
|
|
|
|
DefaultPrinter.PrintfIf(print, fmt, args...)
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:10:44 -07:00
|
|
|
var (
|
|
|
|
// DefaultPrinter is the default Printer, used by Debugf, Printf, and Warnf.
|
|
|
|
DefaultPrinter = &ConsolePrinter{
|
|
|
|
Reader: bufio.NewReader(os.Stdin),
|
|
|
|
Writer: os.Stdout,
|
|
|
|
Verbose: false,
|
|
|
|
}
|
|
|
|
)
|
2017-09-13 10:00:41 -04:00
|
|
|
|
2022-08-20 20:59:02 -04:00
|
|
|
// SkinnyReport is true to to disable certain print statements.
|
|
|
|
// This is a hack until we have the new printer replacement. The long
|
|
|
|
// variable name is easy to grep for when we make the conversion.
|
|
|
|
var SkinnyReport = true
|
|
|
|
|
2023-12-13 12:32:39 -05:00
|
|
|
// MaxReport represents how many records to show if SkinnyReport == true
|
|
|
|
var MaxReport = 5
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// ConsolePrinter is a handle for the console printer.
|
2018-10-08 13:10:44 -07:00
|
|
|
type ConsolePrinter struct {
|
|
|
|
Reader *bufio.Reader
|
|
|
|
Writer io.Writer
|
|
|
|
|
|
|
|
Verbose bool
|
|
|
|
}
|
2017-09-13 10:00:41 -04:00
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// StartDomain is called at the start of each domain.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) StartDomain(domain string) {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintf(c.Writer, "******************** Domain: %s\n", domain)
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// PrintCorrection is called to print/format each correction.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) PrintCorrection(i int, correction *models.Correction) {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintf(c.Writer, "#%d: %s\n", i+1, correction.Msg)
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
|
2023-05-16 10:42:08 -04:00
|
|
|
// PrintReport is called to print/format each non-mutating correction (diff2.REPORT).
|
|
|
|
func (c ConsolePrinter) PrintReport(i int, correction *models.Correction) {
|
2023-10-22 13:56:13 -04:00
|
|
|
fmt.Fprintf(c.Writer, "INFO#%d: %s\n", i+1, correction.Msg)
|
2023-05-16 10:42:08 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// PromptToRun prompts the user to see if they want to execute a correction.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) PromptToRun() bool {
|
2023-10-25 05:30:13 +11:00
|
|
|
fmt.Fprint(c.Writer, "Run? (y/N): ")
|
2018-10-08 13:10:44 -07:00
|
|
|
txt, err := c.Reader.ReadString('\n')
|
2017-09-13 10:00:41 -04:00
|
|
|
run := true
|
|
|
|
if err != nil {
|
|
|
|
run = false
|
|
|
|
}
|
|
|
|
txt = strings.ToLower(strings.TrimSpace(txt))
|
|
|
|
if txt != "y" {
|
|
|
|
run = false
|
|
|
|
}
|
|
|
|
if !run {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintln(c.Writer, "Skipping")
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
return run
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// EndCorrection is called at the end of each correction.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) EndCorrection(err error) {
|
|
|
|
if err != nil {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintln(c.Writer, "FAILURE!", err)
|
2017-09-13 10:00:41 -04:00
|
|
|
} else {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintln(c.Writer, "SUCCESS!")
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// StartDNSProvider is called at the start of each new provider.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) StartDNSProvider(provider string, skip bool) {
|
|
|
|
lbl := ""
|
|
|
|
if skip {
|
2023-12-18 12:55:49 -05:00
|
|
|
lbl = " (skipping)"
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
2022-08-20 20:59:02 -04:00
|
|
|
if !SkinnyReport {
|
|
|
|
fmt.Fprintf(c.Writer, "----- DNS Provider: %s...%s\n", provider, lbl)
|
|
|
|
}
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// StartRegistrar is called at the start of each new registrar.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) StartRegistrar(provider string, skip bool) {
|
|
|
|
lbl := ""
|
|
|
|
if skip {
|
2023-12-18 12:55:49 -05:00
|
|
|
lbl = " (skipping)"
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
2022-08-20 20:59:02 -04:00
|
|
|
if !SkinnyReport {
|
|
|
|
fmt.Fprintf(c.Writer, "----- Registrar: %s...%s\n", provider, lbl)
|
|
|
|
}
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// EndProvider is called at the end of each provider.
|
2023-03-14 13:45:32 -04:00
|
|
|
func (c ConsolePrinter) EndProvider(name string, numCorrections int, err error) {
|
2017-09-13 10:00:41 -04:00
|
|
|
if err != nil {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintln(c.Writer, "ERROR")
|
2023-03-14 13:45:32 -04:00
|
|
|
fmt.Fprintf(c.Writer, "Error getting corrections (%s): %s\n", name, err)
|
2017-09-13 10:00:41 -04:00
|
|
|
} else {
|
|
|
|
plural := "s"
|
|
|
|
if numCorrections == 1 {
|
|
|
|
plural = ""
|
|
|
|
}
|
2022-08-20 20:59:02 -04:00
|
|
|
if (SkinnyReport) && (numCorrections == 0) {
|
|
|
|
return
|
|
|
|
}
|
2023-03-14 13:45:32 -04:00
|
|
|
fmt.Fprintf(c.Writer, "%d correction%s (%s)\n", numCorrections, plural, name)
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 14:14:20 -04:00
|
|
|
// EndProvider2 is called at the end of each provider.
|
|
|
|
func (c ConsolePrinter) EndProvider2(name string, numCorrections int) {
|
|
|
|
plural := "s"
|
|
|
|
if numCorrections == 1 {
|
|
|
|
plural = ""
|
|
|
|
}
|
|
|
|
if (SkinnyReport) && (numCorrections == 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(c.Writer, "%d correction%s (%s)\n", numCorrections, plural, name)
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// Debugf is called to print/format debug information.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) Debugf(format string, args ...interface{}) {
|
2018-10-08 13:10:44 -07:00
|
|
|
if c.Verbose {
|
|
|
|
fmt.Fprintf(c.Writer, format, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Printf is called to print/format information.
|
|
|
|
func (c ConsolePrinter) Printf(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(c.Writer, format, args...)
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
|
|
|
|
2022-06-18 15:01:02 +02:00
|
|
|
// Println is called to print/format information.
|
|
|
|
func (c ConsolePrinter) Println(lines ...string) {
|
|
|
|
fmt.Fprintln(c.Writer, lines)
|
|
|
|
}
|
|
|
|
|
2018-01-09 12:53:16 -05:00
|
|
|
// Warnf is called to print/format a warning.
|
2017-09-13 10:00:41 -04:00
|
|
|
func (c ConsolePrinter) Warnf(format string, args ...interface{}) {
|
2018-10-08 13:10:44 -07:00
|
|
|
fmt.Fprintf(c.Writer, "WARNING: "+format, args...)
|
2017-09-13 10:00:41 -04:00
|
|
|
}
|
2022-06-18 15:01:02 +02:00
|
|
|
|
|
|
|
// Errorf is called to print/format an error.
|
|
|
|
func (c ConsolePrinter) Errorf(format string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(c.Writer, "ERROR: "+format, args...)
|
|
|
|
}
|
2024-03-27 13:54:36 -04:00
|
|
|
|
2024-05-07 14:14:20 -04:00
|
|
|
// PrintfIf is called to optionally print/format a message.
|
2024-03-27 13:54:36 -04:00
|
|
|
func (c ConsolePrinter) PrintfIf(print bool, format string, args ...interface{}) {
|
|
|
|
if print {
|
|
|
|
fmt.Fprintf(c.Writer, format, args...)
|
|
|
|
}
|
|
|
|
}
|