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


Golang filepath.IsAbs函数代码示例

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


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

示例1: validateMounts

func validateMounts(mounts []api.Mount) error {
	for _, mount := range mounts {
		// Target must always be absolute
		if !filepath.IsAbs(mount.Target) {
			return fmt.Errorf("invalid mount target, must be an absolute path: %s", mount.Target)
		}

		switch mount.Type {
		// The checks on abs paths are required due to the container API confusing
		// volume mounts as bind mounts when the source is absolute (and vice-versa)
		// See #25253
		// TODO: This is probably not neccessary once #22373 is merged
		case api.MountTypeBind:
			if !filepath.IsAbs(mount.Source) {
				return fmt.Errorf("invalid bind mount source, must be an absolute path: %s", mount.Source)
			}
		case api.MountTypeVolume:
			if filepath.IsAbs(mount.Source) {
				return fmt.Errorf("invalid volume mount source, must not be an absolute path: %s", mount.Source)
			}
		case api.MountTypeTmpfs:
			if mount.Source != "" {
				return fmt.Errorf("invalid tmpfs source, source must be empty")
			}
		default:
			return fmt.Errorf("invalid mount type: %s", mount.Type)
		}
	}
	return nil
}
开发者ID:ilkka,项目名称:docker,代码行数:30,代码来源:validate.go

示例2: Execute

// Execute builds the archive.
func (self *TarGzPackCommand) Execute(pluginLogger plugin.Logger,
	pluginCom plugin.PluginCommunicator,
	conf *model.TaskConfig,
	stop chan bool) error {

	// if the source dir is a relative path, join it to the working dir
	if !filepath.IsAbs(self.SourceDir) {
		self.SourceDir = filepath.Join(conf.WorkDir, self.SourceDir)
	}

	// if the target is a relative path, join it to the working dir
	if !filepath.IsAbs(self.Target) {
		self.Target = filepath.Join(conf.WorkDir, self.Target)
	}

	errChan := make(chan error)
	go func() {
		errChan <- self.BuildArchive(conf.WorkDir, pluginLogger)
	}()

	select {
	case err := <-errChan:
		return err
	case <-stop:
		pluginLogger.LogExecution(slogger.INFO, "Received signal to terminate"+
			" execution of targz pack command")
		return nil
	}

}
开发者ID:amidvidy,项目名称:evergreen,代码行数:31,代码来源:tar_gz_pack_command.go

示例3: LoadConfig

func LoadConfig(file string) (*Config, error) {
	fixedFile := os.ExpandEnv(file)
	rawConfig, err := ioutil.ReadFile(fixedFile)
	if err != nil {
		log.Error("Failed to read point config file (%s): %v", file, err)
		return nil, err
	}

	config := &Config{}
	err = json.Unmarshal(rawConfig, config)
	if err != nil {
		log.Error("Failed to load point config: %v", err)
		return nil, err
	}

	if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 {
		config.InboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.InboundConfigValue.File)
	}

	if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 {
		config.OutboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.OutboundConfigValue.File)
	}

	return config, err
}
开发者ID:nenew,项目名称:v2ray-core,代码行数:25,代码来源:json.go

示例4: PostProcess

// PostProcess post-processes the flags to fix any compatibility issue.
func (a *ArchiveOptions) PostProcess(cwd string) {
	// Set default blacklist only if none is set.
	if len(a.Blacklist) == 0 {
		// This cannot be generalized as ".*" as there is known use that require
		// a ".pki" directory to be mapped.
		a.Blacklist = common.Strings{
			".git",
			".hg",
			".svn",
		}
	}
	if !filepath.IsAbs(a.Isolate) {
		a.Isolate = filepath.Join(cwd, a.Isolate)
	}
	a.Isolate = filepath.Clean(a.Isolate)

	if !filepath.IsAbs(a.Isolated) {
		a.Isolated = filepath.Join(cwd, a.Isolated)
	}
	a.Isolated = filepath.Clean(a.Isolated)

	for k, v := range a.PathVariables {
		// This is due to a Windows + GYP specific issue, where double-quoted paths
		// would get mangled in a way that cannot be resolved unless a space is
		// injected.
		a.PathVariables[k] = strings.TrimSpace(v)
	}
}
开发者ID:shishkander,项目名称:luci-go,代码行数:29,代码来源:isolate.go

示例5: TestAbs

func TestAbs(t *testing.T) {
	oldwd, err := os.Getwd()
	if err != nil {
		t.Fatal("Getwd failed: ", err)
	}
	defer os.Chdir(oldwd)

	root, err := ioutil.TempDir("", "TestAbs")
	if err != nil {
		t.Fatal("TempDir failed: ", err)
	}
	defer os.RemoveAll(root)

	wd, err := os.Getwd()
	if err != nil {
		t.Fatal("getwd failed: ", err)
	}
	err = os.Chdir(root)
	if err != nil {
		t.Fatal("chdir failed: ", err)
	}
	defer os.Chdir(wd)

	for _, dir := range absTestDirs {
		err = os.Mkdir(dir, 0777)
		if err != nil {
			t.Fatal("Mkdir failed: ", err)
		}
	}

	err = os.Chdir(absTestDirs[0])
	if err != nil {
		t.Fatal("chdir failed: ", err)
	}

	for _, path := range absTests {
		path = strings.Replace(path, "$", root, -1)
		info, err := os.Stat(path)
		if err != nil {
			t.Errorf("%s: %s", path, err)
			continue
		}

		abspath, err := filepath.Abs(path)
		if err != nil {
			t.Errorf("Abs(%q) error: %v", path, err)
			continue
		}
		absinfo, err := os.Stat(abspath)
		if err != nil || !os.SameFile(absinfo, info) {
			t.Errorf("Abs(%q)=%q, not the same file", path, abspath)
		}
		if !filepath.IsAbs(abspath) {
			t.Errorf("Abs(%q)=%q, not an absolute path", path, abspath)
		}
		if filepath.IsAbs(path) && abspath != filepath.Clean(path) {
			t.Errorf("Abs(%q)=%q, isn't clean", path, abspath)
		}
	}
}
开发者ID:TomHoenderdos,项目名称:go-sunos,代码行数:60,代码来源:path_test.go

示例6: generateFile

func generateFile(templatePath, destinationPath string, debugTemplates bool) error {
	if !filepath.IsAbs(templatePath) {
		return fmt.Errorf("Template path '%s' is not absolute!", templatePath)
	}

	if !filepath.IsAbs(destinationPath) {
		return fmt.Errorf("Destination path '%s' is not absolute!", destinationPath)
	}

	var slice []byte
	var err error
	if slice, err = ioutil.ReadFile(templatePath); err != nil {
		return err
	}
	s := string(slice)
	result, err := generateTemplate(s, filepath.Base(templatePath))
	if err != nil {
		return err
	}

	if debugTemplates {
		log.Printf("Printing parsed template to stdout. (It's delimited by 2 character sequence of '\\x00\\n'.)\n%s\x00\n", result)
	}

	if err = ioutil.WriteFile(destinationPath, []byte(result), 0664); err != nil {
		return err
	}

	return nil
}
开发者ID:tnozicka,项目名称:goenvtemplator,代码行数:30,代码来源:template.go

示例7: cleanedPath

func (f *FolderConfiguration) cleanedPath() string {
	cleaned := f.RawPath

	// Attempt tilde expansion; leave unchanged in case of error
	if path, err := osutil.ExpandTilde(cleaned); err == nil {
		cleaned = path
	}

	// Attempt absolutification; leave unchanged in case of error
	if !filepath.IsAbs(cleaned) {
		// Abs() looks like a fairly expensive syscall on Windows, while
		// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
		// somewhat faster in the general case, hence the outer if...
		if path, err := filepath.Abs(cleaned); err == nil {
			cleaned = path
		}
	}

	// Attempt to enable long filename support on Windows. We may still not
	// have an absolute path here if the previous steps failed.
	if runtime.GOOS == "windows" && filepath.IsAbs(cleaned) && !strings.HasPrefix(f.RawPath, `\\`) {
		return `\\?\` + cleaned
	}

	return cleaned
}
开发者ID:hotelzululima,项目名称:syncthing,代码行数:26,代码来源:folderconfiguration.go

示例8: filterPath

// filterPath cleans and makes absolute the path passed in.
//
// On windows it makes the path UNC also.
func (f *Fs) filterPath(s string) string {
	s = filterFragment(s)
	if runtime.GOOS == "windows" {
		if !filepath.IsAbs(s) && !strings.HasPrefix(s, "\\") {
			s2, err := filepath.Abs(s)
			if err == nil {
				s = s2
			}
		}

		if f.nounc {
			return s
		}
		// Convert to UNC
		return uncPath(s)
	}

	if !filepath.IsAbs(s) {
		s2, err := filepath.Abs(s)
		if err == nil {
			s = s2
		}
	}

	return s
}
开发者ID:pombredanne,项目名称:rclone,代码行数:29,代码来源:local.go

示例9: main

func main() {

	if len(os.Args) != 3 {
		fmt.Println("ln target linkname")
		os.Exit(1)
	}

	curDir, _ := os.Getwd()

	target := os.Args[1]
	if !filepath.IsAbs(target) {
		target = curDir + string(os.PathSeparator) + target
	}

	linkname := os.Args[2]
	if !filepath.IsAbs(linkname) {
		linkname = curDir + string(os.PathSeparator) + linkname
	}

	err := os.Symlink(target, linkname)

	if err != nil {
		fmt.Printf("创建失败:%s\n", err)
		os.Exit(2)
	}
}
开发者ID:XiaoboYuan,项目名称:myblog_article_code,代码行数:26,代码来源:ln.go

示例10: Path

func (f FolderConfiguration) Path() string {
	// This is intentionally not a pointer method, because things like
	// cfg.Folders["default"].Path() should be valid.

	// Attempt tilde expansion; leave unchanged in case of error
	if path, err := osutil.ExpandTilde(f.RawPath); err == nil {
		f.RawPath = path
	}

	// Attempt absolutification; leave unchanged in case of error
	if !filepath.IsAbs(f.RawPath) {
		// Abs() looks like a fairly expensive syscall on Windows, while
		// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
		// somewhat faster in the general case, hence the outer if...
		if path, err := filepath.Abs(f.RawPath); err == nil {
			f.RawPath = path
		}
	}

	// Attempt to enable long filename support on Windows. We may still not
	// have an absolute path here if the previous steps failed.
	if runtime.GOOS == "windows" && filepath.IsAbs(f.RawPath) && !strings.HasPrefix(f.RawPath, `\\`) {
		return `\\?\` + f.RawPath
	}

	return f.RawPath
}
开发者ID:modulexcite,项目名称:syncthing-cli,代码行数:27,代码来源:config.go

示例11: Run

func (s *Serve) Run() {
	if root == "" {
		if wd, err := os.Getwd(); err != nil {
			panic(err)
		} else {
			root = wd
		}
	} else if !filepath.IsAbs(root) {
		panic("fileroot must be an absolute path")
	}
	var writer io.Writer
	if logFilePath != "" {
		if !filepath.IsAbs(logFilePath) {
			logFilePath = AbsPath(logFilePath)
		}
		if f, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE, 0666); err != nil {
			panic(err)
		} else {
			writer = f
		}
	} else {
		writer = os.Stdout
	}
	logger = log.New(writer, "", 0)
	serveStatic()
	http.HandleFunc("/", Handler)
	Log("Running Gadget at 0.0.0.0:" + Port + "...")
	err := http.ListenAndServe(":"+Port, nil)
	if err != nil {
		panic(err)
	}
}
开发者ID:ustbgaofan,项目名称:gadget,代码行数:32,代码来源:env.go

示例12: LoadConfig

func LoadConfig(cfgfile string) (cfg *InventoryConfig, err error) {

	if !filepath.IsAbs(cfgfile) {
		cfgfile, _ = filepath.Abs(cfgfile)
	}

	var b []byte

	if b, err = ioutil.ReadFile(cfgfile); err != nil {
		return
	}
	if err = json.Unmarshal(b, &cfg); err != nil {
		return
	}

	if !filepath.IsAbs(cfg.Datastore.Config.MappingFile) {
		cfg.Datastore.Config.MappingFile, _ = filepath.Abs(cfg.Datastore.Config.MappingFile)
	}

	if !filepath.IsAbs(cfg.Datastore.BackupDir) {
		cfg.Datastore.BackupDir, _ = filepath.Abs(cfg.Datastore.BackupDir)
	}

	if !filepath.IsAbs(cfg.Auth.GroupsFile) {
		cfg.Auth.GroupsFile, _ = filepath.Abs(cfg.Auth.GroupsFile)
	}

	return
}
开发者ID:krets,项目名称:infra-inventory,代码行数:29,代码来源:config.go

示例13: fixCompDirArg

func fixCompDirArg(argDir, path string) string {
	if filepath.IsAbs(path) {
		if filepath.IsAbs(argDir) {
			return argDir
		} else {
			abs, err := filepath.Abs(argDir)
			if err != nil {
				log.Panic("unable to get absolute path: ",
					err)
			}
			return filepath.Clean(abs)
		}
	} else {
		if filepath.IsAbs(argDir) {
			wd, err := os.Getwd()
			if err != nil {
				log.Panic("unable to get working directoy: ",
					err)
			}
			rel, err := filepath.Rel(wd, argDir)
			if err != nil {
				log.Panic("unable to get relative path: ",
					err)
			}
			return filepath.Clean(rel)
		} else {
			return filepath.Clean(path + "/" + argDir)
		}
	}
}
开发者ID:rahulpathakgit,项目名称:navc,代码行数:30,代码来源:parse.go

示例14: findRoot

func findRoot(dir string) (root, relative string, err error) {
	abspath, err := filepath.Abs(dir)
	if err != nil {
		return "", "", fmt.Errorf("Cannot get abs of '%s': %s\n", dir)
	}
	grzpath := RootList()
	if len(grzpath) == 0 {
		return "", "", fmt.Errorf("No roots (GRZ_PATH empty)")
	}
	for _, path := range grzpath {
		if len(path) == 0 || !filepath.IsAbs(path) {
			return "", "", fmt.Errorf("path '%s' is not absolute", path)
		}
		if strings.HasPrefix(abspath, path) {
			if relative, err = filepath.Rel(path, abspath); err != nil {
				return "", "", err
			}
			root = path
			break
		}
	}
	if root == "" {
		if !filepath.IsAbs(dir) {
			pwd, err := os.Getwd()
			if err != nil {
				return "", "", fmt.Errorf("Cannot get working dir: %s\n", err)
			}
			root, relative = pwd, dir
		} else {
			return "", "", fmt.Errorf("Root directory '/' not allowed")
		}
	}
	return
}
开发者ID:pauek,项目名称:garzon.old,代码行数:34,代码来源:read.go

示例15: Renameat

// Poor man's Renameat
func Renameat(olddirfd int, oldpath string, newdirfd int, newpath string) (err error) {
	chdirMutex.Lock()
	defer chdirMutex.Unlock()
	// Unless both paths are absolute we have to save the old working dir and
	// Chdir(oldWd) back to it in the end. If we error out before the first
	// chdir, Chdir(oldWd) is unneccassary but does no harm.
	if !filepath.IsAbs(oldpath) || !filepath.IsAbs(newpath) {
		oldWd, err := os.Getwd()
		if err != nil {
			return err
		}
		defer os.Chdir(oldWd)
	}
	// Make oldpath absolute
	oldpath, err = dirfdAbs(olddirfd, oldpath)
	if err != nil {
		return err
	}
	// Make newpath absolute
	newpath, err = dirfdAbs(newdirfd, newpath)
	if err != nil {
		return err
	}
	return syscall.Rename(oldpath, newpath)
}
开发者ID:rfjakob,项目名称:gocryptfs,代码行数:26,代码来源:sys_darwin.go


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