1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Embed types-dnscontrol.d.ts into the binary instead of fetching it via HTTP (#1942)

Co-authored-by: Tom Limoncelli <tlimoncelli@stackoverflow.com>
This commit is contained in:
Jed Fox
2023-01-17 12:10:43 -05:00
committed by GitHub
parent 83b4a301dc
commit 9b3ad81b1d
7 changed files with 9 additions and 19 deletions

View File

@@ -1,10 +1,8 @@
package commands
import (
"io"
"net/http"
_ "embed"
"os"
"strings"
versionInfo "github.com/StackExchange/dnscontrol/v3/pkg/version"
"github.com/urfave/cli/v2"
@@ -39,17 +37,10 @@ func (args *TypesArgs) flags() []cli.Flag {
return flags
}
//go:embed types/dnscontrol.d.ts
var dtsContent string
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
@@ -59,12 +50,11 @@ func WriteTypes(args TypesArgs) error {
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)
file.WriteString(dtsContent)
if err != nil {
return err
}
print(" done.")
print("Successfully wrote " + args.DTSFile + "\n")
return nil
}