本文整理汇总了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 ""
}
示例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
}
示例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)
}
}
}
示例4: getHome
func getHome() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr.HomeDir
}
示例5: buildUser
func buildUser() string {
u, err := user.Current()
if err != nil {
return "unknown-user"
}
return strings.Replace(u.Username, " ", "-", -1)
}
示例6: cacheDirectoryAbsolutePath
func cacheDirectoryAbsolutePath() (path string, err error) {
me, err := user.Current()
if err != nil {
return "", nil
}
return filepath.Join(me.HomeDir, SkeletonCacheDirname), nil
}
示例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
}
示例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
}
}
示例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")
}
示例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
}
示例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
}
示例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
}
示例13: init
func init() {
usr, err := user.Current()
if err != nil {
panic(err)
}
dbPath = filepath.Join(usr.HomeDir, dbPath)
}
示例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
}
示例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
}