qmp

package module
v0.0.0-...-cdd0a8e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-qmp

An experimental QMP client for Go.

Example

import (
    "github.com/saracen/go-qmp"
    "github.com/saracen/go-qmp/api/qsd"
)

func createBlockDevice() error {
    client, err := qmp.Dial(ctx, "unix", "qmp.sock")
    if err != nil {
        return err
    }

    // register qemu storage daemon event factory
    qsd.RegisterEvents(client)

    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    cancel()

    // create listener and filter for certain event & job id
    events := client.Listen(ctx, func(e qmp.Event) bool {
        switch v := e.Event.(type) {
        case *qsd.JobStatusChangeEvent:
            return v.Id == "my-job-id"
        }

        return false
    })

    // create blockdev
    create := qsd.BlockdevCreate{
        JobId: "my-job-id",
        Options: qsd.BlockdevCreateOptions{
        Driver: qsd.BlockdevDriverFile,
            File: &qsd.BlockdevCreateOptionsFile{
                Filename:      "/my/raw.disk",
                Size:          10 * 1024 * 1024,
                Preallocation: qmp.ToPtr(qsd.PreallocModeFalloc),
            },
        },
    }

    if err := create.Execute(ctx, client); err != nil {
        return fmt.Errorf("executing create block device: %w", err)
    }

    // watch events
    var errs []error
    for event := range events {
        statusChange, ok := event.Event.(*qsd.JobStatusChangeEvent)
        if !ok {
            continue
        }

        switch statusChange.Status {
        default:
            continue

        case qsd.JobStatusAborting:
            jobInfo, err := qsd.QueryJobs{}.Execute(ctx, client)
            if err != nil {
                errs = append(errs, fmt.Errorf("querying jobs: %w", err))
            } else {
                // find the error for the job id
                for _, info := range jobInfo {
                    if info.Id == "my-job-id" {
                        errs = append(errs, errors.New(qmp.FromPtr(info.Error)))
                        break
                    }
                }
            }

            continue

        case qsd.JobStatusConcluded:
            err := qsd.JobDismiss{Id: "my-job-id"}.Execute(ctx, .client)
            if err != nil {
                errs = append(errs, fmt.Errorf("dismissing job: %w", err))
            }
        }

        break
    }

    if err := errors.Join(errs...); err != nil {
        return fmt.Errorf("creating block device: %w", err)
    }

    return nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConnectionClosed = errors.New("connection closed")
View Source
var ErrListenerClosed = errors.New("listener closed")

Functions

func FromPtr

func FromPtr[T any](v *T) T

func ToPtr

func ToPtr[T any](v T) *T

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func Dial

func Dial(ctx context.Context, network, address string, opts ...Option) (*Client, error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Execute

func (c *Client) Execute(ctx context.Context, execute string, arguments, result any) error

func (*Client) Greeting

func (c *Client) Greeting() Greeting

func (*Client) Listen

func (c *Client) Listen(ctx context.Context, filter func(Event) bool) <-chan Event

func (*Client) RegisterEvent

func (c *Client) RegisterEvent(name string, factory func() api.EventType)

type Dialer

type Dialer interface {
	DialContext(ctx context.Context, network string, addr string) (net.Conn, error)
}

type Error

type Error struct {
	Class string `json:"class"`
	Desc  string `json:"desc"`
}

func (Error) Error

func (e Error) Error() string

type Event

type Event struct {
	Name      string
	Event     api.EventType
	Timestamp time.Time
}

type Greeting

type Greeting struct {
	QMP struct {
		Version      VersionInfo `json:"version"`
		Capabilities []QMPCapability
	} `json:"qmp"`
}

type Message

type Message struct {
	Return json.RawMessage `json:"return"`
	Error  *Error          `json:"error"`
}

type Option

type Option func(*Options) error

func WithDialer

func WithDialer(dialer Dialer) Option

func WithLogger

func WithLogger(logger *slog.Logger) Option

type Options

type Options struct {
	Dialer Dialer
	Logger *slog.Logger
}

type QMPCapability

type QMPCapability string

type VersionInfo

type VersionInfo struct {
	QEMU    VersionTriple `json:"qemu"`
	Package string        `json:"package"`
}

type VersionTriple

type VersionTriple struct {
	Micro int `json:"micro"`
	Minor int `json:"minor"`
	Major int `json:"major"`
}

Directories

Path Synopsis
api
qsd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL