github.com/teknogeek/dnscontrol@v0.2.8/pkg/printer/printer.go (about)

     1  package printer
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/StackExchange/dnscontrol/models"
    11  )
    12  
    13  // CLI is an abstraction around the CLI.
    14  type CLI interface {
    15  	Printer
    16  	StartDomain(domain string)
    17  	StartDNSProvider(name string, skip bool)
    18  	EndProvider(numCorrections int, err error)
    19  	StartRegistrar(name string, skip bool)
    20  
    21  	PrintCorrection(n int, c *models.Correction)
    22  	EndCorrection(err error)
    23  	PromptToRun() bool
    24  }
    25  
    26  // Printer is a simple abstraction for printing data. Can be passed to providers to give simple output capabilities.
    27  type Printer interface {
    28  	Debugf(fmt string, args ...interface{})
    29  	Printf(fmt string, args ...interface{})
    30  	Warnf(fmt string, args ...interface{})
    31  }
    32  
    33  // Debugf is called to print/format debug information.
    34  func Debugf(fmt string, args ...interface{}) {
    35  	DefaultPrinter.Debugf(fmt, args...)
    36  }
    37  
    38  // Printf is called to print/format information.
    39  func Printf(fmt string, args ...interface{}) {
    40  	DefaultPrinter.Printf(fmt, args...)
    41  }
    42  
    43  // Warnf is called to print/format a warning.
    44  func Warnf(fmt string, args ...interface{}) {
    45  	DefaultPrinter.Warnf(fmt, args...)
    46  }
    47  
    48  var (
    49  	// DefaultPrinter is the default Printer, used by Debugf, Printf, and Warnf.
    50  	DefaultPrinter = &ConsolePrinter{
    51  		Reader:  bufio.NewReader(os.Stdin),
    52  		Writer:  os.Stdout,
    53  		Verbose: false,
    54  	}
    55  )
    56  
    57  // ConsolePrinter is a handle for the console printer.
    58  type ConsolePrinter struct {
    59  	Reader *bufio.Reader
    60  	Writer io.Writer
    61  
    62  	Verbose bool
    63  }
    64  
    65  // StartDomain is called at the start of each domain.
    66  func (c ConsolePrinter) StartDomain(domain string) {
    67  	fmt.Fprintf(c.Writer, "******************** Domain: %s\n", domain)
    68  }
    69  
    70  // PrintCorrection is called to print/format each correction.
    71  func (c ConsolePrinter) PrintCorrection(i int, correction *models.Correction) {
    72  	fmt.Fprintf(c.Writer, "#%d: %s\n", i+1, correction.Msg)
    73  }
    74  
    75  // PromptToRun prompts the user to see if they want to execute a correction.
    76  func (c ConsolePrinter) PromptToRun() bool {
    77  	fmt.Fprint(c.Writer, "Run? (Y/n): ")
    78  	txt, err := c.Reader.ReadString('\n')
    79  	run := true
    80  	if err != nil {
    81  		run = false
    82  	}
    83  	txt = strings.ToLower(strings.TrimSpace(txt))
    84  	if txt != "y" {
    85  		run = false
    86  	}
    87  	if !run {
    88  		fmt.Fprintln(c.Writer, "Skipping")
    89  	}
    90  	return run
    91  }
    92  
    93  // EndCorrection is called at the end of each correction.
    94  func (c ConsolePrinter) EndCorrection(err error) {
    95  	if err != nil {
    96  		fmt.Fprintln(c.Writer, "FAILURE!", err)
    97  	} else {
    98  		fmt.Fprintln(c.Writer, "SUCCESS!")
    99  	}
   100  }
   101  
   102  // StartDNSProvider is called at the start of each new provider.
   103  func (c ConsolePrinter) StartDNSProvider(provider string, skip bool) {
   104  	lbl := ""
   105  	if skip {
   106  		lbl = " (skipping)\n"
   107  	}
   108  	fmt.Fprintf(c.Writer, "----- DNS Provider: %s...%s", provider, lbl)
   109  }
   110  
   111  // StartRegistrar is called at the start of each new registrar.
   112  func (c ConsolePrinter) StartRegistrar(provider string, skip bool) {
   113  	lbl := ""
   114  	if skip {
   115  		lbl = " (skipping)\n"
   116  	}
   117  	fmt.Fprintf(c.Writer, "----- Registrar: %s...%s", provider, lbl)
   118  }
   119  
   120  // EndProvider is called at the end of each provider.
   121  func (c ConsolePrinter) EndProvider(numCorrections int, err error) {
   122  	if err != nil {
   123  		fmt.Fprintln(c.Writer, "ERROR")
   124  		fmt.Fprintf(c.Writer, "Error getting corrections: %s\n", err)
   125  	} else {
   126  		plural := "s"
   127  		if numCorrections == 1 {
   128  			plural = ""
   129  		}
   130  		fmt.Fprintf(c.Writer, "%d correction%s\n", numCorrections, plural)
   131  	}
   132  }
   133  
   134  // Debugf is called to print/format debug information.
   135  func (c ConsolePrinter) Debugf(format string, args ...interface{}) {
   136  	if c.Verbose {
   137  		fmt.Fprintf(c.Writer, format, args...)
   138  	}
   139  }
   140  
   141  // Printf is called to print/format information.
   142  func (c ConsolePrinter) Printf(format string, args ...interface{}) {
   143  	fmt.Fprintf(c.Writer, format, args...)
   144  }
   145  
   146  // Warnf is called to print/format a warning.
   147  func (c ConsolePrinter) Warnf(format string, args ...interface{}) {
   148  	fmt.Fprintf(c.Writer, "WARNING: "+format, args...)
   149  }