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


Golang user.Current函数代码示例

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


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

示例1: ConfigDirDock

// ConfigDirDock returns a full path to the dock config dir, according to the given user option.
//
func ConfigDirDock(dir string) string {
	if len(dir) > 0 {
		switch dir[0] {
		case '/':
			return dir // Full path, used as is.

		case '~':
			usr, e := user.Current()
			if e == nil {
				return usr.HomeDir + dir[1:] // Relative path to the homedir.
			}
		}

		current, e := os.Getwd()
		if e == nil {
			return filepath.Join(current, dir) // Relative path to the current dir.
		}
	}

	usr, e := user.Current()
	if e == nil {
		return filepath.Join(usr.HomeDir, ".config", ConfigDirBaseName) // Default dock config path in .config.
	}

	return ""
}
开发者ID:sqp,项目名称:godock,代码行数:28,代码来源:cdglobal.go

示例2: TenetCfgPathRecusive

// TenetCfgPathRecusive looks for a config file at cfgPath. If the config
// file name is equal to DefaultTenetCfgPath, the func recursively searches the
// parent directory until a file with that name is found. In the case that
// none is found "" is retuned.
func TenetCfgPathRecusive(cfgPath string) (string, error) {
	var err error
	cfgPath, err = filepath.Abs(cfgPath)
	if err != nil {
		return "", err
	}

	if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
		dir, file := path.Split(cfgPath)
		if file == DefaultTenetCfgPath {
			if dir == "/" {
				// we've reached the end of the line. Fall back to default:
				usr, err := user.Current()
				if err != nil {
					return "", err
				}

				lHome, err := util.LingoHome()
				if err != nil {
					return "", errors.Trace(err)
				}
				defaultTenets := path.Join(usr.HomeDir, lHome, DefaultTenetCfgPath)
				if _, err := os.Stat(defaultTenets); err != nil {
					return "", err
				}
				return defaultTenets, nil
			}
			parent := path.Dir(path.Dir(dir))
			return TenetCfgPathRecusive(parent + "/" + DefaultTenetCfgPath)
		}
		return "", err
	}
	return cfgPath, nil
}
开发者ID:howbazaar,项目名称:lingo,代码行数:38,代码来源:common.go

示例3: TestSubstitueUser

func TestSubstitueUser(t *testing.T) {
	usr, err := user.Current()
	if err != nil {
		t.Logf("SKIPPING TEST: unexpected error: %v", err)
		return
	}
	tests := []struct {
		input     string
		expected  string
		expectErr bool
	}{
		{input: "~/foo", expected: path.Join(os.Getenv("HOME"), "foo")},
		{input: "~" + usr.Username + "/bar", expected: usr.HomeDir + "/bar"},
		{input: "/foo/bar", expected: "/foo/bar"},
		{input: "~doesntexit/bar", expectErr: true},
	}
	for _, test := range tests {
		output, err := substituteUserHome(test.input)
		if test.expectErr {
			if err == nil {
				t.Error("unexpected non-error")
			}
			continue
		}
		if err != nil {
			t.Errorf("unexpected error: %v", err)
		}
		if output != test.expected {
			t.Errorf("expected: %s, saw: %s", test.expected, output)
		}
	}
}
开发者ID:ravihansa3000,项目名称:kubernetes,代码行数:32,代码来源:factory_test.go

示例4: getHome

func getHome() string {
	usr, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	return usr.HomeDir
}
开发者ID:pschyska,项目名称:garvest,代码行数:7,代码来源:harvest.go

示例5: buildUser

func buildUser() string {
	u, err := user.Current()
	if err != nil {
		return "unknown-user"
	}
	return strings.Replace(u.Username, " ", "-", -1)
}
开发者ID:WeavingCode,项目名称:syncthing,代码行数:7,代码来源:build.go

示例6: cacheDirectoryAbsolutePath

func cacheDirectoryAbsolutePath() (path string, err error) {
	me, err := user.Current()
	if err != nil {
		return "", nil
	}
	return filepath.Join(me.HomeDir, SkeletonCacheDirname), nil
}
开发者ID:jkotrlik,项目名称:salsaflow,代码行数:7,代码来源:skeleton.go

示例7: connect

func connect(username, host string, authMethod ssh.AuthMethod, timeout time.Duration) (*Client, error) {
	if username == "" {
		user, err := user.Current()
		if err != nil {
			return nil, fmt.Errorf("Username wasn't specified and couldn't get current user: %v", err)
		}

		username = user.Username
	}

	config := &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{authMethod},
	}

	host = addPortToHost(host)

	conn, err := net.DialTimeout("tcp", host, timeout)
	if err != nil {
		return nil, err
	}
	sshConn, chans, reqs, err := ssh.NewClientConn(conn, host, config)
	if err != nil {
		return nil, err
	}
	client := ssh.NewClient(sshConn, chans, reqs)

	c := &Client{SSHClient: client}
	return c, nil
}
开发者ID:bvnarayan,项目名称:simplessh,代码行数:30,代码来源:simplessh.go

示例8: tryConfigFilePath

func tryConfigFilePath(configFilePath string) (string, error) {
	if configFilePath != "" {
		configFilePath, _ = filepath.Abs(configFilePath)
		if _, err := os.Stat(configFilePath); err == nil {
			log.Println("found log file at " + configFilePath)
			return configFilePath, nil
		} else {
			return "", err
		}
	} else {
		posibleConfigFiles := []string{}
		if p, e := os.Getwd(); e == nil {
			// loop for lighthouse.json from current working directory
			posibleConfigFiles = append(posibleConfigFiles, p+"/lighthouse.json")
		}
		if u, e := user.Current(); e == nil {
			// loop for lighthouse.json from current user's home directory
			posibleConfigFiles = append(posibleConfigFiles, u.HomeDir+"/lighthouse.json")
		}
		for _, path := range posibleConfigFiles {
			if _, err := os.Stat(path); err == nil {
				log.Println("found configuration file at " + path)
				return path, nil
			}
		}
		log.Println("no configuration file found. use defalut config.")
		// not valid config file found
		return "", nil
	}
}
开发者ID:gongshw,项目名称:lighthouse,代码行数:30,代码来源:conf.go

示例9: init

func init() {
	var u *user.User
	if u, _ = user.Current(); u == nil {
		panic("user.Current is nil")
	}
	configFile = path.Join(u.HomeDir, ".fish")
}
开发者ID:kajali,项目名称:fish,代码行数:7,代码来源:config.go

示例10: NewGuppyConfig

// NewGuppyConfig will return a config based on a few things;
// It first creates some default settings and merges/overwrites that with the root config (~/.guppy)
// It then looks for a project specific config (current working directory) and will merge/overwrite
// that previous result with the project specific config settings.
func NewGuppyConfig() *GuppyConfig {
	cwd, err := os.Getwd()
	if err != nil {
		return nil
	}

	gc := &GuppyConfig{
		cwd,
		"/guppy_modules",
		"localhost:13379",
		"localhost:13379",
		true,
	}

	user, err := user.Current()
	if err == nil {
		err = gc.load(user.HomeDir)

		if err != nil {
			glog.Error("Failed to load guppy root config:", err)
		}
	}

	if cwd != user.HomeDir {
		err = gc.load(cwd)
		if err != nil {
			glog.Error("Failed to load guppy project config:", err)
		}
	}

	return gc
}
开发者ID:b1lly,项目名称:guppy,代码行数:36,代码来源:config.go

示例11: CleanPath

func CleanPath(path string) string {
	result := ""

	if path == "" {
		return ""
	}

	usr, err := user.Current()
	if err != nil {
		log.Fatalln(err)
	}

	if len(path) > 1 && path[:2] == "~/" {
		dir := usr.HomeDir + "/"
		result = strings.Replace(path, "~/", dir, 1)
	} else {
		result = path
	}

	absResult, absErr := filepath.Abs(result)
	if absErr != nil {
		log.Fatalln(absErr)
	}

	cleanResult := filepath.Clean(absResult)

	return cleanResult
}
开发者ID:thacker99,项目名称:textql,代码行数:28,代码来源:file_helpers.go

示例12: secretsFile

// secretsFile creates a file path/filename
// It returns the app client secrets path/filename.
func secretsFile() (string, error) {
	usr, err := user.Current()
	if err != nil {
		return "", err
	}
	return filepath.Join(usr.HomeDir, ".credentials", "client_secret", "gogas_client_secret.json"), err
}
开发者ID:tnum5,项目名称:gogas,代码行数:9,代码来源:auth.go

示例13: init

func init() {
	usr, err := user.Current()
	if err != nil {
		panic(err)
	}
	dbPath = filepath.Join(usr.HomeDir, dbPath)
}
开发者ID:xqbumu,项目名称:learn,代码行数:7,代码来源:00_write_random_data.go

示例14: loadCache

func (b *Builder) loadCache() (err error) {
	b.cache = map[string]string{}

	usr, err := user.Current()
	if err != nil {
		return fmt.Errorf("unable to get current user: %s", err)
	}

	cacheFilename := fmt.Sprintf("%s%c%s", usr.HomeDir, filepath.Separator, ".dockrampcache")
	cacheFile, err := os.Open(cacheFilename)
	if os.IsNotExist(err) {
		// No cache file exists to load.
		return nil
	}
	if err != nil {
		return fmt.Errorf("unable to open cache file: %s", err)
	}
	defer func() {
		if closeErr := cacheFile.Close(); err == nil {
			err = closeErr
		}
	}()

	if err := json.NewDecoder(cacheFile).Decode(&b.cache); err != nil {
		return fmt.Errorf("unable to decode build cache: %s", err)
	}

	return nil
}
开发者ID:dmcgowan,项目名称:golem,代码行数:29,代码来源:cache.go

示例15: NewGDriveFileSystem

func NewGDriveFileSystem(clientId string, clientSecret string) *GDriveFileSystem {
	u, err := user.Current()

	tokenFile := u.HomeDir + "/.gdrive_token"

	if *tokenFileFlag != "" {
		tokenFile = *tokenFileFlag
	}

	config.TokenCache = oauth.CacheFile(tokenFile)
	config.ClientId = clientId
	config.ClientSecret = clientSecret

	transport := &oauth.Transport{
		Config:    config,
		Transport: &loggingTransport{http.DefaultTransport},
	}

	obtainToken(transport)

	client, err := drive.New(transport.Client())
	if err != nil {
		log.Errorf("An error occurred creating Drive client: %v\n", err)
		panic(-3)
	}

	fs := &GDriveFileSystem{
		client:    client,
		transport: transport,
		cache:     gocache.New(5*time.Minute, 30*time.Second),
	}
	return fs
}
开发者ID:santeriv,项目名称:gdrive-webdav,代码行数:33,代码来源:gdrive.go


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