cli/cli-version.go
2025-12-11 07:57:48 +01:00

60 lines
1.2 KiB
Go

package cli
import (
"fmt"
"os"
"strings"
)
// const (
// VER_PROGRAM = iota
// VER_VERSION
// VER_DATE
// VER_EMAIL
// )
// GetVersionSection()
func (cli *CliParser) GetVersionSection(sectionName string) (secValue string, err error) {
var sectionId int
if sectionName == "" || sectionName == "all" || sectionName == "full" {
secValue = cli.version[5 : len(cli.version)-2]
} else {
sections := strings.Split(cli.version[5:len(cli.version)-2], ",")
switch sectionName {
case "program":
sectionId = 0
case "version", "number":
sectionId = 1
case "date":
sectionId = 2
case "email":
sectionId = 3
case "full":
sectionId = -1
default:
sectionId = 1
}
if sectionId >= 0 && sectionId < len(sections) {
secValue = sections[sectionId]
} else {
err = fmt.Errorf("unknown version section %q", sectionName)
}
}
return
}
// PrintVersion()
func (cli *CliParser) PrintVersion(specs []string) {
if len(specs) == 0 {
if specValue, err := cli.GetVersionSection("number"); err == nil {
os.Stdout.WriteString(specValue + "\n")
}
} else {
for _, spec := range specs {
if specValue, err := cli.GetVersionSection(spec); err == nil {
os.Stdout.WriteString(specValue + "\n")
}
}
}
}