File: local.go

package info (click to toggle)
golang-github-digitalocean-go-libvirt 0.0~git20250317.13bf9b4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,188 kB
  • sloc: yacc: 188; sh: 76; xml: 50; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,222 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
package dialers

import (
	"net"
	"time"
)

const (
	// defaultSocket specifies the default path to the libvirt unix socket.
	defaultSocket = "/var/run/libvirt/libvirt-sock"

	// defaultLocalTimeout specifies the default libvirt dial timeout.
	defaultLocalTimeout = 15 * time.Second
)

// Local implements connecting to a local libvirtd over the unix socket.
type Local struct {
	timeout time.Duration
	socket  string
}

// LocalOption is a function for setting local socket options.
type LocalOption func(*Local)

// WithLocalTimeout sets the dial timeout.
func WithLocalTimeout(timeout time.Duration) LocalOption {
	return func(l *Local) {
		l.timeout = timeout
	}
}

// WithSocket sets the path to the local libvirt socket.
func WithSocket(socket string) LocalOption {
	return func(l *Local) {
		l.socket = socket
	}
}

// NewLocal is a default dialer to simply connect to a locally running libvirt's
// socket.
func NewLocal(opts ...LocalOption) *Local {
	l := &Local{
		timeout: defaultLocalTimeout,
		socket:  defaultSocket,
	}

	for _, opt := range opts {
		opt(l)
	}

	return l
}

// Dial connects to a local socket
func (l *Local) Dial() (net.Conn, error) {
	return net.DialTimeout("unix", l.socket, l.timeout)
}