updated cmd_docs.go again

had to change a couple of things, like updating the docsURLForVersion
command, and removing the nested function inside since regexMustCompile
is a memory sensitive operation.
- rewrote docsURLForVersion as well
This commit is contained in:
Turmaxx 2026-05-19 17:01:27 +03:00
parent c39f31a416
commit 123ea0f7bd
No known key found for this signature in database
GPG key ID: 35CFDFBB5FF2E522

View file

@ -8,6 +8,7 @@ import (
"os/exec"
"regexp"
"runtime"
"strings"
"github.com/spf13/cobra"
@ -22,8 +23,9 @@ const (
type execFn func(name string, arg ...string) *exec.Cmd
var (
stdout io.Writer = os.Stdout
start execFn = exec.Command
stdout io.Writer = os.Stdout
start execFn = exec.Command
versionRegex *regexp.Regexp = regexp.MustCompile(`^(\d+\.\d+\.\d+)`)
)
func newDocsCommand(globalOptions *global.Options) *cobra.Command {
@ -60,22 +62,23 @@ func newDocsCommand(globalOptions *global.Options) *cobra.Command {
}
func docsURLForVersion(version string) string {
extractVersion := func(version string) string {
// match a semantic version at the start of the string, e.g. "0.18.1" or
// "0.18.1-dev (compiled manually)" -> capture "0.18.1"
versionRegex := regexp.MustCompile(`^(\d+\.\d+\.\d+)`)
matches := versionRegex.FindStringSubmatch(version)
if len(matches) == 2 {
return matches[1]
}
return ""
// 1. Safe default fallback for empty/unknown versions
if version == "" || version == "unknown" {
return fmt.Sprintf("%s/stable", ResticURL)
}
if tag := extractVersion(version); tag != "" {
return fmt.Sprintf("%s/v%s", ResticURL, tag)
// 2. Route development / local compilation environments directly to bleeding edge docs
if strings.Contains(version, "dev") || strings.Contains(version, "compiled") {
return fmt.Sprintf("%s/latest", ResticURL)
}
return ResticURL
// 3. Match strict tag releases (e.g., exact matches like "0.18.1")
matches := versionRegex.FindStringSubmatch(version)
if len(matches) == 2 {
return fmt.Sprintf("%s/v%s", ResticURL, matches[1])
}
return fmt.Sprintf("%s/stable", ResticURL)
}
func openDocs(GOOS string, url string, docType string) {