Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
320ac339
提交
320ac339
authored
10月 15, 2014
作者:
Henry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
checkin github.com/mitchellh/go-homedir"
上级
c9236dd8
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
199 行增加
和
0 行删除
+199
-0
Godeps.json
Godeps/Godeps.json
+4
-0
LICENSE
...ps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE
+21
-0
README.md
.../_workspace/src/github.com/mitchellh/go-homedir/README.md
+14
-0
homedir.go
..._workspace/src/github.com/mitchellh/go-homedir/homedir.go
+83
-0
homedir_test.go
...space/src/github.com/mitchellh/go-homedir/homedir_test.go
+77
-0
没有找到文件。
Godeps/Godeps.json
浏览文件 @
320ac339
...
...
@@ -93,6 +93,10 @@
"Rev"
:
"1976046c2b0db0b668791b3e541d76a38b7c1af7"
},
{
"ImportPath"
:
"github.com/mitchellh/go-homedir"
,
"Rev"
:
"7d2d8c8a4e078ce3c58736ab521a40b37a504c52"
},
{
"ImportPath"
:
"github.com/op/go-logging"
,
"Rev"
:
"3df864a88c7f005e676db4f026a4fe2f14929be3"
},
...
...
Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE
0 → 100644
浏览文件 @
320ac339
The MIT License (MIT)
Copyright (c) 2013 Mitchell Hashimoto
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md
0 → 100644
浏览文件 @
320ac339
# go-homedir
This is a Go library for detecting the user's home directory without
the use of cgo, so the library can be used in cross-compilation environments.
Usage is incredibly simple, just call
`homedir.Dir()`
to get the home directory
for a user, and
`homedir.Expand()`
to expand the
`~`
in a path to the home
directory.
**Why not just use `os/user`?**
The built-in
`os/user`
package requires
cgo on Darwin systems. This means that any Go code that uses that package
cannot cross compile. But 99% of the time the use for
`os/user`
is just to
retrieve the home directory, which we can do for the current user without
cgo. This library does that, enabling cross-compilation.
Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go
0 → 100644
浏览文件 @
320ac339
package
homedir
import
(
"bytes"
"errors"
"os"
"os/exec"
"runtime"
"strings"
)
// Dir returns the home directory for the executing user.
//
// This uses an OS-specific method for discovering the home directory.
// An error is returned if a home directory cannot be detected.
func
Dir
()
(
string
,
error
)
{
if
runtime
.
GOOS
==
"windows"
{
return
dirWindows
()
}
// Unix-like system, so just assume Unix
return
dirUnix
()
}
// Expand expands the path to include the home directory if the path
// is prefixed with `~`. If it isn't prefixed with `~`, the path is
// returned as-is.
func
Expand
(
path
string
)
(
string
,
error
)
{
if
len
(
path
)
==
0
{
return
path
,
nil
}
if
path
[
0
]
!=
'~'
{
return
path
,
nil
}
if
len
(
path
)
>
1
&&
path
[
1
]
!=
'/'
&&
path
[
1
]
!=
'\\'
{
return
""
,
errors
.
New
(
"cannot expand user-specific home dir"
)
}
dir
,
err
:=
Dir
()
if
err
!=
nil
{
return
""
,
err
}
return
dir
+
path
[
1
:
],
nil
}
func
dirUnix
()
(
string
,
error
)
{
// First prefer the HOME environmental variable
if
home
:=
os
.
Getenv
(
"HOME"
);
home
!=
""
{
return
home
,
nil
}
// If that fails, try the shell
var
stdout
bytes
.
Buffer
cmd
:=
exec
.
Command
(
"sh"
,
"-c"
,
"eval echo ~$USER"
)
cmd
.
Stdout
=
&
stdout
if
err
:=
cmd
.
Run
();
err
!=
nil
{
return
""
,
err
}
result
:=
strings
.
TrimSpace
(
stdout
.
String
())
if
result
==
""
{
return
""
,
errors
.
New
(
"blank output when reading home directory"
)
}
return
result
,
nil
}
func
dirWindows
()
(
string
,
error
)
{
drive
:=
os
.
Getenv
(
"HOMEDRIVE"
)
path
:=
os
.
Getenv
(
"HOMEPATH"
)
home
:=
drive
+
path
if
drive
==
""
||
path
==
""
{
home
=
os
.
Getenv
(
"USERPROFILE"
)
}
if
home
==
""
{
return
""
,
errors
.
New
(
"HOMEDRIVE, HOMEPATH, and USERPROFILE are blank"
)
}
return
home
,
nil
}
Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go
0 → 100644
浏览文件 @
320ac339
package
homedir
import
(
"fmt"
"os/user"
"testing"
)
func
TestDir
(
t
*
testing
.
T
)
{
u
,
err
:=
user
.
Current
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
dir
,
err
:=
Dir
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
u
.
HomeDir
!=
dir
{
t
.
Fatalf
(
"%#v != %#v"
,
u
.
HomeDir
,
dir
)
}
}
func
TestExpand
(
t
*
testing
.
T
)
{
u
,
err
:=
user
.
Current
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
cases
:=
[]
struct
{
Input
string
Output
string
Err
bool
}{
{
"/foo"
,
"/foo"
,
false
,
},
{
"~/foo"
,
fmt
.
Sprintf
(
"%s/foo"
,
u
.
HomeDir
),
false
,
},
{
""
,
""
,
false
,
},
{
"~"
,
u
.
HomeDir
,
false
,
},
{
"~foo/foo"
,
""
,
true
,
},
}
for
_
,
tc
:=
range
cases
{
actual
,
err
:=
Expand
(
tc
.
Input
)
if
(
err
!=
nil
)
!=
tc
.
Err
{
t
.
Fatalf
(
"Input: %#v
\n\n
Err: %s"
,
tc
.
Input
,
err
)
}
if
actual
!=
tc
.
Output
{
t
.
Fatalf
(
"Input: %#v
\n\n
Output: %#v"
,
tc
.
Input
,
actual
)
}
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论