cmd/gitannex: Simplify messageParser's finalParameter() func

Issue #7625
This commit is contained in:
Dan McArdle 2024-04-11 11:06:33 -04:00 committed by Nick Craig-Wood
parent 61ab519791
commit 36ad4eb145
2 changed files with 25 additions and 32 deletions

View file

@ -96,15 +96,15 @@ func (m *messageParser) nextSpaceDelimitedParameter() (string, error) {
} }
// finalParameter consumes the final parameter, which may contain spaces. // finalParameter consumes the final parameter, which may contain spaces.
func (m *messageParser) finalParameter() (string, error) { func (m *messageParser) finalParameter() string {
m.line = strings.TrimRight(m.line, "\r\n") m.line = strings.TrimRight(m.line, "\r\n")
if len(m.line) == 0 { if len(m.line) == 0 {
return "", errors.New("nothing remains to parse") return ""
} }
param := m.line param := m.line
m.line = "" m.line = ""
return param, nil return param
} }
// configDefinition describes a configuration value required by this command. We // configDefinition describes a configuration value required by this command. We
@ -203,11 +203,7 @@ func (s *server) run() error {
case "REMOVE": case "REMOVE":
err = s.handleRemove(message) err = s.handleRemove(message)
case "ERROR": case "ERROR":
errorMessage, parseErr := message.finalParameter() errorMessage := message.finalParameter()
if parseErr != nil {
err = fmt.Errorf("error while parsing ERROR message from git-annex: %w", parseErr)
break
}
err = fmt.Errorf("received error message from git-annex: %s", errorMessage) err = fmt.Errorf("received error message from git-annex: %s", errorMessage)
// //
@ -310,8 +306,8 @@ func (s *server) queryConfigs() error {
return fmt.Errorf("failed to parse config value: %s %s", valueKeyword, message.line) return fmt.Errorf("failed to parse config value: %s %s", valueKeyword, message.line)
} }
value, err := message.finalParameter() value := message.finalParameter()
if err != nil || value == "" { if value == "" {
return fmt.Errorf("config value of %q must not be empty", config.name) return fmt.Errorf("config value of %q must not be empty", config.name)
} }
@ -351,10 +347,10 @@ func (s *server) handleTransfer(message *messageParser) error {
s.sendMsg("TRANSFER-FAILURE failed to parse key") s.sendMsg("TRANSFER-FAILURE failed to parse key")
return fmt.Errorf("malformed arguments for TRANSFER: %w", err) return fmt.Errorf("malformed arguments for TRANSFER: %w", err)
} }
argFile, err := message.finalParameter() argFile := message.finalParameter()
if err != nil { if argFile == "" {
s.sendMsg("TRANSFER-FAILURE failed to parse file") s.sendMsg("TRANSFER-FAILURE failed to parse file path")
return fmt.Errorf("malformed arguments for TRANSFER: %w", err) return errors.New("failed to parse file path")
} }
if err := s.queryConfigs(); err != nil { if err := s.queryConfigs(); err != nil {
@ -409,9 +405,9 @@ func (s *server) handleTransfer(message *messageParser) error {
} }
func (s *server) handleCheckPresent(message *messageParser) error { func (s *server) handleCheckPresent(message *messageParser) error {
argKey, err := message.finalParameter() argKey := message.finalParameter()
if err != nil { if argKey == "" {
return err return errors.New("failed to parse response for CHECKPRESENT")
} }
if err := s.queryConfigs(); err != nil { if err := s.queryConfigs(); err != nil {
@ -440,7 +436,11 @@ func (s *server) handleCheckPresent(message *messageParser) error {
} }
func (s *server) handleRemove(message *messageParser) error { func (s *server) handleRemove(message *messageParser) error {
argKey, err := message.finalParameter() argKey := message.finalParameter()
if argKey == "" {
return errors.New("failed to parse key for REMOVE")
}
if err != nil { if err != nil {
return err return err
} }

View file

@ -64,12 +64,10 @@ var messageParserTestCases = []messageParserTestCase{
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
param, err = m.finalParameter() param = m.finalParameter()
assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
param, err = m.finalParameter() param = m.finalParameter()
assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
param, err = m.nextSpaceDelimitedParameter() param, err = m.nextSpaceDelimitedParameter()
@ -95,8 +93,7 @@ var messageParserTestCases = []messageParserTestCase{
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
param, err = m.finalParameter() param = m.finalParameter()
assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
}, },
}, },
@ -118,8 +115,7 @@ var messageParserTestCases = []messageParserTestCase{
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
param, err = m.finalParameter() param = m.finalParameter()
assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
}, },
}, },
@ -136,8 +132,7 @@ var messageParserTestCases = []messageParserTestCase{
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, param, "secondparam") assert.Equal(t, param, "secondparam")
param, err = m.finalParameter() param = m.finalParameter()
assert.NoError(t, err)
assert.Equal(t, param, "final param with spaces") assert.Equal(t, param, "final param with spaces")
}, },
}, },
@ -151,12 +146,10 @@ var messageParserTestCases = []messageParserTestCase{
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
m := messageParser{"one long final parameter" + lineEnding} m := messageParser{"one long final parameter" + lineEnding}
param, err := m.finalParameter() param := m.finalParameter()
assert.NoError(t, err)
assert.Equal(t, param, "one long final parameter") assert.Equal(t, param, "one long final parameter")
param, err = m.finalParameter() param = m.finalParameter()
assert.Error(t, err)
assert.Equal(t, param, "") assert.Equal(t, param, "")
}) })