File: resolver.go

package info (click to toggle)
golang-github-varlink-go 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 272 kB
  • sloc: makefile: 13
file content (94 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package varlink

import "context"

// ResolverAddress is the well-known address of the varlink interface resolver,
// it translates varlink interface names to varlink service addresses.
const ResolverAddress = "unix:/run/org.varlink.resolver"

// Resolver resolves varlink interface names to varlink addresses
type Resolver struct {
	address string
	conn    *Connection
}

// Resolve resolves a varlink interface name to a varlink address.
func (r *Resolver) Resolve(ctx context.Context, iface string) (string, error) {
	type request struct {
		Interface string `json:"interface"`
	}
	type reply struct {
		Address string `json:"address"`
	}

	/* don't ask the resolver for itself */
	if iface == "org.varlink.resolver" {
		return r.address, nil
	}

	var rep reply
	err := r.conn.Call(ctx, "org.varlink.resolver.Resolve", &request{Interface: iface}, &rep)
	if err != nil {
		return "", err
	}

	return rep.Address, nil
}

// GetInfo requests information about the resolver.
func (r *Resolver) GetInfo(ctx context.Context, vendor *string, product *string, version *string, url *string, interfaces *[]string) error {
	type reply struct {
		Vendor     string
		Product    string
		Version    string
		URL        string
		Interfaces []string
	}

	var rep reply
	err := r.conn.Call(ctx, "org.varlink.resolver.GetInfo", nil, &rep)
	if err != nil {
		return err
	}

	if vendor != nil {
		*vendor = rep.Vendor
	}
	if product != nil {
		*product = rep.Product
	}
	if version != nil {
		*version = rep.Version
	}
	if url != nil {
		*url = rep.URL
	}
	if interfaces != nil {
		*interfaces = rep.Interfaces
	}

	return nil
}

// Close terminates the resolver.
func (r *Resolver) Close() error {
	return r.conn.Close()
}

// NewResolver returns a new resolver connected to the given address.
func NewResolver(ctx context.Context, address string) (*Resolver, error) {
	if address == "" {
		address = ResolverAddress
	}

	c, err := NewConnection(ctx, address)
	if err != nil {
		return nil, err
	}
	r := Resolver{
		address: address,
		conn:    c,
	}

	return &r, nil
}