go-hep.org/x/hep@v0.38.1/groot/rcmd/rcmd.go (about) 1 // Copyright ©2019 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package rcmd provides helper functions containing the logic of various root-xyz commands. 6 package rcmd // import "go-hep.org/x/hep/groot/rcmd" 7 8 import ( 9 "fmt" 10 "path/filepath" 11 "strings" 12 ) 13 14 func splitArg(cmd string) (fname, sel string, err error) { 15 fname = cmd 16 prefix := "" 17 for _, p := range []string{"https://", "http://", "root://", "file://"} { 18 if strings.HasPrefix(cmd, p) { 19 prefix = p 20 break 21 } 22 } 23 fname = fname[len(prefix):] 24 25 vol := filepath.VolumeName(fname) 26 if vol != fname { 27 fname = fname[len(vol):] 28 } 29 30 if strings.Count(fname, ":") > 1 { 31 return "", "", fmt.Errorf("root-cp: too many ':' in %q", cmd) 32 } 33 34 i := strings.LastIndex(fname, ":") 35 switch { 36 case i > 0: 37 sel = fname[i+1:] 38 fname = fname[:i] 39 default: 40 sel = ".*" 41 } 42 if sel == "" { 43 sel = ".*" 44 } 45 fname = prefix + vol + fname 46 switch { 47 case strings.HasPrefix(sel, "/"): 48 case strings.HasPrefix(sel, "^/"): 49 case strings.HasPrefix(sel, "^"): 50 sel = "^/" + sel[1:] 51 default: 52 sel = "/" + sel 53 } 54 return fname, sel, err 55 }