本文整理汇总了Golang中path.IsAbs函数的典型用法代码示例。如果您正苦于以下问题:Golang IsAbs函数的具体用法?Golang IsAbs怎么用?Golang IsAbs使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsAbs函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: discoverExecName
/*
Returns absolute path of executing file.
WARNING: this must be called before
changing the current directory
*/
func discoverExecName() string {
if DEBUG {
log.Print("Debug: discoverExecName\n")
}
f := os.Args[0]
if path.IsAbs(f) {
return f
}
wd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("Getwd failed: %s", err))
}
_, err = os.Stat(f)
if err == nil { // relative file exists
return path.Clean(path.Join(wd, f))
} // not exists? lookup in path
f2, err := exec.LookPath(f)
if err != nil {
panic(fmt.Sprintf("lookpath failed: %s", err))
}
if path.IsAbs(f2) {
return f2
}
return path.Clean(path.Join(wd, f2))
}
示例2: main
func main() {
flag.StringVar(&etcdctlPath, "etcdctl-path", "/usr/bin/etcdctl", "absolute path to etcdctl executable")
flag.StringVar(&etcdPath, "etcd-path", "/usr/bin/etcd2", "absolute path to etcd2 executable")
flag.StringVar(&etcdRestoreDir, "etcd-restore-dir", "/var/lib/etcd2-restore", "absolute path to etcd2 restore dir")
flag.StringVar(&etcdName, "etcd-name", "default", "name of etcd2 node")
flag.StringVar(&etcdPeerUrls, "etcd-peer-urls", "", "advertise peer urls")
flag.Parse()
if etcdPeerUrls == "" {
panic("must set -etcd-peer-urls")
}
if finfo, err := os.Stat(etcdRestoreDir); err != nil {
panic(err)
} else {
if !finfo.IsDir() {
panic(fmt.Errorf("%s is not a directory", etcdRestoreDir))
}
}
if !path.IsAbs(etcdctlPath) {
panic(fmt.Sprintf("etcdctl-path %s is not absolute", etcdctlPath))
}
if !path.IsAbs(etcdPath) {
panic(fmt.Sprintf("etcd-path %s is not absolute", etcdPath))
}
if err := restoreEtcd(); err != nil {
panic(err)
}
}
示例3: posixRel
// posixRel returns a relative path that is lexically equivalent to targpath when
// joined to basepath with an intervening separator.
//
// That is, Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
// On success, the returned path will always be relative to basepath,
// even if basepath and targpath share no elements.
// An error is returned if targpath can't be made relative to basepath or if
// knowing the current working directory would be necessary to compute it.
//
// Copy-pasted & slightly edited from Go's lib path/filepath/path.go .
//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
func posixRel(basepath, targpath string) (string, error) {
base := path.Clean(basepath)
targ := path.Clean(targpath)
if targ == base {
return ".", nil
}
if base == "." {
base = ""
}
if path.IsAbs(base) != path.IsAbs(targ) {
return "", errors.New("Rel: can't make " + targ + " relative to " + base)
}
// Position base[b0:bi] and targ[t0:ti] at the first differing elements.
bl := len(base)
tl := len(targ)
var b0, bi, t0, ti int
for {
for bi < bl && base[bi] != '/' {
bi++
}
for ti < tl && targ[ti] != '/' {
ti++
}
if targ[t0:ti] != base[b0:bi] {
break
}
if bi < bl {
bi++
}
if ti < tl {
ti++
}
b0 = bi
t0 = ti
}
if base[b0:bi] == ".." {
return "", errors.New("Rel: can't make " + targ + " relative to " + base)
}
if b0 != bl {
// Base elements left. Must go up before going down.
seps := strings.Count(base[b0:bl], string('/'))
size := 2 + seps*3
if tl != t0 {
size += 1 + tl - t0
}
buf := make([]byte, size)
n := copy(buf, "..")
for i := 0; i < seps; i++ {
buf[n] = '/'
copy(buf[n+1:], "..")
n += 3
}
if t0 != tl {
buf[n] = '/'
copy(buf[n+1:], targ[t0:])
}
return string(buf), nil
}
return targ[t0:], nil
}
示例4: discoverExecName
/*
Returns absolute path of executing file.
WARNING: this must be called before
changing the current directory
*/
func (be *BdsExec) discoverExecName() string {
if DEBUG {
log.Printf("Debug: discoverExecName (%s)\n", be.args[0])
}
f := be.args[0]
if path.IsAbs(f) {
return f
}
wd, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("discoverExecName: Getwd failed '%s'", err))
}
_, err = os.Stat(f)
if err == nil {
// Relative file exists
return path.Clean(path.Join(wd, f))
}
f2, err := exec.LookPath(f)
if err != nil {
panic(fmt.Sprintf("discoverExecName: Lookpath failed '%s'", err))
}
if path.IsAbs(f2) {
return f2
}
return path.Clean(path.Join(wd, f2))
}
示例5: main
func main() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
fmt.Println("working directory: ", wd)
fmt.Println("base: ", path.Base(wd))
fmt.Println("dir: ", path.Dir(wd))
fmt.Println("ext: ", path.Ext(wd+"howdy.test"))
fmt.Println("abs: ", path.IsAbs(wd))
fmt.Println("abs: ", path.IsAbs("howdy/../../"))
dirty := "////howdy////lots/of//slashes//yeah/"
fmt.Println("dirty: ", dirty)
fmt.Println("clean: ", path.Clean(dirty))
fmt.Println("joined: ", path.Join("one", "two", "three", "four"))
dir, file := path.Split(wd + "/lala.txt")
fmt.Println("dir: ", dir, " file: ", file)
// Hmmmm, I wonder if path works with URLs.
var base string
url := "http://example.com/test/file.txt"
base, file = path.Split(url)
fmt.Printf("Got base %v and file %v\n", base, file)
// Looks like the double slash after http: gets messed up by path.Dir.
fmt.Printf("Base is %v\nDir is %v\n", path.Base(url), path.Dir(url))
}
示例6: TestAbs
func TestAbs(t *testing.T) {
fmt.Println("Ejecutanto test de Abs...")
falseExpected := path.IsAbs("./some/path")
trueExpected := path.IsAbs("/dev/nul")
if falseExpected == true || trueExpected == false {
t.Error("Fallo en test de Abs.")
}
}
示例7: getBinlogSize
// getBinlogSize returns the total size of the binlog files
func (m *MySQLBinlogGrowth) getBinlogSize(binLog string, dataDir string) (size int64, err error) {
// Read the binlog.index file
// It contains a list of log files, one per line
file, err := os.Open(binLog)
if err != nil {
m.log.Warn(fmt.Sprintf("Cannot open index file %s : %s", binLog, err))
return
}
defer file.Close()
// Read the file line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fileName := scanner.Text()
// If the fileName is not an absolute path, then it's relative to the datadir directory
if !path.IsAbs(fileName) {
fileName = path.Join(dataDir, fileName)
}
fileSize, err := getFileSize(fileName)
if err != nil {
m.log.Warn(err)
}
size += fileSize
}
if scanErr := scanner.Err(); scanErr != nil {
m.log.Warn("There was an error reading the index file")
return
}
return
}
示例8: split
func split(pathStr string) ([]string, error) {
cleanPath := path.Clean(pathStr)
if !path.IsAbs(cleanPath) {
return nil, fmt.Errorf("split: %s is not an absolute path", pathStr)
}
return splitHelper(cleanPath), nil
}
示例9: zipPath
func zipPath(name string) (string, error) {
name = path.Clean(name)
if !path.IsAbs(name) {
return "", fmt.Errorf("stat: not an absolute path: %s", name)
}
return name[1:], nil // strip leading '/'
}
示例10: getBinlogPath
// getBinlogPath read and parse the my.cnf config file and returns the path to the binlog file and datadir.
func (m *MySQLBinlogGrowth) getBinlogPath() (binLog string, dataDir string) {
// read my.cnf config file
config, err := configparser.Read(m.myCnfPath)
if err != nil {
m.log.Error(err)
return
}
section, err := config.Section("mysqld")
if err != nil {
m.log.Error("mysqld section missing in ", m.myCnfPath)
return
}
binLog = section.ValueOf("log-bin")
if binLog == "" {
m.log.Error("log-bin value missing from ", m.myCnfPath)
return
}
dataDir = section.ValueOf("datadir")
if dataDir == "" {
m.log.Error("datadir value missing from ", m.myCnfPath)
return
}
// If the log-bin value is a relative path then it's based on datadir
if !path.IsAbs(binLog) {
binLog = path.Join(dataDir, binLog)
}
return
}
示例11: validatePath
// validatePath validates a single path, returning an error if the path is
// invalid. paths may not:
//
// 1. be absolute
// 2. contain '..' as an element
// 3. start with '..'
// 4. contain filenames larger than 255 characters
// 5. be longer than 4096 characters
func validatePath(targetPath string) error {
// TODO: somehow unify this with the similar api validation,
// validateVolumeSourcePath; the error semantics are just different enough
// from this that it was time-prohibitive trying to find the right
// refactoring to re-use.
if targetPath == "" {
return fmt.Errorf("invalid path: must not be empty: %q", targetPath)
}
if path.IsAbs(targetPath) {
return fmt.Errorf("invalid path: must be relative path: %s", targetPath)
}
if len(targetPath) > maxPathLength {
return fmt.Errorf("invalid path: must be less than %d characters", maxPathLength)
}
items := strings.Split(targetPath, string(os.PathSeparator))
for _, item := range items {
if item == ".." {
return fmt.Errorf("invalid path: must not contain '..': %s", targetPath)
}
if len(item) > maxFileNameLength {
return fmt.Errorf("invalid path: filenames must be less than %d characters", maxFileNameLength)
}
}
if strings.HasPrefix(items[0], "..") && len(items[0]) > 2 {
return fmt.Errorf("invalid path: must not start with '..': %s", targetPath)
}
return nil
}
示例12: getCommand
func getCommand(r *ReplYell) {
shellState := getShellState()
key := ""
switch len(r.Args) {
case 1:
key = r.Args[0]
case 0:
key = ""
fmt.Println("No key set: Getting pwd " + shellState.pwd)
default:
fmt.Println("Need one key - multiple key retrieval TODO")
return
}
if !path.IsAbs(key) {
key = path.Join(shellState.pwd, key)
} else {
key = path.Clean(key)
}
resp, err := shellState.kapi.Get(context.TODO(), key, nil)
if err != nil {
fmt.Println(err)
return
}
printResponseKey(resp)
}
示例13: findConfig
// findConfig returns the filename of the
// config. It searches parent directories
// if it can't find any of the config
// filenames in the current directory.
func findConfig(location string) string {
configFiles := configFilenames(location)
// Absolute path to config given
if len(location) > 0 && path.IsAbs(location) {
if _, err := os.Stat(location); err == nil {
return location
}
} else { // Relative config
configPath, _ := os.Getwd()
for {
for _, f := range configFiles {
// the root path is a `/` but others don't have a trailing `/`
filename := strings.TrimSuffix(configPath, "/") + "/" + f
if _, err := os.Stat(filename); err == nil {
return filename
}
}
// loop only if we haven't yet reached the root
if parentPath := path.Dir(configPath); len(parentPath) != len(configPath) {
configPath = parentPath
} else {
break
}
}
}
panic(StatusError{fmt.Errorf("No configuration found %v", configFiles), 78})
}
示例14: PopulateBoxes
func (bh *BoxHandler) PopulateBoxes(directories []string, port *int, hostname *string) {
log.Println("Populating boxes..")
absolutedirectories := []string{}
for _, d := range directories {
if !path.IsAbs(d) {
wd, _ := os.Getwd()
absolute := path.Clean(path.Join(wd, d))
absolutedirectories = append(absolutedirectories, absolute)
} else {
absolutedirectories = append(absolutedirectories, d)
}
}
bh.Directories = absolutedirectories
boxfiles := getBoxList(absolutedirectories)
boxdata := bh.getBoxData(boxfiles)
bh.createBoxes(boxdata, *port, hostname)
for _, boxinfo := range bh.Boxes {
for boxname, box := range boxinfo {
for _, version := range box.Versions {
for _, provider := range version.Providers {
log.Println("Found " + box.Username + "/" + boxname + "/" + version.Version + "/" + provider.Name)
}
}
}
}
}
示例15: abs
func abs(name string) (string, error) {
if path.IsAbs(name) {
return name, nil
}
wd, err := os.Getwd()
return path.Join(wd, name), err
}