github.com/philhug/dnscontrol@v0.2.4-0.20180625181521-921fa9849001/pkg/printer/printer.go (about) 1 package printer 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/StackExchange/dnscontrol/models" 10 ) 11 12 // CLI is an abstraction around the CLI. 13 type CLI interface { 14 Printer 15 StartDomain(domain string) 16 StartDNSProvider(name string, skip bool) 17 EndProvider(numCorrections int, err error) 18 StartRegistrar(name string, skip bool) 19 20 PrintCorrection(n int, c *models.Correction) 21 EndCorrection(err error) 22 PromptToRun() bool 23 } 24 25 // Printer is a simple abstraction for printing data. Can be passed to providers to give simple output capabilities. 26 type Printer interface { 27 Debugf(fmt string, args ...interface{}) 28 Warnf(fmt string, args ...interface{}) 29 } 30 31 var reader = bufio.NewReader(os.Stdin) 32 33 // ConsolePrinter is a handle for the console printer. 34 type ConsolePrinter struct{} 35 36 // StartDomain is called at the start of each domain. 37 func (c ConsolePrinter) StartDomain(domain string) { 38 fmt.Printf("******************** Domain: %s\n", domain) 39 } 40 41 // PrintCorrection is called to print/format each correction. 42 func (c ConsolePrinter) PrintCorrection(i int, correction *models.Correction) { 43 fmt.Printf("#%d: %s\n", i+1, correction.Msg) 44 } 45 46 // PromptToRun prompts the user to see if they want to execute a correction. 47 func (c ConsolePrinter) PromptToRun() bool { 48 fmt.Print("Run? (Y/n): ") 49 txt, err := reader.ReadString('\n') 50 run := true 51 if err != nil { 52 run = false 53 } 54 txt = strings.ToLower(strings.TrimSpace(txt)) 55 if txt != "y" { 56 run = false 57 } 58 if !run { 59 fmt.Println("Skipping") 60 } 61 return run 62 } 63 64 // EndCorrection is called at the end of each correction. 65 func (c ConsolePrinter) EndCorrection(err error) { 66 if err != nil { 67 fmt.Println("FAILURE!", err) 68 } else { 69 fmt.Println("SUCCESS!") 70 } 71 } 72 73 // StartDNSProvider is called at the start of each new provider. 74 func (c ConsolePrinter) StartDNSProvider(provider string, skip bool) { 75 lbl := "" 76 if skip { 77 lbl = " (skipping)\n" 78 } 79 fmt.Printf("----- DNS Provider: %s...%s", provider, lbl) 80 } 81 82 // StartRegistrar is called at the start of each new registrar. 83 func (c ConsolePrinter) StartRegistrar(provider string, skip bool) { 84 lbl := "" 85 if skip { 86 lbl = " (skipping)\n" 87 } 88 fmt.Printf("----- Registrar: %s...%s", provider, lbl) 89 } 90 91 // EndProvider is called at the end of each provider. 92 func (c ConsolePrinter) EndProvider(numCorrections int, err error) { 93 if err != nil { 94 fmt.Println("ERROR") 95 fmt.Printf("Error getting corrections: %s\n", err) 96 } else { 97 plural := "s" 98 if numCorrections == 1 { 99 plural = "" 100 } 101 fmt.Printf("%d correction%s\n", numCorrections, plural) 102 } 103 } 104 105 // Debugf is called to print/format debug information. 106 func (c ConsolePrinter) Debugf(format string, args ...interface{}) { 107 fmt.Printf(format, args...) 108 } 109 110 // Warnf is called to print/format a warning. 111 func (c ConsolePrinter) Warnf(format string, args ...interface{}) { 112 fmt.Printf("WARNING: "+format, args...) 113 }