当前位置: 首页>>代码示例>>Golang>>正文


Golang filepath.Rel函数代码示例

本文整理汇总了Golang中path/filepath.Rel函数的典型用法代码示例。如果您正苦于以下问题:Golang Rel函数的具体用法?Golang Rel怎么用?Golang Rel使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Rel函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: pathResolve

// Resolves the local path relative to the root directory
// Returns the path relative to the remote, the abspath on disk and an error if any
func (g *Commands) pathResolve() (relPath, absPath string, err error) {
	root := g.context.AbsPathOf("")
	absPath = g.context.AbsPathOf(g.opts.Path)
	relPath = ""

	if absPath != root {
		relPath, err = filepath.Rel(root, absPath)
		if err != nil {
			return
		}
	} else {
		var cwd string
		if cwd, err = os.Getwd(); err != nil {
			return
		}
		if cwd == root {
			relPath = ""
		} else if relPath, err = filepath.Rel(root, cwd); err != nil {
			return
		}
	}
	relPath = strings.Join([]string{"", relPath}, "/")

	return
}
开发者ID:enzochiau,项目名称:drive,代码行数:27,代码来源:changes.go

示例2: subdir

// subdir determines the package based on the current working directory,
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
func subdir() (pkgpath string, underGoRoot bool, err error) {
	cwd, err := os.Getwd()
	if err != nil {
		return "", false, err
	}
	if root := runtime.GOROOT(); strings.HasPrefix(cwd, root) {
		subdir, err := filepath.Rel(root, cwd)
		if err != nil {
			return "", false, err
		}
		return subdir, true, nil
	}

	for _, p := range filepath.SplitList(build.Default.GOPATH) {
		if !strings.HasPrefix(cwd, p) {
			continue
		}
		subdir, err := filepath.Rel(p, cwd)
		if err == nil {
			return subdir, false, nil
		}
	}
	return "", false, fmt.Errorf(
		"working directory %q is not in either GOROOT(%q) or GOPATH(%q)",
		cwd,
		runtime.GOROOT(),
		build.Default.GOPATH,
	)
}
开发者ID:tidatida,项目名称:go,代码行数:31,代码来源:go_darwin_arm_exec.go

示例3: makeFileRelative

func makeFileRelative(filePath string, processingFile string) (string, error) {
	cwd := path.Dir(processingFile)
	if filePath, err := filepath.Rel(cwd, filePath); err == nil {
		return filePath, nil
	}
	return filepath.Rel(cwd, path.Join(cwd, filePath))
}
开发者ID:CodeJuan,项目名称:kubernetes,代码行数:7,代码来源:util.go

示例4: execNpm

func (c *Client) execNpm(args ...string) (string, string, error) {
	if err := os.MkdirAll(filepath.Join(c.RootPath, "node_modules"), 0755); err != nil {
		return "", "", err
	}
	nodePath, err := filepath.Rel(c.RootPath, c.nodePath())
	if err != nil {
		return "", "", err
	}
	npmPath, err := filepath.Rel(c.RootPath, c.npmPath())
	if err != nil {
		return "", "", err
	}
	args = append([]string{npmPath}, args...)
	if debugging() {
		args = append(args, "--loglevel=silly")
	}
	cmd := exec.Command(nodePath, args...)
	cmd.Dir = c.RootPath
	cmd.Env = c.environ()
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err = cmd.Run()
	if debugging() {
		fmt.Fprintln(os.Stderr, stderr.String())
	}
	return stdout.String(), stderr.String(), err
}
开发者ID:istrategylabs,项目名称:heroku-cli,代码行数:28,代码来源:packages.go

示例5: WriteTar

func WriteTar(srcPath string, dest io.Writer) error {
	absPath, err := filepath.Abs(srcPath)
	if err != nil {
		return err
	}

	tw := tar.NewWriter(dest)
	defer tw.Close()

	err = filepath.Walk(absPath, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		var relative string
		if os.IsPathSeparator(srcPath[len(srcPath)-1]) {
			relative, err = filepath.Rel(absPath, path)
		} else {
			relative, err = filepath.Rel(filepath.Dir(absPath), path)
		}

		relative = filepath.ToSlash(relative)

		if err != nil {
			return err
		}

		return addTarFile(path, relative, tw)
	})

	return err
}
开发者ID:pcfdev-forks,项目名称:houdini,代码行数:32,代码来源:write_tar.go

示例6: DownloadFiles

func (sd *SmartDownloader) DownloadFiles() {
	pwd, _ := os.Getwd()
	pwd = filepath.Join(pwd, sd.domain)
	os.Mkdir(pwd, 0755)

	if len(sd.filelist) == 0 {
		sd.DownloadDirectory(pwd, sd.docroot)
	} else {
		for _, dir := range sd.dirlist {
			rel_dir, _ := filepath.Rel(sd.docroot, dir)
			local_dir := filepath.Join(pwd, rel_dir)
			sd.connlogger.Printf("Getting directory [%s] to [%s]\n", dir, local_dir)
			sd.conn.GetDirectory(local_dir, dir, false)
		}

		var fileGroup sync.WaitGroup
		fileGroup.Add(len(sd.filelist))
		semaphore := make(chan bool, 500)

		for _, file := range sd.filelist {
			go func(file string) {
				semaphore <- true
				defer func() {
					<-semaphore
					fileGroup.Done()
				}()

				rel_file, _ := filepath.Rel(sd.docroot, file)
				sd.conn.GetFile(filepath.Join(pwd, rel_file), file)
			}(file)
		}

		fileGroup.Wait()
	}
}
开发者ID:r3nic1e,项目名称:bbdownloader,代码行数:35,代码来源:smart.go

示例7: makeRepoRelative

// filePath is the file we are looking for
// inFile is the file where we found the link. So if we are processing
//    /path/to/repoRoot/docs/admin/README.md and are looking for
//    ../../file.json we can find that location.
// In many cases filePath and processingFile may be the same
func makeRepoRelative(filePath string, processingFile string) (string, error) {
	if filePath, err := filepath.Rel(repoRoot, filePath); err == nil {
		return filePath, nil
	}
	cwd := path.Dir(processingFile)
	return filepath.Rel(repoRoot, path.Join(cwd, filePath))
}
开发者ID:CodeJuan,项目名称:kubernetes,代码行数:12,代码来源:util.go

示例8: Diff

// Creates the FSChanges between sourceDir and destDir.
// To detect if a file was changed it checks the file's size and mtime (like
// rsync does by default if no --checksum options is used)
func (s *SimpleFSDiffer) Diff() (FSChanges, error) {
	changes := FSChanges{}
	sourceFileInfos := make(map[string]fileInfo)
	destFileInfos := make(map[string]fileInfo)
	err := filepath.Walk(s.sourceDir, fsWalker(sourceFileInfos))
	if err != nil {
		return nil, err
	}
	err = filepath.Walk(s.destDir, fsWalker(destFileInfos))
	if err != nil {
		return nil, err
	}

	for _, destInfo := range destFileInfos {
		relpath, _ := filepath.Rel(s.destDir, destInfo.Path)
		sourceInfo, ok := sourceFileInfos[filepath.Join(s.sourceDir, relpath)]
		if !ok {
			changes = append(changes, &FSChange{Path: relpath, ChangeType: Added})
		} else {
			if sourceInfo.Size() != destInfo.Size() || sourceInfo.ModTime().Before(destInfo.ModTime()) {
				changes = append(changes, &FSChange{Path: relpath, ChangeType: Modified})
			}
		}
	}
	for _, infoA := range sourceFileInfos {
		relpath, _ := filepath.Rel(s.sourceDir, infoA.Path)
		_, ok := destFileInfos[filepath.Join(s.destDir, relpath)]
		if !ok {
			changes = append(changes, &FSChange{Path: relpath, ChangeType: Deleted})
		}
	}
	return changes, nil
}
开发者ID:dgonyeo,项目名称:fsdiffer,代码行数:36,代码来源:simple.go

示例9: Diff

// Diff will return any changes to the filesystem in the provided directory
// since Start was called.
//
// To detect if a file was changed it checks the file's size and mtime (like
// rsync does by default if no --checksum options is used)
func (ba *BeforeAfterFSDiffer) Diff() (FSChanges, error) {
	changes := FSChanges{}
	after := make(map[string]fileInfo)
	err := filepath.Walk(ba.dir, fsWalker(after))
	if err != nil {
		return nil, err
	}

	for _, afterInfo := range after {
		relpath, _ := filepath.Rel(ba.dir, afterInfo.Path)
		sourceInfo, ok := ba.before[filepath.Join(ba.dir, relpath)]
		if !ok {
			changes = append(changes, &FSChange{Path: relpath, ChangeType: Added})
		} else {
			if sourceInfo.Size() != afterInfo.Size() || sourceInfo.ModTime().Before(afterInfo.ModTime()) {
				changes = append(changes, &FSChange{Path: relpath, ChangeType: Modified})
			}
		}
	}
	for _, infoA := range ba.before {
		relpath, _ := filepath.Rel(ba.dir, infoA.Path)
		_, ok := after[filepath.Join(ba.dir, relpath)]
		if !ok {
			changes = append(changes, &FSChange{Path: relpath, ChangeType: Deleted})
		}
	}
	return changes, nil
}
开发者ID:dgonyeo,项目名称:fsdiffer,代码行数:33,代码来源:beforeafter.go

示例10: fileInfo

// Get file info and return the number of parts in the file. If the filename is
// a directory or glob, return the list of files the directory/glob contains.
func (iom *IOMeshage) fileInfo(filename string) ([]string, int64, error) {
	glob, err := filepath.Glob(filename)
	if err != nil {
		return nil, 0, err
	}
	if len(glob) > 1 {
		// globs are recursive, figure out any directories
		var globsRet []string
		for _, v := range glob {
			rGlob, _, err := iom.fileInfo(v)
			if err != nil {
				return nil, 0, err
			}
			globsRet = append(globsRet, rGlob...)
		}
		return globsRet, 0, nil
	}

	f, err := os.Open(filename)
	if err != nil {
		return nil, 0, err
	}
	defer f.Close()

	// is this a directory
	fi, err := f.Stat()
	if err != nil {
		if log.WillLog(log.DEBUG) {
			log.Debugln("fileInfo error stat: ", err)
		}
		return nil, 0, err
	}
	if fi.IsDir() {
		// walk the directory and populate glob
		glob = []string{}
		err := filepath.Walk(filename, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				return err
			}
			if info.IsDir() {
				return nil
			}
			rel, err := filepath.Rel(iom.base, path)
			if err != nil {
				return err
			}
			glob = append(glob, rel)
			return nil
		})
		if err != nil {
			return nil, 0, err
		}
		return glob, 0, nil
	}

	// we do have the file, calculate the number of parts
	parts := (fi.Size() + PART_SIZE - 1) / PART_SIZE // integer divide with ceiling instead of floor
	rel, err := filepath.Rel(iom.base, filename)
	return []string{rel}, parts, nil
}
开发者ID:npe9,项目名称:minimega,代码行数:62,代码来源:handler.go

示例11: execNpm

func execNpm(args ...string) (string, string, error) {
	if err := os.MkdirAll(filepath.Join(rootPath, "node_modules"), 0755); err != nil {
		return "", "", err
	}
	nodePath, err := filepath.Rel(rootPath, nodePath)
	if err != nil {
		return "", "", err
	}
	npmPath, err := filepath.Rel(rootPath, npmPath)
	if err != nil {
		return "", "", err
	}
	args = append([]string{npmPath}, args...)
	if debugging() {
		args = append(args, "--loglevel="+os.Getenv("GODE_DEBUG"))
	}
	cmd := exec.Command(nodePath, args...)
	cmd.Dir = rootPath
	cmd.Env = environ()
	var stdout, stderr bytes.Buffer
	cmd.Stdout = &stdout
	if debugging() {
		cmd.Stderr = os.Stderr
	} else {
		cmd.Stderr = &stderr
	}
	err = cmd.Run()
	return stdout.String(), stderr.String(), err
}
开发者ID:maxehmookau,项目名称:heroku-cli,代码行数:29,代码来源:packages.go

示例12: addFileToWorkingDir

// Adds the file to a temp directory for deploy
func (pb *PackageBuilder) addFileToWorkingDir(fpath string) (err error) {
	// Get relative dir from source
	srcDir := filepath.Dir(filepath.Dir(fpath))
	frel, _ := filepath.Rel(srcDir, fpath)

	// Try to find meta file
	hasMeta := true
	fmeta := fpath + "-meta.xml"
	fmetarel := ""
	if _, err = os.Stat(fmeta); err != nil {
		if os.IsNotExist(err) {
			hasMeta = false
		} else {
			// Has error
			return
		}
	} else {
		// Should be present since we worked back to srcDir
		fmetarel, _ = filepath.Rel(srcDir, fmeta)
	}

	fdata, err := ioutil.ReadFile(fpath)
	if err != nil {
		return
	}

	pb.Files[frel] = fdata
	if hasMeta {
		fdata, err = ioutil.ReadFile(fmeta)
		pb.Files[fmetarel] = fdata
		return
	}

	return
}
开发者ID:sfdcmahi,项目名称:force,代码行数:36,代码来源:packagebuilder.go

示例13: checkvolumes

func (svc *IService) checkvolumes(ctr *docker.Container) bool {
	dctr, err := ctr.Inspect()
	if err != nil {
		return false
	}

	if svc.Volumes != nil {
		for src, dest := range svc.Volumes {
			if p, ok := dctr.Volumes[dest]; ok {
				src, _ = filepath.EvalSymlinks(svc.getResourcePath(src))
				if rel, _ := filepath.Rel(filepath.Clean(src), p); rel != "." {
					return false
				}
			} else {
				return false
			}
		}
	}

	if isvcsVolumes != nil {
		for src, dest := range isvcsVolumes {
			if p, ok := dctr.Volumes[dest]; ok {
				if rel, _ := filepath.Rel(src, p); rel != "." {
					return false
				}
			} else {
				return false
			}
		}
	}

	return true
}
开发者ID:carriercomm,项目名称:serviced,代码行数:33,代码来源:container.go

示例14: checkMountDestination

// checkMountDestination checks to ensure that the mount destination is not over the top of /proc.
// dest is required to be an abs path and have any symlinks resolved before calling this function.
func checkMountDestination(rootfs, dest string) error {
	invalidDestinations := []string{
		"/proc",
	}
	// White list, it should be sub directories of invalid destinations
	validDestinations := []string{
		// These entries can be bind mounted by files emulated by fuse,
		// so commands like top, free displays stats in container.
		"/proc/cpuinfo",
		"/proc/diskstats",
		"/proc/meminfo",
		"/proc/stat",
		"/proc/net/dev",
	}
	for _, valid := range validDestinations {
		path, err := filepath.Rel(filepath.Join(rootfs, valid), dest)
		if err != nil {
			return err
		}
		if path == "." {
			return nil
		}
	}
	for _, invalid := range invalidDestinations {
		path, err := filepath.Rel(filepath.Join(rootfs, invalid), dest)
		if err != nil {
			return err
		}
		if path == "." || !strings.HasPrefix(path, "..") {
			return fmt.Errorf("%q cannot be mounted because it is located inside %q", dest, invalid)
		}

	}
	return nil
}
开发者ID:feiskyer,项目名称:runv,代码行数:37,代码来源:container.go

示例15: localDirectoryToRemotePath

func localDirectoryToRemotePath(directory, local, remote string, parentsPath bool) (ret string) {
	defer func() {
		ret = filepath.Clean(ret)
	}()
	if parentsPath {
		if strings.HasSuffix(remote, "/") {
			ret = remote + local
			return
		}
		ret = remote + "/" + local
		return
	}
	if strings.HasSuffix(remote, "/") {
		if strings.HasSuffix(directory, "/") {
			directory = strings.TrimSuffix(directory, "/")
		}
		path, _ := filepath.Rel(filepath.Dir(directory), local)
		ret = remote + path
		return
	} else {
		path, _ := filepath.Rel(directory, local)
		ret = remote + "/" + path
		return
	}
}
开发者ID:caiguanhao,项目名称:aliyun,代码行数:25,代码来源:upload.go


注:本文中的path/filepath.Rel函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。