summaryrefslogtreecommitdiff
path: root/cmd_comment.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2017-11-22 22:04:14 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2017-11-22 22:13:31 -0500
commit3efa1fc8a98d2d90c204c7bd5a0e2519e1ac7745 (patch)
treefb78c0b98d24be8d1985af1187f0202c052363d1 /cmd_comment.go
parentf13250e6a926640c4d0ee858f84fcf8036d612aa (diff)
split the parser up, and have read methods like write emethods
Diffstat (limited to 'cmd_comment.go')
-rw-r--r--cmd_comment.go67
1 files changed, 66 insertions, 1 deletions
diff --git a/cmd_comment.go b/cmd_comment.go
index d783ecc..6ce801c 100644
--- a/cmd_comment.go
+++ b/cmd_comment.go
@@ -1,11 +1,15 @@
package libfastimport
import (
+ "fmt"
"strconv"
+ "strings"
"git.lukeshu.com/go/libfastimport/textproto"
)
+// comment /////////////////////////////////////////////////////////////////////
+
type CmdComment struct {
Comment string
}
@@ -14,6 +18,16 @@ func (c CmdComment) fiCmdClass() cmdClass { return cmdClassComment }
func (c CmdComment) fiCmdWrite(fiw *textproto.FIWriter) error {
return fiw.WriteLine("#" + c.Comment)
}
+func init() { parser_registerCmd("#", CmdComment{}) }
+func (CmdComment) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
+ line, err := fir.ReadLine()
+ if err != nil {
+ return nil, err
+ }
+ return CmdComment{Comment: trimLinePrefix(line, "#")}, nil
+}
+
+// get-mark ////////////////////////////////////////////////////////////////////
type CmdGetMark struct {
Mark int
@@ -23,6 +37,21 @@ func (c CmdGetMark) fiCmdClass() cmdClass { return cmdClassComment }
func (c CmdGetMark) fiCmdWrite(fiw *textproto.FIWriter) error {
return fiw.WriteLine("get-mark", ":"+strconv.Itoa(c.Mark))
}
+func init() { parser_registerCmd("get-mark :", CmdGetMark{}) }
+func (CmdGetMark) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
+ line, err := fir.ReadLine()
+ if err != nil {
+ return nil, err
+ }
+ c := CmdGetMark{}
+ c.Mark, err = strconv.Atoi(trimLinePrefix(line, "get-mark :"))
+ if err != nil {
+ return nil, fmt.Errorf("get-mark: %v", err)
+ }
+ return c, nil
+}
+
+// cat-blob ////////////////////////////////////////////////////////////////////
type CmdCatBlob struct {
DataRef string
@@ -32,12 +61,30 @@ func (c CmdCatBlob) fiCmdClass() cmdClass { return cmdClassComment }
func (c CmdCatBlob) fiCmdWrite(fiw *textproto.FIWriter) error {
return fiw.WriteLine("cat-blob", c.DataRef)
}
+func init() { parser_registerCmd("cat-blob ", CmdCatBlob{}) }
+func (CmdCatBlob) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
+ line, err := fir.ReadLine()
+ if err != nil {
+ return nil, err
+ }
+ return CmdCatBlob{DataRef: trimLinePrefix(line, "cat-blob ")}, nil
+}
+
+// ls //////////////////////////////////////////////////////////////////////////
type CmdLs struct {
DataRef string // optional if inside of a commit
Path textproto.Path
}
+// If you're thinking "but wait, parser_registerCmd will see CmdLs as
+// cmdClassCommit, not cmdClassComment, that means it won't be allowed
+// embedded inside other commands! (while still allowing it both
+// inside and outside of a commit)", you're absolutely correct.
+// That's the desired behavior. It's a happy accident that the little
+// fiCmdClass hack works out that way, instead of having to add even
+// more complexity.
+
func (c CmdLs) fiCmdClass() cmdClass {
if c.DataRef == "" {
return cmdClassCommit
@@ -51,4 +98,22 @@ func (c CmdLs) fiCmdWrite(fiw *textproto.FIWriter) error {
return fiw.WriteLine("ls", c.DataRef, c.Path)
}
}
-
+func init() { parser_registerCmd("ls ", CmdLs{}) }
+func (CmdLs) fiCmdRead(fir fiReader) (cmd Cmd, err error) {
+ // 'ls' SP <dataref> SP <path> LF
+ line, err := fir.ReadLine()
+ if err != nil {
+ return nil, err
+ }
+ str := trimLinePrefix(line, "ls ")
+ sp := -1
+ if !strings.HasPrefix(str, "\"") {
+ sp = strings.IndexByte(line, ' ')
+ }
+ c := CmdLs{}
+ c.Path = textproto.PathUnescape(str[sp+1:])
+ if sp >= 0 {
+ c.DataRef = str[:sp]
+ }
+ return c, nil
+}