本文整理汇总了Golang中path/filepath.HasPrefix函数的典型用法代码示例。如果您正苦于以下问题:Golang HasPrefix函数的具体用法?Golang HasPrefix怎么用?Golang HasPrefix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了HasPrefix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Post
func (v *VuesController) Post() {
file := v.Ctx.Input.Param(":files")
d, f := Emplacement(Root, file)
fileNotExt := strings.TrimSuffix(f, filepath.Ext(f))
c := fileNotExt + ".srt"
pathSrt := path.Dir(d) + "/" + fileNotExt + ".srt"
if !filepath.HasPrefix(f, ".") {
finfo, err := os.Stat(d)
if err != nil {
check(err)
} else {
if !finfo.IsDir() {
err := os.Rename(d, path.Dir(d)+"/."+f)
if err != nil {
check(err)
}
if !filepath.HasPrefix(c, ".") {
_, err = os.Stat(pathSrt)
if err == nil {
err := os.Rename(pathSrt, path.Dir(pathSrt)+"/."+c)
if err != nil {
check(err)
}
}
}
v.Redirect("/list/"+path.Dir(file), 302)
}
}
} else {
fmt.Println(" le fichier a déjà été modifié !")
v.Redirect("/list/"+path.Dir(file), 302)
}
}
示例2: isLocalPath
// isLocalPath returns whether the given path is local (/foo ./foo ../foo . ..)
// Windows paths that starts with drive letter (c:\foo c:foo) are considered local.
func isLocalPath(s string) bool {
const sep = string(filepath.Separator)
return s == "." || s == ".." ||
filepath.HasPrefix(s, sep) ||
filepath.HasPrefix(s, "."+sep) || filepath.HasPrefix(s, ".."+sep) ||
filepath.VolumeName(s) != ""
}
示例3: relevantImport
// relevantImport will determine whether this import should be instrumented as well
func (i *Instrumentable) relevantImport(imp string) bool {
if i.basepkg == "*" || build.IsLocalImport(imp) {
return true
} else if i.IsInGopath() || i.basepkg != "" {
return filepath.HasPrefix(imp, i.basepkg) || filepath.HasPrefix(i.basepkg, imp)
}
return false
}
示例4: isSkipURL
func isSkipURL(url string) bool {
if filepath.HasPrefix(url, "http://") {
return true
}
if filepath.HasPrefix(url, "https://") {
return true
}
return false
}
示例5: relevantImport
// relevantImport will determine whether this import should be instrumented as well
func (i *Instrumentable) relevantImport(imp string) bool {
switch {
case imp == "C":
return false
case i.gorootPkgs[imp] && !i.InstrumentGoroot:
return false
case i.basepkg == "*" || build.IsLocalImport(imp):
return true
case i.IsInGopath() || i.basepkg != "":
return filepath.HasPrefix(imp, i.basepkg) || filepath.HasPrefix(i.basepkg, imp)
}
return false
}
示例6: getPackagePath
func getPackagePath(curpath string) (packpath string) {
gopath := os.Getenv("GOPATH")
Debugf("gopath:%s", gopath)
if gopath == "" {
ColorLog("[ERRO] you should set GOPATH in the env")
os.Exit(2)
}
appsrcpath := ""
haspath := false
wgopath := filepath.SplitList(gopath)
for _, wg := range wgopath {
wg, _ = filepath.EvalSymlinks(path.Join(wg, "src"))
if filepath.HasPrefix(strings.ToLower(curpath), strings.ToLower(wg)) {
haspath = true
appsrcpath = wg
break
}
}
if !haspath {
ColorLog("[ERRO] Can't generate application code outside of GOPATH '%s'\n", gopath)
os.Exit(2)
}
packpath = strings.Join(strings.Split(curpath[len(appsrcpath)+1:], string(filepath.Separator)), "/")
return
}
示例7: FindParentPaths
func FindParentPaths(fileName string) []string {
cwd, _ := os.Getwd()
paths := make([]string, 0)
// special case if homedir is not in current path then check there anyway
homedir := homedir()
if !filepath.HasPrefix(cwd, homedir) {
path := filepath.Join(homedir, fileName)
if _, err := os.Stat(path); err == nil {
paths = append(paths, path)
}
}
path := filepath.Join(cwd, fileName)
if _, err := os.Stat(path); err == nil {
paths = append(paths, path)
}
for true {
cwd = filepath.Dir(cwd)
path := filepath.Join(cwd, fileName)
if _, err := os.Stat(path); err == nil {
paths = append(paths, path)
}
if cwd[len(cwd)-1] == filepath.Separator {
break
}
}
return paths
}
示例8: FindTree
// FindTree takes an import or filesystem path and returns the
// tree where the package source should be and the package import path.
func FindTree(path string) (tree *Tree, pkg string, err error) {
if isLocalPath(path) {
if path, err = filepath.Abs(path); err != nil {
return
}
if path, err = filepath.EvalSymlinks(path); err != nil {
return
}
for _, t := range Path {
tpath := t.SrcDir() + string(filepath.Separator)
if !filepath.HasPrefix(path, tpath) {
continue
}
tree = t
pkg = path[len(tpath):]
return
}
err = fmt.Errorf("path %q not inside a GOPATH", path)
return
}
tree = defaultTree
pkg = path
for _, t := range Path {
if t.HasSrc(pkg) {
tree = t
return
}
}
if tree == nil {
err = ErrTreeNotFound
} else {
err = ErrNotFound
}
return
}
示例9: walkExt
// walkExt returns all FileInfos with specific extension.
// Make sure to prefix the extension name with dot.
// For example, to find all go files, pass ".go".
func walkExt(targetDir, ext string) (map[os.FileInfo]string, error) {
rmap := make(map[os.FileInfo]string)
visit := func(path string, f os.FileInfo, err error) error {
if f != nil {
if !f.IsDir() {
if filepath.Ext(path) == ext {
if !filepath.HasPrefix(path, ".") && !strings.Contains(path, "/.") {
if _, ok := rmap[f]; !ok {
wd, err := os.Getwd()
if err != nil {
return err
}
thepath := filepath.Join(wd, strings.Replace(path, wd, "", -1))
rmap[f] = thepath
}
}
}
}
}
return nil
}
err := filepath.Walk(targetDir, visit)
if err != nil {
return nil, err
}
return rmap, nil
}
示例10: readFileFromBackup
// readFileFromBackup copies the next file from the archive into the shard.
// The file is skipped if it does not have a matching shardRelativePath prefix.
func (e *Engine) readFileFromBackup(tr *tar.Reader, shardRelativePath string) error {
// Read next archive file.
hdr, err := tr.Next()
if err != nil {
return err
}
// Skip file if it does not have a matching prefix.
if !filepath.HasPrefix(hdr.Name, shardRelativePath) {
return nil
}
path, err := filepath.Rel(shardRelativePath, hdr.Name)
if err != nil {
return err
}
// Create new file on disk.
f, err := os.OpenFile(filepath.Join(e.path, path), os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
return err
}
defer f.Close()
// Copy from archive to the file.
if _, err := io.CopyN(f, tr, hdr.Size); err != nil {
return err
}
// Sync to disk & close.
if err := f.Sync(); err != nil {
return err
}
return f.Close()
}
示例11: TestCommandDetection
func TestCommandDetection(t *testing.T) {
srcDirs := build.Default.SrcDirs()
t.Logf("SRC DIRS: %v\n", srcDirs)
commands := make(map[string]string)
for _, srcDir := range srcDirs {
// Skip stuff that is in the GOROOT
if filepath.HasPrefix(srcDir, goroot) {
continue
}
filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
pkg, err := build.Default.ImportDir(path, 0)
if err == nil && pkg.IsCommand() {
t.Logf("PKG: %v\n", pkg.ImportPath)
commands[pkg.ImportPath] = filepath.Base(pkg.ImportPath)
}
}
return nil
})
}
t.Logf("MAP: %v\n", commands)
}
示例12: ReadAt
// ReadAt returns a reader for a file at the path relative to the alloc dir
func (d *AllocDir) ReadAt(path string, offset int64) (io.ReadCloser, error) {
if escapes, err := structs.PathEscapesAllocDir(path); err != nil {
return nil, fmt.Errorf("Failed to check if path escapes alloc directory: %v", err)
} else if escapes {
return nil, fmt.Errorf("Path escapes the alloc directory")
}
p := filepath.Join(d.AllocDir, path)
// Check if it is trying to read into a secret directory
for _, dir := range d.TaskDirs {
sdir := filepath.Join(dir, TaskSecrets)
if filepath.HasPrefix(p, sdir) {
return nil, fmt.Errorf("Reading secret file prohibited: %s", path)
}
}
f, err := os.Open(p)
if err != nil {
return nil, err
}
if _, err := f.Seek(offset, 0); err != nil {
return nil, fmt.Errorf("can't seek to offset %q: %v", offset, err)
}
return f, nil
}
示例13: search
// Search images file.
func search(e Engine, dir string) filepath.WalkFunc {
// Implements "filepath.WalkFunc".
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
log.Printf(" + checking: %q\n", path)
}
// Skip directories and hidden files
if info.IsDir() || filepath.HasPrefix(info.Name(), ".") {
return nil
}
if e.IsImage(path) {
p := NewPhoto(path, dir)
p = e.repo.AddPhoto(p)
log.Printf("----------------------------")
log.Printf("Id => %d\n", p.Id)
log.Printf("Name => %s\n", p.Name)
log.Printf("Album => %s\n", p.Album)
log.Printf("Directory => %s\n", p.Directory)
log.Printf("File => %s\n", p.File)
log.Printf("Width => %d\n", p.Width)
log.Printf("Height => %d\n", p.Height)
log.Printf("Aspect => %f\n", p.Aspect())
}
return nil
}
}
示例14: findAbsoluteLinks
func findAbsoluteLinks(ch chan *absoluteLink, errch chan error, root string) {
di, err := os.Open(root)
if err != nil {
errch <- err
return
}
files, err := di.Readdir(-1)
if err != nil {
errch <- err
return
}
di.Close()
for _, file := range files {
pathname := filepath.Join(root, file.Name())
if file.IsDir() {
findAbsoluteLinks(ch, errch, pathname)
continue
}
if (file.Mode() & os.ModeSymlink) != 0 {
target, err := os.Readlink(pathname)
if err != nil {
errch <- err
}
if filepath.IsAbs(target) && !filepath.HasPrefix(target, sysroot) {
ch <- &absoluteLink{pathname, target}
}
}
}
}
示例15: hJSONList
func (s *HTTPStaticServer) hJSONList(w http.ResponseWriter, r *http.Request) {
requestPath := mux.Vars(r)["path"]
localPath := filepath.Join(s.Root, requestPath)
search := r.FormValue("search")
// path string -> info os.FileInfo
fileInfoMap := make(map[string]os.FileInfo, 0)
if search != "" {
results := s.findIndex(search)
if len(results) > 50 { // max 50
results = results[:50]
}
for _, item := range results {
if filepath.HasPrefix(item.Path, requestPath) {
fileInfoMap[item.Path] = item.Info
}
}
} else {
infos, err := ioutil.ReadDir(localPath)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, info := range infos {
fileInfoMap[filepath.Join(requestPath, info.Name())] = info
}
}
lrs := make([]ListResponse, 0)
for path, info := range fileInfoMap {
lr := ListResponse{
Name: info.Name(),
Path: path,
}
if search != "" {
name, err := filepath.Rel(requestPath, path)
if err != nil {
log.Println(requestPath, path, err)
}
lr.Name = filepath.ToSlash(name) // fix for windows
}
if info.IsDir() {
name := deepPath(localPath, info.Name())
lr.Name = name
lr.Path = filepath.Join(filepath.Dir(path), name)
lr.Type = "dir"
lr.Size = "-"
} else {
lr.Type = "file"
lr.Size = formatSize(info)
}
lrs = append(lrs, lr)
}
data, _ := json.Marshal(lrs)
w.Header().Set("Content-Type", "application/json")
w.Write(data)
}