diff options
| author | Shulhan <ms@kilabit.info> | 2023-10-06 01:33:12 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-10-06 01:33:12 +0700 |
| commit | b22f2104dd822a890b8f8b8de5489215493c0812 (patch) | |
| tree | 08b4c281c95ff7f8693cddc3dce2ce25c46c57cf /statement.go | |
| parent | 006fe79d7523c47102462595d8e8664fc1910c6a (diff) | |
| download | awwan-b22f2104dd822a890b8f8b8de5489215493c0812.tar.xz | |
all: simplify parsing statement for magic command get
This changes minimize code duplication when parsing magic command
between "#get:" and "#get!" using single function parseStatementGetPut.
In the returned Statement, we changes where to store the source argument
into args[0] instead of in cmd for better readability and minimize
confusion in Copy/SudoCopy.
While at it, we add unit test for "#get:".
Unit test for "#get!" is not possible right now, because it will prompt
for password.
Diffstat (limited to 'statement.go')
| -rw-r--r-- | statement.go | 63 |
1 files changed, 43 insertions, 20 deletions
diff --git a/statement.go b/statement.go index 84071dd..4b8b1a2 100644 --- a/statement.go +++ b/statement.go @@ -5,6 +5,7 @@ package awwan import ( "bytes" + "errors" "fmt" "strings" @@ -12,7 +13,7 @@ import ( ) const ( - statementKindDefault = iota + statementKindDefault int = iota statementKindComment statementKindRequire statementKindGet @@ -21,6 +22,15 @@ const ( statementKindSudoPut ) +// List of magic command. +var ( + cmdMagicGet = []byte(`#get:`) + cmdMagicPut = []byte(`#put:`) + cmdMagicSudoGet = []byte(`#get!`) + cmdMagicSudoPut = []byte(`#put!`) + cmdMagicRequire = []byte(`#require:`) +) + // Statement contains parsed raw line from the script. type Statement struct { cmd string @@ -33,7 +43,7 @@ type Statement struct { // It will return nil if raw line is empty. func ParseStatement(raw []byte) (stmt *Statement, err error) { var ( - logp = "ParseStatement" + logp = `ParseStatement` cmd string args []string @@ -46,15 +56,9 @@ func ParseStatement(raw []byte) (stmt *Statement, err error) { if bytes.HasPrefix(raw, cmdMagicGet) { raw = raw[len(cmdMagicGet):] - cmd, args = libexec.ParseCommandArgs(string(raw)) - if len(cmd) == 0 || len(args) == 0 { - return nil, fmt.Errorf("%s: %s missing argument", logp, cmdMagicGet) - } - stmt = &Statement{ - kind: statementKindGet, - cmd: cmd, - args: args, - raw: raw, + stmt, err = parseStatementGetPut(statementKindGet, raw) + if err != nil { + return nil, fmt.Errorf(`%s: %q: %w`, logp, cmdMagicGet, err) } return stmt, nil } @@ -74,15 +78,9 @@ func ParseStatement(raw []byte) (stmt *Statement, err error) { } if bytes.HasPrefix(raw, cmdMagicSudoGet) { raw = raw[len(cmdMagicSudoGet):] - cmd, args = libexec.ParseCommandArgs(string(raw)) - if len(cmd) == 0 || len(args) == 0 { - return nil, fmt.Errorf("%s: %s missing argument", logp, cmdMagicSudoGet) - } - stmt = &Statement{ - kind: statementKindSudoGet, - cmd: cmd, - args: args, - raw: raw, + stmt, err = parseStatementGetPut(statementKindSudoGet, raw) + if err != nil { + return nil, fmt.Errorf(`%s: %q: %w`, logp, cmdMagicSudoGet, err) } return stmt, nil } @@ -131,3 +129,28 @@ func ParseStatement(raw []byte) (stmt *Statement, err error) { func (stmt *Statement) String() string { return fmt.Sprintf("%s %s", stmt.cmd, strings.Join(stmt.args, " ")) } + +// parseStatementGetPut parse the raw "#get" or "#put" statement. +func parseStatementGetPut(kind int, raw []byte) (stmt *Statement, err error) { + var ( + cmd string + args []string + ) + + cmd, args = libexec.ParseCommandArgs(string(raw)) + if len(cmd) == 0 { + return nil, errors.New(`missing arguments`) + } + if len(args) == 0 { + return nil, errors.New(`missing destination file`) + } + if len(args) > 1 { + return nil, errors.New(`too many arguments`) + } + stmt = &Statement{ + kind: kind, + args: []string{cmd, args[0]}, + raw: raw, + } + return stmt, nil +} |
