systemd.parser: Add GetTemplateParts()

This helper splits out a templated filename into the base template and
the instance name. This will be used later.

Signed-off-by: Alexander Larsson <alexl@redhat.com>
This commit is contained in:
Alexander Larsson
2023-12-20 15:27:11 +01:00
parent 2df994ba0c
commit 7e1942ed46

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/user" "os/user"
"path" "path"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"unicode" "unicode"
@ -919,3 +920,13 @@ func (f *UnitFile) PrependComment(groupName string, comments ...string) {
group.prependComment(newUnitLine("", "# "+comments[i], true)) group.prependComment(newUnitLine("", "# "+comments[i], true))
} }
} }
func (f *UnitFile) GetTemplateParts() (string, string) {
ext := filepath.Ext(f.Filename)
basename := strings.TrimSuffix(f.Filename, ext)
parts := strings.SplitN(basename, "@", 2)
if len(parts) < 2 {
return "", ""
}
return parts[0] + "@" + ext, parts[1]
}