go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/mqlc/dedent.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package mqlc 5 6 import ( 7 "regexp" 8 "strings" 9 ) 10 11 var leadingWhitespace = regexp.MustCompile(`^\s*`) 12 13 func Dedent(content string) string { 14 initial := true 15 indent := "" 16 17 lines := strings.Split(content, "\n") 18 19 // find max indent 20 for i := range lines { 21 line := lines[i] 22 if line == "" { 23 continue 24 } 25 whitespace := leadingWhitespace.FindString(line) 26 if initial || len(indent) > len(whitespace) { 27 indent = whitespace 28 } 29 } 30 31 // cut the whitespace 32 result := []string{} 33 for i := range lines { 34 line := lines[i] 35 if line != "" { 36 line = strings.TrimPrefix(line, indent) 37 } 38 result = append(result, line) 39 } 40 41 return strings.Join(result, "\n") 42 }