|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package md |
| 22 | + |
| 23 | +import ( |
| 24 | + "bytes" |
| 25 | + "fmt" |
| 26 | + "os" |
| 27 | + "strings" |
| 28 | + |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 30 | +) |
| 31 | + |
| 32 | +func ReplaceSectionsInFile(path string, sections map[string]string) error { |
| 33 | + data, err := os.ReadFile(path) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + res, err := ReplaceSections(string(data), sections) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + return os.WriteFile(path, []byte(res), 0644) |
| 44 | +} |
| 45 | + |
| 46 | +func ReplaceSections(in string, sections map[string]string) (string, error) { |
| 47 | + for k, v := range sections { |
| 48 | + if n, err := ReplaceSection(in, v, k); err != nil { |
| 49 | + return "", err |
| 50 | + } else { |
| 51 | + in = n |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return in, nil |
| 56 | +} |
| 57 | + |
| 58 | +func ReplaceSection(in, replace, section string) (string, error) { |
| 59 | + start, end := fmt.Sprintf("<!-- START(%s) -->", section), fmt.Sprintf("<!-- END(%s) -->", section) |
| 60 | + |
| 61 | + b := bytes.NewBuffer(nil) |
| 62 | + |
| 63 | + for len(in) > 0 { |
| 64 | + startID := strings.Index(in, start) |
| 65 | + if startID == -1 { |
| 66 | + b.WriteString(in) |
| 67 | + in = "" |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + b.WriteString(in[0:startID]) |
| 72 | + |
| 73 | + in = moveString(in, startID+len(start)) |
| 74 | + |
| 75 | + b.WriteString(start) |
| 76 | + |
| 77 | + b.WriteString(replace) |
| 78 | + |
| 79 | + endID := strings.Index(in, end) |
| 80 | + if endID == -1 { |
| 81 | + return "", errors.Newf("END sections is missing") |
| 82 | + } |
| 83 | + |
| 84 | + b.WriteString(end) |
| 85 | + |
| 86 | + in = moveString(in, endID+len(end)) |
| 87 | + } |
| 88 | + |
| 89 | + return b.String(), nil |
| 90 | +} |
| 91 | + |
| 92 | +func moveString(in string, offset int) string { |
| 93 | + if offset >= len(in) { |
| 94 | + return "" |
| 95 | + } |
| 96 | + |
| 97 | + return in[offset:] |
| 98 | +} |
0 commit comments