github.com/pmoroney/dnscontrol@v0.2.4-0.20171024134423-fad98f73f44a/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 acstraction 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 type ConsolePrinter struct{} 34 35 func (c ConsolePrinter) StartDomain(domain string) { 36 fmt.Printf("******************** Domain: %s\n", domain) 37 } 38 39 func (c ConsolePrinter) PrintCorrection(i int, correction *models.Correction) { 40 fmt.Printf("#%d: %s\n", i+1, correction.Msg) 41 } 42 43 func (c ConsolePrinter) PromptToRun() bool { 44 fmt.Print("Run? (Y/n): ") 45 txt, err := reader.ReadString('\n') 46 run := true 47 if err != nil { 48 run = false 49 } 50 txt = strings.ToLower(strings.TrimSpace(txt)) 51 if txt != "y" { 52 run = false 53 } 54 if !run { 55 fmt.Println("Skipping") 56 } 57 return run 58 } 59 60 func (c ConsolePrinter) EndCorrection(err error) { 61 if err != nil { 62 fmt.Println("FAILURE!", err) 63 } else { 64 fmt.Println("SUCCESS!") 65 } 66 } 67 68 func (c ConsolePrinter) StartDNSProvider(provider string, skip bool) { 69 lbl := "" 70 if skip { 71 lbl = " (skipping)\n" 72 } 73 fmt.Printf("----- DNS Provider: %s...%s", provider, lbl) 74 } 75 76 func (c ConsolePrinter) StartRegistrar(provider string, skip bool) { 77 lbl := "" 78 if skip { 79 lbl = " (skipping)\n" 80 } 81 fmt.Printf("----- Registrar: %s...%s", provider, lbl) 82 } 83 84 func (c ConsolePrinter) EndProvider(numCorrections int, err error) { 85 if err != nil { 86 fmt.Println("ERROR") 87 fmt.Printf("Error getting corrections: %s\n", err) 88 } else { 89 plural := "s" 90 if numCorrections == 1 { 91 plural = "" 92 } 93 fmt.Printf("%d correction%s\n", numCorrections, plural) 94 } 95 } 96 97 func (c ConsolePrinter) Debugf(format string, args ...interface{}) { 98 fmt.Printf(format, args...) 99 } 100 101 func (c ConsolePrinter) Warnf(format string, args ...interface{}) { 102 fmt.Printf("WARNING: "+format, args...) 103 }