fix: manage write error during the display of the DNS help. (#923)

This commit is contained in:
Ludovic Fernandez 2019-07-10 19:34:10 +02:00 committed by GitHub
parent f7dcb6ff22
commit 79f6b35841
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 963 additions and 928 deletions

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
@ -27,22 +28,46 @@ func dnsHelp(ctx *cli.Context) error {
code := ctx.String("code") code := ctx.String("code")
if code == "" { if code == "" {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
ew := &errWriter{w: w}
fmt.Fprintln(w, `Credentials for DNS providers must be passed through environment variables.`) ew.writeln(`Credentials for DNS providers must be passed through environment variables.`)
fmt.Fprintln(w) ew.writeln()
fmt.Fprintln(w, `To display the documentation for a DNS providers:`) ew.writeln(`To display the documentation for a DNS providers:`)
fmt.Fprintln(w) ew.writeln()
fmt.Fprintln(w, "\t$ lego dnshelp -c code") ew.writeln("\t$ lego dnshelp -c code")
fmt.Fprintln(w) ew.writeln()
fmt.Fprintln(w, "All DNS codes:") ew.writeln("All DNS codes:")
fmt.Fprintf(w, "\t%s\n", allDNSCodes()) ew.writef("\t%s\n", allDNSCodes())
fmt.Fprintln(w) ew.writeln()
fmt.Fprintln(w, "More information: https://go-acme.github.io/lego/dns") ew.writeln("More information: https://go-acme.github.io/lego/dns")
if ew.err != nil {
return ew.err
}
return w.Flush() return w.Flush()
} }
displayDNSHelp(strings.ToLower(code)) return displayDNSHelp(strings.ToLower(code))
}
return nil
type errWriter struct {
w io.Writer
err error
}
func (ew *errWriter) writeln(a ...interface{}) {
if ew.err != nil {
return
}
_, ew.err = fmt.Fprintln(ew.w, a...)
}
func (ew *errWriter) writef(format string, a ...interface{}) {
if ew.err != nil {
return
}
_, ew.err = fmt.Fprintf(ew.w, format, a...)
} }

File diff suppressed because it is too large Load diff

View file

@ -9,8 +9,6 @@ import (
"sort" "sort"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"github.com/go-acme/lego/log"
) )
func allDNSCodes() string { func allDNSCodes() string {
@ -24,35 +22,42 @@ func allDNSCodes() string {
return strings.Join(providers, ", ") return strings.Join(providers, ", ")
} }
func displayDNSHelp(name string) { func displayDNSHelp(name string) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
ew := &errWriter{w: w}
switch name { switch name {
{{ range $provider := .Providers }} {{ range $provider := .Providers }}
case "{{ $provider.Code }}": case "{{ $provider.Code }}":
// generated from: {{ .GeneratedFrom }} // generated from: {{ .GeneratedFrom }}
fmt.Fprintln(w, `Configuration for {{ $provider.Name }}.`) ew.writeln(`Configuration for {{ $provider.Name }}.`)
fmt.Fprintln(w, `Code: '{{ $provider.Code }}'`) ew.writeln(`Code: '{{ $provider.Code }}'`)
fmt.Fprintln(w, `Since: '{{ $provider.Since }}'`) ew.writeln(`Since: '{{ $provider.Since }}'`)
fmt.Fprintln(w) ew.writeln()
{{if $provider.Configuration }}{{if $provider.Configuration.Credentials }} {{if $provider.Configuration }}{{if $provider.Configuration.Credentials }}
fmt.Fprintln(w, `Credentials:`) ew.writeln(`Credentials:`)
{{- range $k, $v := $provider.Configuration.Credentials }} {{- range $k, $v := $provider.Configuration.Credentials }}
fmt.Fprintln(w,` - "{{ $k }}": {{ safe $v }}`) ew.writeln(` - "{{ $k }}": {{ safe $v }}`)
{{- end}} {{- end}}
fmt.Fprintln(w) ew.writeln()
{{end}}{{if $provider.Configuration.Additional }} {{end}}{{if $provider.Configuration.Additional }}
fmt.Fprintln(w, `Additional Configuration:`) ew.writeln(`Additional Configuration:`)
{{- range $k, $v := $provider.Configuration.Additional }} {{- range $k, $v := $provider.Configuration.Additional }}
fmt.Fprintln(w, ` - "{{ $k }}": {{ safe $v }}`) ew.writeln(` - "{{ $k }}": {{ safe $v }}`)
{{- end}} {{- end}}
{{end}}{{end}} {{end}}{{end}}
fmt.Fprintln(w) ew.writeln()
fmt.Fprintln(w, `More information: https://go-acme.github.io/lego/dns/{{ $provider.Code }}`) ew.writeln(`More information: https://go-acme.github.io/lego/dns/{{ $provider.Code }}`)
{{end}} {{end}}
case "manual": case "manual":
fmt.Fprintln(w, `Solving the DNS-01 challenge using CLI prompt.`) ew.writeln(`Solving the DNS-01 challenge using CLI prompt.`)
default: default:
log.Fatalf("%q is not yet supported.", name) return fmt.Errorf("%q is not yet supported", name)
} }
w.Flush()
if ew.err != nil {
return fmt.Errorf("error: %v", ew.err)
}
return w.Flush()
} }