Debian Codegraph @

mpd/response.go

raw symbols SCIP
1 // Copyright 2018 The GoMPD Authors. All rights reserved.
2 // Use of this source code is governed by the MIT
3 // license that can be found in the LICENSE file.
4
5 package mpd
6
7 import "fmt"
8
9 // Quoted is a string that do no need to be quoted.
10 type Quoted string
11
12 // Command returns a command that can be sent to MPD sever.
13 // It enables low-level access to MPD protocol and should be avoided if
14 // the user is not familiar with MPD protocol.
15 //
16 // Strings in args are automatically quoted so that spaces are preserved.
17 // Pass strings as Quoted type if this is not desired.
18 func (c *Client) Command(format string, args ...interface{}) *Command {
19 for i := range args {
20 switch s := args[i].(type) {
21 case Quoted: // ignore
22 case string:
23 args[i] = quote(s)
24 }
25 }
26 return &Command{
27 client: c,
28 cmd: fmt.Sprintf(format, args...),
29 }
30 }
31
32 // A Command represents a MPD command.
33 type Command struct {
34 client *Client
35 cmd string
36 }
37
38 // String returns the encoded command.
39 func (cmd *Command) String() string {
40 return cmd.cmd
41 }
42
43 // OK sends command to server and checks for error.
44 func (cmd *Command) OK() error {
45 id, err := cmd.client.cmd("%v", cmd.cmd)
46 if err != nil {
47 return err
48 }
49 cmd.client.text.StartResponse(id)
50 defer cmd.client.text.EndResponse(id)
51 return cmd.client.readOKLine("OK")
52 }
53
54 // Attrs sends command to server and reads attributes returned in response.
55 func (cmd *Command) Attrs() (Attrs, error) {
56 id, err := cmd.client.cmd(cmd.cmd)
57 if err != nil {
58 return nil, err
59 }
60 cmd.client.text.StartResponse(id)
61 defer cmd.client.text.EndResponse(id)
62 return cmd.client.readAttrs("OK")
63 }
64
65 // AttrsList sends command to server and reads a list of attributes returned in response.
66 // Each attribute group starts with key startKey.
67 func (cmd *Command) AttrsList(startKey string) ([]Attrs, error) {
68 id, err := cmd.client.cmd(cmd.cmd)
69 if err != nil {
70 return nil, err
71 }
72 cmd.client.text.StartResponse(id)
73 defer cmd.client.text.EndResponse(id)
74 return cmd.client.readAttrsList(startKey)
75 }
76
77 // Strings sends command to server and reads a list of strings returned in response.
78 // Each string have the key key.
79 func (cmd *Command) Strings(key string) ([]string, error) {
80 id, err := cmd.client.cmd(cmd.cmd)
81 if err != nil {
82 return nil, err
83 }
84 cmd.client.text.StartResponse(id)
85 defer cmd.client.text.EndResponse(id)
86 return cmd.client.readList(key)
87 }
88
89 // Binary sends command to server and reads its binary response, returning the data and its total size (which can be
90 // greater than the returned chunk).
91 func (cmd *Command) Binary() ([]byte, int, error) {
92 id, err := cmd.client.cmd(cmd.cmd)
93 if err != nil {
94 return nil, 0, err
95 }
96 cmd.client.text.StartResponse(id)
97 defer cmd.client.text.EndResponse(id)
98 return cmd.client.readBinary()
99 }