aboutsummaryrefslogtreecommitdiffstats
path: root/packet/command/passthrough/passthrough.go
blob: 58c937c5fbe2fbca952062ab6744cd768d2d827e (plain) (blame)
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
package passthrough

import (
	"github.com/osm/quake/common/buffer"
	"github.com/osm/quake/common/context"
)

type Command struct {
	Data []byte
}

func (cmd *Command) Bytes() []byte {
	buf := buffer.New()

	buf.PutBytes(cmd.Data)

	return buf.Bytes()
}

func Parse(ctx *context.Context, buf *buffer.Buffer, str string) (*Command, error) {
	var cmd Command

	if len(str) > 0 {
		cmd.Data = []byte(str)
	}

	remaining := buf.Len() - buf.Off()
	if remaining > 0 {
		data, err := buf.GetBytes(remaining)
		if err != nil {
			return nil, err
		}

		cmd.Data = append(cmd.Data, data...)
	}

	return &cmd, nil
}