|
| 1 | +/* |
| 2 | +Copyright 2019 HAProxy Technologies |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//nolint:dupl |
| 18 | +package actions |
| 19 | + |
| 20 | +import ( |
| 21 | + stderrors "errors" |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/haproxytech/client-native/v6/config-parser/common" |
| 26 | + "github.com/haproxytech/client-native/v6/config-parser/types" |
| 27 | +) |
| 28 | + |
| 29 | +type DoLog struct { |
| 30 | + Cond string |
| 31 | + CondTest string |
| 32 | + Comment string |
| 33 | +} |
| 34 | + |
| 35 | +func (f *DoLog) Parse(parts []string, parserType types.ParserType, comment string) error { |
| 36 | + if comment != "" { |
| 37 | + f.Comment = comment |
| 38 | + } |
| 39 | + var command []string |
| 40 | + var minLen, requiredLen int |
| 41 | + switch parserType { |
| 42 | + case types.HTTP: |
| 43 | + command = parts[1:] |
| 44 | + minLen = 2 |
| 45 | + requiredLen = 4 |
| 46 | + case types.TCP: |
| 47 | + command = parts[2:] |
| 48 | + minLen = 3 |
| 49 | + requiredLen = 5 |
| 50 | + } |
| 51 | + if len(parts) == minLen { |
| 52 | + return nil |
| 53 | + } |
| 54 | + if len(parts) < requiredLen { |
| 55 | + return stderrors.New("not enough params") |
| 56 | + } |
| 57 | + _, condition := common.SplitRequest(command) |
| 58 | + if len(condition) > 1 { |
| 59 | + f.Cond = condition[0] |
| 60 | + f.CondTest = strings.Join(condition[1:], " ") |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (f *DoLog) String() string { |
| 66 | + if f.Cond != "" { |
| 67 | + return fmt.Sprintf("do-log %s %s", f.Cond, f.CondTest) |
| 68 | + } |
| 69 | + return "do-log" |
| 70 | +} |
| 71 | + |
| 72 | +func (f *DoLog) GetComment() string { |
| 73 | + return f.Comment |
| 74 | +} |
0 commit comments