forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
62 lines (52 loc) · 1.49 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/tools/doc-generator/util.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package parse
import (
"math"
"strings"
)
func FindFlagsPrefix(flags []string) []string {
if len(flags) == 0 {
return flags
}
// Split the input flags input tokens separated by "."
// because the want to find the prefix where segments
// are dot-separated.
var tokens [][]string
for _, flag := range flags {
tokens = append(tokens, strings.Split(flag, "."))
}
// Find the shortest tokens.
minLength := math.MaxInt32
for _, t := range tokens {
if len(t) < minLength {
minLength = len(t)
}
}
// We iterate backward to find common suffixes. Each time
// a common suffix is found, we remove it from the tokens.
outer:
for i := 0; i < minLength; i++ {
lastToken := tokens[0][len(tokens[0])-1]
// Interrupt if the last token is different across the flags.
for _, t := range tokens {
if t[len(t)-1] != lastToken {
break outer
}
}
// The suffix token is equal across all flags, so we
// remove it from all of them and re-iterate.
for i, t := range tokens {
tokens[i] = t[:len(t)-1]
}
}
// The remaining tokens are the different flags prefix, which we can
// now merge with the ".".
var prefixes []string
for _, t := range tokens {
prefixes = append(prefixes, strings.Join(t, "."))
}
return prefixes
}