Golang Rel示例

说明

golang rel示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: path/filepath

示例#1
文件: changes.go 项目: enzochiau/drive

// 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
}

示例#2
文件: go_darwin_arm_exec.go 项目: tidatida/go

// 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,
	)
}

示例#3
文件: util.go 项目: CodeJuan/kubernetes

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))
}

示例#4
文件: packages.go 项目: istrategylabs/heroku-cli

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
}

示例#5
文件: write_tar.go 项目: pcfdev-forks/houdini

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
}

示例#6
文件: smart.go 项目: r3nic1e/bbdownloader

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()
	}
}

示例#7
文件: util.go 项目: CodeJuan/kubernetes

// 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))
}

示例#8
文件: simple.go 项目: dgonyeo/fsdiffer

// 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
}

示例#9
文件: beforeafter.go 项目: dgonyeo/fsdiffer

// 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
}

示例#10
文件: handler.go 项目: npe9/minimega

// 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
}

示例#11
文件: packages.go 项目: maxehmookau/heroku-cli

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
}

示例#12
文件: packagebuilder.go 项目: sfdcmahi/force

// 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
}

示例#13
文件: container.go 项目: carriercomm/serviced

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
}

示例#14

// 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) {
	cwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	if root := runtime.GOROOT(); strings.HasPrefix(cwd, root) {
		subdir, err := filepath.Rel(root, cwd)
		if err != nil {
			log.Fatal(err)
		}
		return subdir, true
	}

	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
		}
	}
	log.Fatalf("the current path %q is not in either GOROOT(%q) or GOPATH(%q)",
		cwd, runtime.GOROOT(), build.Default.GOPATH)
	return "", false
}

示例#15
文件: container.go 项目: feiskyer/runv

// 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
}

示例#16
文件: upload.go 项目: caiguanhao/aliyun

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
	}
}

示例#17
文件: save.go 项目: abiosoft/wgo

func (w *workspace) getOutsidePackages(targets []string) map[string]string {
	os.Setenv("GOPATH", w.Gopath(true))

	for _, gopath := range w.Gopaths {
		target := "./" + gopath + "/src/..." // filepath.Join() doesn't like a leading dot.
		targets = append(targets, target)
	}

	goListTestArgs := []string{"list", "-e", "-f", "{{range .TestImports}}{{.}}\n{{end}}"}
	goListTestArgs = append(goListTestArgs, targets...)
	// fmt.Printf("%q\n", goListTestArgs)
	var testBuf bytes.Buffer
	cmd := exec.Command("go", goListTestArgs...)
	cmd.Dir = w.Root
	cmd.Stdout = &testBuf
	orExit(cmd.Run())
	for _, pkg := range strings.Split(testBuf.String(), "\n") {
		targets = append(targets, pkg)
	}

	goListArgs := []string{"list", "-e", "-f", "{{.ImportPath}}\n{{range .Deps}}{{.}}\n{{end}}"}
	goListArgs = append(goListArgs, targets...)
	// fmt.Printf("%q\n", goListArgs)
	var buf bytes.Buffer
	cmd = exec.Command("go", goListArgs...)
	cmd.Dir = w.Root
	cmd.Stdout = &buf
	orExit(cmd.Run())

	goroot := runtime.GOROOT()
	build.Default.GOPATH = w.Gopath(true)

	pkgs := map[string]string{}
	for _, pkg := range strings.Split(buf.String(), "\n") {
		if pkg == "" {
			continue
		}
		p, err := build.Import(pkg, w.Root, build.FindOnly)
		if err != nil {
			continue
		}
		if x, err := filepath.Rel(goroot, p.Dir); err == nil && !strings.HasPrefix(x, "..") {
			continue
		}
		pkgs[pkg] = p.Dir
	}

	for pkg, dir := range pkgs {
		if !filepath.IsAbs(dir) {
			continue
		}
		if x, err := filepath.Rel(w.Root, dir); err == nil && !strings.HasPrefix(x, "..") {
			continue
		}
		pkgs[pkg] = dir
	}

	return pkgs
}

示例#18
文件: pipe_test.go 项目: jhautefeuille/restic

func testPipeWalkerRootWithPath(path string, t *testing.T) {
	pattern := filepath.Join(path, "*")
	rootPaths, err := filepath.Glob(pattern)
	OK(t, err)

	for i, p := range rootPaths {
		rootPaths[i], err = filepath.Rel(path, p)
		OK(t, err)
	}

	t.Logf("paths in %v (pattern %q) expanded to %v items", path, pattern, len(rootPaths))

	done := make(chan struct{})
	defer close(done)

	jobCh := make(chan pipe.Job)
	var jobs []pipe.Job

	worker := func(wg *sync.WaitGroup) {
		defer wg.Done()
		for job := range jobCh {
			jobs = append(jobs, job)
		}
	}

	var wg sync.WaitGroup
	wg.Add(1)
	go worker(&wg)

	filter := func(p string, fi os.FileInfo) bool {
		p, err := filepath.Rel(path, p)
		OK(t, err)
		return dirsInPath(p) <= 1
	}

	resCh := make(chan pipe.Result, 1)
	pipe.Walk([]string{path}, filter, done, jobCh, resCh)

	wg.Wait()

	t.Logf("received %d jobs", len(jobs))

	for i, job := range jobs[:len(jobs)-1] {
		path := job.Path()
		if path == "." || path == ".." || path == string(filepath.Separator) {
			t.Errorf("job %v has invalid path %q", i, path)
		}
	}

	lastPath := jobs[len(jobs)-1].Path()
	if lastPath != "" {
		t.Errorf("last job has non-empty path %q", lastPath)
	}

	if len(jobs) < len(rootPaths) {
		t.Errorf("want at least %v jobs, got %v for path %v\n", len(rootPaths), len(jobs), path)
	}
}

示例#19
文件: project.go 项目: nebulon42/magnacarto

// findProjects searches for .mml files in path and in all sub-directories of
// path, but not any deeper.
func findProjects(path string) ([]project, error) {
	projects := []project{}
	var mmls []string
	if files, err := filepath.Glob(filepath.Join(path, "*.mml")); err != nil {
		return nil, err
	} else {
		mmls = append(mmls, files...)
	}

	if files, err := filepath.Glob(filepath.Join(path, "*", "*.mml")); err != nil {
		return nil, err
	} else {
		mmls = append(mmls, files...)
	}

	for _, mmlFile := range mmls {
		projDir := filepath.Dir(mmlFile)
		projBase, _ := filepath.Rel(path, projDir)

		mssFiles, err := findMSS(projDir)
		if err != nil {
			return nil, err
		}

		r, err := os.Open(mmlFile)
		if err != nil {
			return nil, err
		}
		parsedMML, err := mml.Parse(r)
		r.Close()
		if err != nil {
			return nil, fmt.Errorf("error parsing %s: %v", mmlFile, err)
		}

		lastChange := lastModTime(append([]string{mmlFile}, mssFiles...)...)

		// remove base dir from mml/mss
		mmlFile = filepath.Base(mmlFile)
		for i := range mssFiles {
			mssFiles[i], _ = filepath.Rel(projDir, mssFiles[i])
		}

		name := parsedMML.Name

		projects = append(projects,
			project{
				Name:         name,
				Base:         projBase,
				LastChange:   lastChange,
				MML:          mmlFile,
				MCP:          strings.TrimSuffix(mmlFile, filepath.Ext(mmlFile)) + ".mcp",
				AvailableMSS: mssFiles,
				SelectedMSS:  parsedMML.Stylesheets,
			})
	}

	return projects, nil
}

示例#20
文件: event.go 项目: paytonrules/photolibrary

func (e Event) ImagesRelativeTo(root string) *Event {
	imagesWithRelativePaths := e.mapImages(func(original Image) Image {
		relPath, _ := filepath.Rel(root, original.GetFullPath())
		relPathThumbnail, _ := filepath.Rel(root, original.GetThumbnail())
		return &FileSystemImage{FullPath: relPath, Thumbnail: relPathThumbnail}
	})

	return &Event{Images: imagesWithRelativePaths, Events: e.Events}
}

示例#21
文件: ln.go 项目: u-root/u-root

// relLink get the relative link path between
// a target and linkName fpath
// between a linkName operand and the target
// HMM, i don't have sure if that works well...
func relLink(target, linkName string) (string, error) {
	base := filepath.Dir(linkName)
	if newTarget, err := filepath.Rel(base, target); err == nil {
		return newTarget, nil
	} else if absLink, err := filepath.Abs(linkName); err == nil {
		return filepath.Rel(absLink, target)
	}

	return "", nil
}

示例#22
文件: sequins_test.go 项目: delkyd/sequins

func directoryCopy(t *testing.T, dest, src string) error {
	return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
		// Swallow errors, since sometimes other jobs will clean up the .manifest partway through
		// the directory copy, which causes errors
		if err != nil {
			return nil
		} else if strings.HasPrefix(filepath.Base(path), ".") {
			if info.IsDir() {
				return filepath.SkipDir
			}
			return nil
		} else if info.IsDir() {
			return nil
		}

		reldir, err := filepath.Rel(src, filepath.Dir(path))
		require.NoError(t, err)
		if err != nil {
			return err
		}

		err = os.MkdirAll(filepath.Join(dest, reldir), 0755)
		require.NoError(t, err)
		if err != nil {
			return err
		}

		rel, err := filepath.Rel(src, path)
		require.NoError(t, err)
		if err != nil {
			return err
		}

		destfilename := filepath.Join(dest, rel)
		t.Logf("Copying %s -> %s\n", path, destfilename)

		srcfile, err := os.Open(path)
		require.NoError(t, err)
		if err != nil {
			return err
		}
		defer srcfile.Close()

		destfile, err := os.Create(destfilename)
		require.NoError(t, err)
		if err != nil {
			return err
		}
		defer destfile.Close()

		_, err = io.Copy(destfile, srcfile)
		require.NoError(t, err)
		return err
	})
}

示例#23
文件: misc.go 项目: petitbon/tiedot

// Pause all activities and make a dump of entire database to another file system location.
func Dump(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Cache-Control", "must-revalidate")
	w.Header().Set("Content-Type", "application/json")
	var dest string
	if !Require(w, r, "dest", &dest) {
		return
	}
	// Note that symbol links are skipped!
	walkFun := func(currPath string, info os.FileInfo, err error) error {
		if info.IsDir() {
			// Calculate directory path at destination and create it
			relPath, err := filepath.Rel(V3DB.Dir, currPath)
			if err != nil {
				return err
			}
			destDir := path.Join(dest, relPath)
			if err := os.MkdirAll(destDir, 0700); err != nil {
				return err
			}
			tdlog.Printf("Dump created directory %s with permission 0700", destDir)
		} else {
			// Open the file to be copied (collection data/index)
			src, err := os.Open(currPath)
			if err != nil {
				return err
			}
			// Calculate file path at destination and create it
			relPath, err := filepath.Rel(V3DB.Dir, currPath)
			if err != nil {
				return err
			}
			destPath := path.Join(dest, relPath)
			destFile, err := os.Create(destPath)
			if err != nil {
				return err
			}
			// Copy from source to destination
			written, err := io.Copy(destFile, src)
			if err != nil {
				return err
			}
			tdlog.Printf("Dump create file %s with permission 666 (before umask), size is %d", destPath, written)
		}
		return nil
	}
	V3Sync.Lock()
	defer V3Sync.Unlock()
	V3DB.Flush()
	err := filepath.Walk(V3DB.Dir, walkFun)
	if err != nil {
		http.Error(w, fmt.Sprint(err), 500)
	}
}

示例#24
文件: buildkit.go 项目: alecthomas/buildkit

func (b *BuildKit) MakeDeps(w io.Writer) error {
	sorted, err := SortTargets(b.targets)
	if err != nil {
		return err
	}
	for _, target := range sorted {
		err := target.Initialize(b.log.New(target))
		if err != nil {
			return err
		}
	}

	cwd, err := os.Getwd()
	if err != nil {
		return err
	}
	for _, target := range sorted {
		inputs, outputs, err := ExpandInputsOutputs(target)
		if err != nil {
			return err
		}
		if len(outputs) == 0 {
			continue
		}
		fmt.Fprintf(w, "# %s\n", target.ID())
		l := 0
		for output := range outputs {
			output, err = filepath.Rel(cwd, output)
			if err != nil {
				return err
			}
			fmt.Fprintf(w, "%s ", output)
			l += len(output) + 1
		}
		fmt.Fprintf(w, ":")
		l++
		for input := range inputs {
			input, err = filepath.Rel(cwd, input)
			if err != nil {
				return err
			}
			if l+len(input)+1 > 120 {
				fmt.Fprintf(w, " \\\n  ")
				l = 2
			}
			fmt.Fprintf(w, " %s", input)
			l += len(input) + 1
		}
		fmt.Fprintf(w, "\n")
	}
	return nil
}

示例#25
文件: reinhardt.go 项目: r0fls/reinhardt

func new_project(name string) {
	base := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "r0fls", "reinhardt")
	os.MkdirAll(filepath.Join(name, "app", "models"), 0700)
	text, err := ioutil.ReadFile(filepath.Join(base, "src", "app_files", "models.go"))
	check(err)
	err = ioutil.WriteFile(filepath.Join(name, "app", "models", "models.go"), text, 0644)
	check(err)

	os.Mkdir(filepath.Join(name, "app", "views"), 0700)

	text, err = ioutil.ReadFile(filepath.Join(base, "src", "app_files", "views.go"))
	check(err)
	err = ioutil.WriteFile(filepath.Join(name, "app", "views", "views.go"), text, 0644)
	check(err)

	os.Mkdir(filepath.Join(name, "app", "temps"), 0700)

	text, err = ioutil.ReadFile(filepath.Join(base, "src", "app_files", "home.html"))
	check(err)
	err = ioutil.WriteFile(filepath.Join(name, "app", "temps", "home.html"), text, 0644)
	check(err)

	text, err = ioutil.ReadFile(filepath.Join(base, "src", "app_files", "settings.json"))
	check(err)
	dir, _ := os.Getwd()
	gopath := os.Getenv("GOPATH")
	local, _ := filepath.Rel(gopath, dir)
	local, _ = filepath.Rel("src", local)
	if string([]rune(local)[0]) == "/" || string([]rune(local)[0]) == "\\" {
		local = local[1:]
	}
	local = filepath.Join(local, name)
	err = ioutil.WriteFile(filepath.Join(name, "settings.json"), []byte(fmt.Sprintf(string(text), dir, name, gopath, local)), 0644)
	check(err)

	c := config.Load_config(filepath.Join(name, "settings.json"))
	text, err = ioutil.ReadFile(filepath.Join(base, "src", "app_files", "manager.go"))
	check(err)
	tmpl, _ := template.New("manager").Parse(string(text))
	f, err := os.Create(filepath.Join(name, "manager.go"))
	check(err)
	err = tmpl.Execute(f, c)
	check(err)

	text, err = ioutil.ReadFile(filepath.Join(base, "src", "app_files", "urls.go"))
	check(err)
	tmpl, _ = template.New("urls").Parse(string(text))
	f, err = os.Create(filepath.Join(name, "app", "urls.go"))
	check(err)
	err = tmpl.Execute(f, c)
	check(err)
}

示例#26
文件: builder.go 项目: ponyta/gob

func (g *Gob) checkIsSource(srcDir, buildDir, path string) ([]string, bool) {
	var absPath string
	toReturn := make([]string, 4) // [0]=dir, [1]=filename, [2]=pkgPath [3]=binary
	// Handle filename and "package" as inputs
	toReturn[0], toReturn[1] = filepath.Split(path)

	pkgPath := filepath.Join(srcDir, path)
	if _, err := os.Stat(pkgPath); !os.IsNotExist(err) {
		// we were passed the package name to build and run
		toReturn[2] = path
		absPath = pkgPath
	} else if strings.Contains(toReturn[1], ".") {
		toReturn[0], err = filepath.Abs(toReturn[0])
		if err != nil {
			g.PrintErr(err)
			return nil, false
		}

		toReturn[2], err = filepath.Rel(srcDir, toReturn[0])
		if err != nil {
			g.PrintErr(err)
			return nil, false
		}

		absPath = filepath.Join(srcDir, toReturn[2], toReturn[1])
	} else {
		toReturn[0], err = filepath.Abs(path)
		if err != nil {
			g.PrintErr(err)
			return nil, false
		}

		toReturn[2], err = filepath.Rel(srcDir, toReturn[0])
		if err != nil {
			g.PrintErr(err)
			return nil, false
		}

		absPath = filepath.Join(srcDir, toReturn[2])
	}

	// Save our "tmp" binary as the last element of the input
	toReturn[3] = buildDir + "/" + filepath.Base(toReturn[2])

	// Make sure the file/package we're trying to build exists
	if _, err := os.Stat(absPath); os.IsNotExist(err) {
		g.Print("Please provide a valid source file/package to run.")
		return nil, false
	}

	return toReturn, true
}

示例#27
文件: sprite.go 项目: xarem/wellington

// OutputPath generates a unique filename based on the relative path
// from image directory to build directory and the files matched in
// the glob lookup.  OutputPath is not cache safe.
func (l *Sprite) OutputPath() (string, error) {
	l.outFileMu.RLock()
	outFile := l.outFile
	l.outFileMu.RUnlock()
	// Pull cached output path
	if len(outFile) > 0 {
		return l.outFile, nil
	}

	l.globMu.RLock()
	globs := l.globs
	l.globMu.RUnlock()
	if len(globs) == 0 {
		return "", ErrNoPattern
	}

	l.optsMu.RLock()
	path, err := filepath.Rel(l.opts.BuildDir, l.opts.GenImgDir)
	pack := l.opts.Pack
	padding := l.opts.Padding
	l.optsMu.RUnlock()
	if err != nil {
		return "", err
	}
	// TODO: remove this
	if path == "." {
		path = "image"
	}
	relglobs := make([]string, len(globs))
	for i := range globs {
		if !filepath.IsAbs(globs[i]) {
			relglobs[i] = globs[i]
			continue
		}
		relglobs[i], err = filepath.Rel(l.opts.GenImgDir, globs[i])
		if err != nil {
			return "", err
		}

	}
	hasher := md5.New()
	seed := pack + strconv.Itoa(padding) + "|" +
		filepath.ToSlash(path+"|"+strings.Join(relglobs, "|"))
	hasher.Write([]byte(seed))
	salt := hex.EncodeToString(hasher.Sum(nil))[:6]
	outFile = filepath.Join(path, salt+".png")

	l.outFileMu.Lock()
	l.outFile = outFile
	l.outFileMu.Unlock()
	return outFile, nil
}

示例#28
文件: main.go 项目: caesarxuchao/caesarxuchao.github.io

func fixURL(filename string, u *url.URL) bool {
	if u.Host != "" || u.Path == "" {
		return false
	}

	target := filepath.Join(filepath.Dir(filename), u.Path)
	if fi, err := os.Stat(target); os.IsNotExist(err) {
		// We're linking to something we didn't copy over. Send
		// it through the redirector.
		rel, err := filepath.Rel(*outputDir, target)
		if err != nil {
			return false
		}

		if fileExistsInBranch(rel) {
			u.Path = filepath.Join(*branch, rel)
			u.Host = "releases.k8s.io"
			u.Scheme = "https"
			return true
		}
	} else if fi.IsDir() {
		// If there's no README.md in the directory, redirect to github
		// for the directory view.
		files, err := filepath.Glob(target + "/*")
		if err != nil {
			return false
		}

		hasReadme := false
		for _, f := range files {
			if strings.ToLower(filepath.Base(f)) == "readme.md" {
				hasReadme = true
			}
		}
		if !hasReadme {
			rel, err := filepath.Rel(*outputDir, target)
			if err != nil {
				return false
			}
			u.Path = filepath.Join(*branch, rel)
			u.Host = "releases.k8s.io"
			u.Scheme = "https"
			return true
		}
	} else if strings.HasSuffix(u.Path, ".md") {
		u.Path = u.Path[:len(u.Path)-3] + ".html"
		return true
	}

	return false
}

示例#29
文件: instrument.go 项目: soniah/gosloppy

func (i *Instrumentable) instrumentPatchable(outdir, relpath string, pkg *patch.PatchablePkg, f func(file *patch.PatchableFile) patch.Patches) error {
	path := ""
	if build.IsLocalImport(relpath) {
		path = filepath.Join("locals", relpath)
		path = strings.Replace(path, "..", "__", -1)
	} else if relpath != "" {
		path = filepath.Join("gopath", i.pkg.ImportPath)
	}
	if err := os.MkdirAll(filepath.Join(outdir, path), 0755); err != nil {
		return err
	}
	for filename, file := range pkg.Files {
		if outfile, err := os.Create(filepath.Join(outdir, path, filepath.Base(filename))); err != nil {
			return err
		} else {
			patches := f(file)
			// TODO(elazar): check the relative path from current location (aka relpath, path), to the import path
			// (aka v)
			for _, imp := range file.File.Imports {
				switch v := imp.Path.Value[1 : len(imp.Path.Value)-1]; {
				case v == i.pkg.ImportPath:
					patches = appendNoContradict(patches, patch.Replace(imp.Path, `"."`))
				case !i.relevantImport(v):
					continue
				case build.IsLocalImport(v):
					rel, err := filepath.Rel(path, filepath.Join("locals", v))
					if err != nil {
						return err
					}
					patches = appendNoContradict(patches, patch.Replace(imp.Path, `"./`+rel+`"`))
				default:
					if v == i.name {
						v = ""
					} else {
						v = filepath.Join("gopath", v)
					}
					rel, err := filepath.Rel(path, v)
					if err != nil {
						return err
					}
					patches = appendNoContradict(patches, patch.Replace(imp.Path, `"./`+rel+`"`))
				}
			}
			file.FprintPatched(outfile, file.File, patches)
			if err := outfile.Close(); err != nil {
				return err
			}
		}
	}
	return nil
}

示例#30
文件: directory_crawler.go 项目: jimmysawczuk/less-tree

func (c *directoryCrawler) parseDirectory(prefix string, lessDir, cssDir *os.File) {
	files, err := lessDir.Readdir(-1)
	if err != nil {
		fmt.Printf("Can't scan %s for files", lessDir.Name())
		return
	}

	for _, v := range files {
		if v.IsDir() {
			if strings.HasPrefix(v.Name(), "_") {
				// We're dealing with an underscore-prefixed directory.
				if isVerbose {
					dir, _ := filepath.Rel(c.rootLESS.Name(), filepath.Join(lessDir.Name(), v.Name()))
					fmt.Printf("skip: %s\n", dir+"/*")
				}

				continue
			}

			lessDeeper, _ := os.Open(lessDir.Name() + string(os.PathSeparator) + v.Name())
			cssDeeper, err := os.Open(cssDir.Name() + string(os.PathSeparator) + v.Name())
			if err != nil {
				if os.IsNotExist(err) {
					err = os.Mkdir(cssDir.Name()+string(os.PathSeparator)+v.Name(), 0755)
					if err != nil {
						fmt.Println("Can't create css directory")
						return
					}
					cssDeeper, _ = os.Open(cssDir.Name() + string(os.PathSeparator) + v.Name())
				}
			}

			c.parseDirectory(v.Name()+string(os.PathSeparator), lessDeeper, cssDeeper)
		}

		if !v.IsDir() && lessFilename.MatchString(v.Name()) {
			if strings.HasPrefix(v.Name(), "_") {

				// We're dealing with an underscore-prefixed file (an include).
				if isVerbose {
					filename, _ := filepath.Rel(c.rootLESS.Name(), filepath.Join(lessDir.Name(), v.Name()))
					fmt.Printf("skip: %s\n", filename)
				}

				continue
			}

			c.addFunc(c, lessDir, cssDir, v)
		}
	}
}

展开阅读全文