Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
7226fc72
H
hub
项目概览
github
/
hub
11 个月 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7226fc72
编写于
1月 22, 2016
作者:
M
Mislav Marohnić
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1082 from github/go-homedir
Avoid "user: Current not implemented" error
上级
19017f93
f6bc4d73
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
296 addition
and
17 deletion
+296
-17
github/client.go
github/client.go
+14
-5
github/config.go
github/config.go
+3
-12
vendor/github.com/mitchellh/go-homedir/LICENSE
vendor/github.com/mitchellh/go-homedir/LICENSE
+21
-0
vendor/github.com/mitchellh/go-homedir/README.md
vendor/github.com/mitchellh/go-homedir/README.md
+14
-0
vendor/github.com/mitchellh/go-homedir/homedir.go
vendor/github.com/mitchellh/go-homedir/homedir.go
+132
-0
vendor/github.com/mitchellh/go-homedir/homedir_test.go
vendor/github.com/mitchellh/go-homedir/homedir_test.go
+112
-0
未找到文件。
github/client.go
浏览文件 @
7226fc72
...
...
@@ -5,7 +5,7 @@ import (
"io"
"net/url"
"os"
"os/
user
"
"os/
exec
"
"strings"
"github.com/octokit/go-octokit/octokit"
...
...
@@ -697,12 +697,21 @@ func warnExistenceOfRepo(project *Project, ee error) (err error) {
}
func
authTokenNote
(
num
int
)
(
string
,
error
)
{
u
,
err
:=
user
.
Current
()
if
err
!=
nil
{
return
""
,
err
n
:=
os
.
Getenv
(
"USER"
)
if
n
==
""
{
n
=
os
.
Getenv
(
"USERNAME"
)
}
if
n
==
""
{
whoami
:=
exec
.
Command
(
"whoami"
)
whoamiOut
,
err
:=
whoami
.
Output
()
if
err
!=
nil
{
return
""
,
err
}
n
=
strings
.
TrimSpace
(
string
(
whoamiOut
))
}
n
:=
u
.
Username
h
,
err
:=
os
.
Hostname
()
if
err
!=
nil
{
return
""
,
err
...
...
github/config.go
浏览文件 @
7226fc72
...
...
@@ -5,29 +5,20 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
"github.com/howeyc/gopass"
"github.com/mitchellh/go-homedir"
)
var
defaultConfigsFile
string
func
init
()
{
homeDir
:=
os
.
Getenv
(
"HOME"
)
if
homeDir
==
""
{
if
u
,
err
:=
user
.
Current
();
err
==
nil
{
homeDir
=
u
.
HomeDir
}
}
if
homeDir
==
""
{
utils
.
Check
(
fmt
.
Errorf
(
"Can't get current user's home dir"
))
}
homeDir
,
err
:=
homedir
.
Dir
()
utils
.
Check
(
err
)
defaultConfigsFile
=
filepath
.
Join
(
homeDir
,
".config"
,
"hub"
)
}
...
...
vendor/github.com/mitchellh/go-homedir/LICENSE
0 → 100644
浏览文件 @
7226fc72
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.
vendor/github.com/mitchellh/go-homedir/README.md
0 → 100644
浏览文件 @
7226fc72
# 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.
vendor/github.com/mitchellh/go-homedir/homedir.go
0 → 100644
浏览文件 @
7226fc72
package
homedir
import
(
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
)
// DisableCache will disable caching of the home directory. Caching is enabled
// by default.
var
DisableCache
bool
var
homedirCache
string
var
cacheLock
sync
.
RWMutex
// 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
!
DisableCache
{
cacheLock
.
RLock
()
cached
:=
homedirCache
cacheLock
.
RUnlock
()
if
cached
!=
""
{
return
cached
,
nil
}
}
cacheLock
.
Lock
()
defer
cacheLock
.
Unlock
()
var
result
string
var
err
error
if
runtime
.
GOOS
==
"windows"
{
result
,
err
=
dirWindows
()
}
else
{
// Unix-like system, so just assume Unix
result
,
err
=
dirUnix
()
}
if
err
!=
nil
{
return
""
,
err
}
homedirCache
=
result
return
result
,
nil
}
// 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
filepath
.
Join
(
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 getent
var
stdout
bytes
.
Buffer
cmd
:=
exec
.
Command
(
"getent"
,
"passwd"
,
strconv
.
Itoa
(
os
.
Getuid
()))
cmd
.
Stdout
=
&
stdout
if
err
:=
cmd
.
Run
();
err
!=
nil
{
// If "getent" is missing, ignore it
if
err
!=
exec
.
ErrNotFound
{
return
""
,
err
}
}
else
{
if
passwd
:=
strings
.
TrimSpace
(
stdout
.
String
());
passwd
!=
""
{
// username:password:uid:gid:gecos:home:shell
passwdParts
:=
strings
.
SplitN
(
passwd
,
":"
,
7
)
if
len
(
passwdParts
)
>
5
{
return
passwdParts
[
5
],
nil
}
}
}
// If all else fails, try the shell
stdout
.
Reset
()
cmd
=
exec
.
Command
(
"sh"
,
"-c"
,
"cd && pwd"
)
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
}
vendor/github.com/mitchellh/go-homedir/homedir_test.go
0 → 100644
浏览文件 @
7226fc72
package
homedir
import
(
"fmt"
"os"
"os/user"
"testing"
)
func
patchEnv
(
key
,
value
string
)
func
()
{
bck
:=
os
.
Getenv
(
key
)
deferFunc
:=
func
()
{
os
.
Setenv
(
key
,
bck
)
}
os
.
Setenv
(
key
,
value
)
return
deferFunc
}
func
BenchmarkDir
(
b
*
testing
.
B
)
{
// We do this for any "warmups"
for
i
:=
0
;
i
<
10
;
i
++
{
Dir
()
}
b
.
ResetTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
Dir
()
}
}
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
)
}
}
DisableCache
=
true
defer
func
()
{
DisableCache
=
false
}()
defer
patchEnv
(
"HOME"
,
"/custom/path/"
)()
expected
:=
"/custom/path/foo/bar"
actual
,
err
:=
Expand
(
"~/foo/bar"
)
if
err
!=
nil
{
t
.
Errorf
(
"No error is expected, got: %v"
,
err
)
}
else
if
actual
!=
"/custom/path/foo/bar"
{
t
.
Errorf
(
"Expected: %v; actual: %v"
,
expected
,
actual
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录