Created
November 2, 2018 14:25
-
-
Save cpu/9e232491edb5fd7db18c2e1926ee532c to your computer and use it in GitHub Desktop.
Tiny command line tool to encode IDN domains from stdin to ASCII using IDNA2008. Errors are printed to stderr. ASCII results are printed to stdout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"strings" | |
"golang.org/x/net/idna" | |
) | |
func main() { | |
stdinBytes, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
log.Fatalf("Error reading names from standard in: %v\n", err) | |
} | |
for _, name := range strings.Split(string(stdinBytes), "\n") { | |
if name == "" { | |
continue | |
} | |
out, err := idna.ToASCII(name) | |
if err != nil { | |
fmt.Fprintf(os.Stderr, "error converting %q to ascii: %v\n", name, err) | |
continue | |
} | |
fmt.Printf("%s\n", out) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment