gitlink-cli/shortcuts/workflow/command_util.go

69 lines
1.5 KiB
Go

package workflow
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// ParseCommandTarget splits a CLI command string into tokens, respecting quoted arguments.
func ParseCommandTarget(target string) []string {
var parts []string
var current strings.Builder
inQuote := false
quoteChar := byte(0)
for i := 0; i < len(target); i++ {
c := target[i]
switch {
case c == '"' || c == '\'':
if inQuote && c == quoteChar {
inQuote = false
quoteChar = 0
} else if !inQuote {
inQuote = true
quoteChar = c
} else {
current.WriteByte(c)
}
case c == ' ' && !inQuote:
if current.Len() > 0 {
parts = append(parts, current.String())
current.Reset()
}
default:
current.WriteByte(c)
}
}
if current.Len() > 0 {
parts = append(parts, current.String())
}
return parts
}
// ResolveCLIBinary finds the gitlink-cli binary for subprocess calls.
func ResolveCLIBinary() string {
if exe, err := os.Executable(); err == nil && exe != "" {
return exe
}
for _, p := range []string{"./gitlink-cli", "./gitlink-cli.exe", "../gitlink-cli", "../gitlink-cli.exe"} {
if _, err := os.Stat(p); err == nil {
if abs, err := filepath.Abs(p); err == nil {
return abs
}
return p
}
}
return "gitlink-cli"
}
// ResolvePath replaces template placeholders in a path string.
func ResolvePath(template, owner, repo string) string {
base := fmt.Sprintf("/%s/%s", owner, repo)
v1 := fmt.Sprintf("/v1/%s/%s", owner, repo)
s := strings.Replace(template, "{v1}", v1, 1)
s = strings.Replace(s, "{base}", base, 1)
return s
}