Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
b573a8f3
H
hub
项目概览
github
/
hub
10 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
b573a8f3
编写于
12月 19, 2013
作者:
J
Jingwen Owen Ou
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'autoupdate'
上级
9bf17e1f
6faecd18
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
245 addition
and
135 deletion
+245
-135
.goxc.json
.goxc.json
+0
-1
commands/runner.go
commands/runner.go
+6
-1
commands/update.go
commands/update.go
+2
-132
commands/updater.go
commands/updater.go
+236
-0
commands/version.go
commands/version.go
+1
-1
未找到文件。
.goxc.json
浏览文件 @
b573a8f3
...
...
@@ -6,7 +6,6 @@
"ResourcesExclude"
:
"*.go"
,
"MainDirsExclude"
:
"Godeps"
,
"PackageVersion"
:
"0.26.0"
,
"PrereleaseInfo"
:
"snapshot"
,
"Verbosity"
:
"v"
,
"TaskSettings"
:
{
"downloads-page"
:
{
...
...
commands/runner.go
浏览文件 @
b573a8f3
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/jingweno/gh/cmd"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/utils"
"github.com/kballard/go-shellquote"
"os/exec"
"syscall"
...
...
@@ -44,6 +45,10 @@ func (r *Runner) Execute() ExecError {
return
newExecError
(
nil
)
}
updater
:=
NewUpdater
()
err
:=
updater
.
PromptForUpdate
()
utils
.
Check
(
err
)
expandAlias
(
args
)
slurpGlobalFlags
(
args
)
...
...
@@ -78,7 +83,7 @@ func (r *Runner) Execute() ExecError {
}
}
err
:
=
git
.
Spawn
(
args
.
Command
,
args
.
Params
...
)
err
=
git
.
Spawn
(
args
.
Command
,
args
.
Params
...
)
return
newExecError
(
err
)
}
...
...
commands/update.go
浏览文件 @
b573a8f3
package
commands
import
(
"archive/zip"
"fmt"
updater
"github.com/inconshreveable/go-update"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)
var
cmdUpdate
=
&
Command
{
...
...
@@ -27,128 +17,8 @@ Examples:
}
func
update
(
cmd
*
Command
,
args
*
Args
)
{
err
:=
doUpdate
()
updater
:=
NewUpdater
()
err
:=
updater
.
Update
()
utils
.
Check
(
err
)
os
.
Exit
(
0
)
}
func
doUpdate
()
(
err
error
)
{
client
:=
github
.
NewClient
(
github
.
GitHubHost
)
releases
,
err
:=
client
.
Releases
(
github
.
NewProject
(
"jingweno"
,
"gh"
,
github
.
GitHubHost
))
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Error fetching releases: %s"
,
err
)
return
}
latestRelease
:=
releases
[
0
]
tagName
:=
latestRelease
.
TagName
version
:=
strings
.
TrimPrefix
(
tagName
,
"v"
)
if
version
==
Version
{
err
=
fmt
.
Errorf
(
"You're already on the latest version: %s"
,
Version
)
return
}
fmt
.
Printf
(
"Updating gh to release %s...
\n
"
,
version
)
downloadURL
:=
fmt
.
Sprintf
(
"https://github.com/jingweno/gh/releases/download/%s/gh_%s-snapshot_%s_%s.zip"
,
tagName
,
version
,
runtime
.
GOOS
,
runtime
.
GOARCH
)
path
,
err
:=
downloadFile
(
downloadURL
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Can't download update from %s to %s"
,
downloadURL
,
path
)
return
}
exec
,
err
:=
unzipExecutable
(
path
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Can't unzip gh executable: %s"
,
err
)
return
}
err
,
_
=
updater
.
FromFile
(
exec
)
if
err
==
nil
{
fmt
.
Println
(
"Done!"
)
}
return
}
func
unzipExecutable
(
path
string
)
(
exec
string
,
err
error
)
{
rc
,
err
:=
zip
.
OpenReader
(
path
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Can't open zip file %s: %s"
,
path
,
err
)
return
}
defer
rc
.
Close
()
for
_
,
file
:=
range
rc
.
File
{
if
!
strings
.
HasPrefix
(
file
.
Name
,
"gh"
)
{
continue
}
dir
:=
filepath
.
Dir
(
path
)
exec
,
err
=
unzipFile
(
file
,
dir
)
break
}
if
exec
==
""
&&
err
==
nil
{
err
=
fmt
.
Errorf
(
"No gh executable is found in %s"
,
path
)
}
return
}
func
unzipFile
(
file
*
zip
.
File
,
to
string
)
(
exec
string
,
err
error
)
{
frc
,
err
:=
file
.
Open
()
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Can't open zip entry %s when reading: %s"
,
file
.
Name
,
err
)
return
}
defer
frc
.
Close
()
dest
:=
filepath
.
Join
(
to
,
filepath
.
Base
(
file
.
Name
))
f
,
err
:=
os
.
Create
(
dest
)
if
err
!=
nil
{
return
}
defer
f
.
Close
()
copied
,
err
:=
io
.
Copy
(
f
,
frc
)
if
err
!=
nil
{
return
}
if
uint32
(
copied
)
!=
file
.
UncompressedSize
{
err
=
fmt
.
Errorf
(
"Zip entry %s is corrupted"
,
file
.
Name
)
return
}
exec
=
f
.
Name
()
return
}
func
downloadFile
(
url
string
)
(
path
string
,
err
error
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"gh-update"
)
if
err
!=
nil
{
return
}
file
,
err
:=
os
.
Create
(
filepath
.
Join
(
dir
,
filepath
.
Base
(
url
)))
if
err
!=
nil
{
return
}
defer
file
.
Close
()
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
return
}
defer
resp
.
Body
.
Close
()
_
,
err
=
io
.
Copy
(
file
,
resp
.
Body
)
if
err
!=
nil
{
return
}
path
=
file
.
Name
()
return
}
commands/updater.go
0 → 100644
浏览文件 @
b573a8f3
package
commands
import
(
"archive/zip"
"fmt"
goupdate
"github.com/inconshreveable/go-update"
"github.com/jingweno/gh/github"
"github.com/jingweno/go-octokit/octokit"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
var
(
updateTimestampPath
=
filepath
.
Join
(
os
.
Getenv
(
"HOME"
),
".config"
,
"gh-update"
)
)
func
NewUpdater
()
*
Updater
{
version
:=
os
.
Getenv
(
"GH_VERSION"
)
if
version
==
""
{
version
=
Version
}
return
&
Updater
{
Host
:
github
.
GitHubHost
,
CurrentVersion
:
version
}
}
type
Updater
struct
{
Host
string
CurrentVersion
string
}
func
(
updater
*
Updater
)
timeToUpdate
()
bool
{
if
updater
.
CurrentVersion
==
"dev"
||
readTime
(
updateTimestampPath
)
.
After
(
time
.
Now
())
{
return
false
}
// the next update is in about 14 days
wait
:=
13
*
24
*
time
.
Hour
+
randDuration
(
24
*
time
.
Hour
)
return
writeTime
(
updateTimestampPath
,
time
.
Now
()
.
Add
(
wait
))
}
func
(
updater
*
Updater
)
latestRelease
()
(
r
*
octokit
.
Release
)
{
client
:=
github
.
NewClient
(
updater
.
Host
)
releases
,
err
:=
client
.
Releases
(
github
.
NewProject
(
"jingweno"
,
"gh"
,
updater
.
Host
))
if
err
!=
nil
{
return
}
if
len
(
releases
)
>
0
{
r
=
&
releases
[
0
]
}
return
}
func
(
updater
*
Updater
)
latestReleaseNameAndVersion
()
(
name
,
version
string
)
{
if
latestRelease
:=
updater
.
latestRelease
();
latestRelease
!=
nil
{
name
=
latestRelease
.
TagName
version
=
strings
.
TrimPrefix
(
name
,
"v"
)
}
return
}
func
(
updater
*
Updater
)
PromptForUpdate
()
(
err
error
)
{
if
!
updater
.
timeToUpdate
()
{
return
}
releaseName
,
version
:=
updater
.
latestReleaseNameAndVersion
()
if
version
!=
""
&&
version
!=
updater
.
CurrentVersion
{
fmt
.
Println
(
"There is a newer version of gh available."
)
fmt
.
Print
(
"Type Y to update: "
)
var
confirm
string
fmt
.
Scan
(
&
confirm
)
if
confirm
==
"Y"
||
confirm
==
"y"
{
err
=
updater
.
updateTo
(
releaseName
,
version
)
}
}
return
}
func
(
updater
*
Updater
)
Update
()
(
err
error
)
{
releaseName
,
version
:=
updater
.
latestReleaseNameAndVersion
()
if
version
==
""
{
fmt
.
Println
(
"There is no newer version of gh available."
)
return
}
if
version
==
updater
.
CurrentVersion
{
fmt
.
Printf
(
"You're already on the latest version: %s
\n
"
,
version
)
}
else
{
err
=
updater
.
updateTo
(
releaseName
,
version
)
}
return
}
func
(
updater
*
Updater
)
updateTo
(
releaseName
,
version
string
)
(
err
error
)
{
fmt
.
Printf
(
"Updating gh to %s...
\n
"
,
version
)
downloadURL
:=
fmt
.
Sprintf
(
"https://%s/jingweno/gh/releases/download/%s/gh_%s_%s_%s.zip"
,
updater
.
Host
,
releaseName
,
version
,
runtime
.
GOOS
,
runtime
.
GOARCH
)
path
,
err
:=
downloadFile
(
downloadURL
)
if
err
!=
nil
{
return
}
exec
,
err
:=
unzipExecutable
(
path
)
if
err
!=
nil
{
return
}
err
,
_
=
goupdate
.
FromFile
(
exec
)
if
err
==
nil
{
fmt
.
Println
(
"Done!"
)
}
return
}
func
unzipExecutable
(
path
string
)
(
exec
string
,
err
error
)
{
rc
,
err
:=
zip
.
OpenReader
(
path
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Can't open zip file %s: %s"
,
path
,
err
)
return
}
defer
rc
.
Close
()
for
_
,
file
:=
range
rc
.
File
{
if
!
strings
.
HasPrefix
(
file
.
Name
,
"gh"
)
{
continue
}
dir
:=
filepath
.
Dir
(
path
)
exec
,
err
=
unzipFile
(
file
,
dir
)
break
}
if
exec
==
""
&&
err
==
nil
{
err
=
fmt
.
Errorf
(
"No gh executable is found in %s"
,
path
)
}
return
}
func
unzipFile
(
file
*
zip
.
File
,
to
string
)
(
exec
string
,
err
error
)
{
frc
,
err
:=
file
.
Open
()
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Can't open zip entry %s when reading: %s"
,
file
.
Name
,
err
)
return
}
defer
frc
.
Close
()
dest
:=
filepath
.
Join
(
to
,
filepath
.
Base
(
file
.
Name
))
f
,
err
:=
os
.
Create
(
dest
)
if
err
!=
nil
{
return
}
defer
f
.
Close
()
copied
,
err
:=
io
.
Copy
(
f
,
frc
)
if
err
!=
nil
{
return
}
if
uint32
(
copied
)
!=
file
.
UncompressedSize
{
err
=
fmt
.
Errorf
(
"Zip entry %s is corrupted"
,
file
.
Name
)
return
}
exec
=
f
.
Name
()
return
}
func
downloadFile
(
url
string
)
(
path
string
,
err
error
)
{
dir
,
err
:=
ioutil
.
TempDir
(
""
,
"gh-update"
)
if
err
!=
nil
{
return
}
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
return
}
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
>=
300
||
resp
.
StatusCode
<
200
{
err
=
fmt
.
Errorf
(
"Can't download %s: %d"
,
url
,
resp
.
StatusCode
)
return
}
file
,
err
:=
os
.
Create
(
filepath
.
Join
(
dir
,
filepath
.
Base
(
url
)))
if
err
!=
nil
{
return
}
defer
file
.
Close
()
_
,
err
=
io
.
Copy
(
file
,
resp
.
Body
)
if
err
!=
nil
{
return
}
path
=
file
.
Name
()
return
}
func
randDuration
(
n
time
.
Duration
)
time
.
Duration
{
return
time
.
Duration
(
rand
.
Int63n
(
int64
(
n
)))
}
func
readTime
(
path
string
)
time
.
Time
{
p
,
err
:=
ioutil
.
ReadFile
(
path
)
if
os
.
IsNotExist
(
err
)
{
return
time
.
Time
{}
}
if
err
!=
nil
{
return
time
.
Now
()
.
Add
(
1000
*
time
.
Hour
)
}
t
,
err
:=
time
.
Parse
(
time
.
RFC3339
,
strings
.
TrimSpace
(
string
(
p
)))
if
err
!=
nil
{
return
time
.
Now
()
.
Add
(
1000
*
time
.
Hour
)
}
return
t
}
func
writeTime
(
path
string
,
t
time
.
Time
)
bool
{
return
ioutil
.
WriteFile
(
path
,
[]
byte
(
t
.
Format
(
time
.
RFC3339
)),
0644
)
==
nil
}
commands/version.go
浏览文件 @
b573a8f3
...
...
@@ -7,7 +7,7 @@ import (
"os"
)
const
Version
=
"0.2
6
.0"
const
Version
=
"0.2
7
.0"
var
cmdVersion
=
&
Command
{
Run
:
runVersion
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录