MCPcopy Create free account
hub / github.com/encodeous/nylon / FetchConfig

Function FetchConfig

core/nylon_distribution.go:19–75  ·  view source on GitHub ↗

fetches and unbundles central config from url

(repoStr string, key state.NyPublicKey, maxSize int64)

Source from the content-addressed store, hash-verified

17
18// fetches and unbundles central config from url
19func FetchConfig(repoStr string, key state.NyPublicKey, maxSize int64) (*state.CentralCfg, error) {
20 repo, err := url.Parse(repoStr)
21 if err != nil {
22 return nil, fmt.Errorf("failed to parse repo URL %s: %w", repoStr, err)
23 }
24 cfgBody := make([]byte, 0)
25
26 if repo.Scheme == "file" {
27 file, err := os.ReadFile(repo.Opaque)
28 if err != nil {
29 return nil, fmt.Errorf("failed to read file %s: %w", repo.Opaque, err)
30 }
31 cfgBody = file
32 } else if repo.Scheme == "http" || repo.Scheme == "https" {
33 client := &http.Client{
34 Transport: &http.Transport{
35 DialContext: func(ctx context.Context, network string, addr string) (conn net.Conn, err error) {
36 host, port, err := net.SplitHostPort(addr)
37 if err != nil {
38 return nil, err
39 }
40 addrs, err := state.ResolveName(ctx, host)
41 if err != nil {
42 return nil, err
43 }
44 for _, ip := range addrs {
45 var dialer net.Dialer
46 conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), port))
47 if err == nil {
48 break
49 }
50 }
51 return
52 },
53 },
54 }
55 res, err := client.Get(repo.String())
56 if err != nil {
57 return nil, fmt.Errorf("failed to fetch %s: %w", repo.String(), err)
58 }
59 cfgBody, err = io.ReadAll(io.LimitReader(res.Body, maxSize))
60 if err != nil {
61 res.Body.Close()
62 return nil, fmt.Errorf("failed to read response from %s: %w", repo.String(), err)
63 }
64 err = res.Body.Close()
65 if err != nil {
66 return nil, fmt.Errorf("failed to close response from %s: %w", repo.String(), err)
67 }
68 }
69
70 config, err := state.UnbundleConfig(string(cfgBody), key)
71 if err != nil {
72 return nil, fmt.Errorf("failed to unbundle config from %s: %w", repoStr, err)
73 }
74 return config, nil
75}
76

Callers 2

checkForConfigUpdatesFunction · 0.85
readCentralConfigFunction · 0.85

Calls 7

ResolveNameFunction · 0.92
UnbundleConfigFunction · 0.92
ParseMethod · 0.80
CloseMethod · 0.65
DialContextMethod · 0.45
StringMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected