package commands import ( "io" "net/http" "os" "strings" versionInfo "github.com/StackExchange/dnscontrol/v3/pkg/version" "github.com/urfave/cli/v2" ) var _ = cmd(catUtils, func() *cli.Command { var args TypesArgs return &cli.Command{ Name: "write-types", Usage: "[BETA] Write a TypeScript declaration file in the current directory", Action: func(c *cli.Context) error { return exit(WriteTypes(args)) }, Flags: args.flags(), } }()) // TypesArgs stores arguments related to the types subcommand. type TypesArgs struct { DTSFile string } func (args *TypesArgs) flags() []cli.Flag { var flags []cli.Flag flags = append(flags, &cli.StringFlag{ Name: "dts-file", Aliases: []string{"o"}, Value: "types-dnscontrol.d.ts", Usage: "Path to the .d.ts file to create", Destination: &args.DTSFile, }) return flags } func WriteTypes(args TypesArgs) error { url := "https://raw.githubusercontent.com/StackExchange/dnscontrol/" + strings.Replace(versionInfo.SHA, "[dirty]", "", 1) + "/types/dnscontrol.d.ts" print("Downloading " + url + " to " + args.DTSFile + "...") defer print("\n") resp, err := http.Get(url) if err != nil { return err } defer resp.Body.Close() file, err := os.Create(args.DTSFile) if err != nil { return err } defer file.Close() file.WriteString("// This file was automatically generated by DNSControl. Do not edit it directly.\n") file.WriteString("// To update it, run `dnscontrol write-types`.\n\n") file.WriteString("// DNSControl version: " + versionInfo.Banner() + "\n") file.WriteString("// Source: " + url + "\n\n") _, err = io.Copy(file, resp.Body) if err != nil { return err } print(" done.") return nil }