aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/path_prefix.go
diff options
context:
space:
mode:
authortsne <tsne.dev@outlook.com>2026-07-06 16:52:20 +0200
committertsne <tsne.dev@outlook.com>2026-07-06 21:37:42 +0200
commit84da43998f7cc42cc72652333ac6e3d293abb23e (patch)
tree6610f82df854ae86f7f34152bb05fc712ef6f314 /internal/cli/path_prefix.go
downloadhopper-main.tar.gz
initialHEADmain
Diffstat (limited to 'internal/cli/path_prefix.go')
-rw-r--r--internal/cli/path_prefix.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/internal/cli/path_prefix.go b/internal/cli/path_prefix.go
new file mode 100644
index 0000000..355349e
--- /dev/null
+++ b/internal/cli/path_prefix.go
@@ -0,0 +1,51 @@
+package cli
+
+import (
+ "fmt"
+ "path"
+ "strings"
+
+ "github.com/spf13/pflag"
+)
+
+var _ pflag.Value = (*pathPrefixValue)(nil)
+
+type pathPrefixValue struct {
+ p *string
+ name string
+}
+
+func registerPathPrefixFlag(flags *pflag.FlagSet, p *string, name string, val string, usage string) {
+ *p = val
+ flags.Var(&pathPrefixValue{p, name}, name, usage)
+}
+
+func (v *pathPrefixValue) Type() string { return "string" }
+func (v *pathPrefixValue) String() string { return *v.p }
+
+func (v *pathPrefixValue) Set(raw string) error {
+ trimmed := strings.Trim(raw, "/")
+ if trimmed == "" {
+ *v.p = ""
+ return nil
+ }
+
+ cleaned := path.Clean(trimmed)
+ if cleaned == "." {
+ *v.p = ""
+ return nil
+ }
+ if cleaned == ".." || strings.HasPrefix(cleaned, "../") {
+ return usageError{fmt.Sprintf("%s %q escapes", v.name, raw)}
+ }
+
+ *v.p = cleaned
+ return nil
+}
+
+func prefixedPath(prefix string, filePath string) string {
+ if prefix == "" {
+ return filePath
+ }
+ return path.Join(prefix, filePath)
+}