Simplify flag handling in `hub release` commands

This commit is contained in:
Mislav Marohnić 2016-01-31 22:45:01 +11:00
Родитель 15ee075ff7
Коммит 1bb1b025fe
3 изменённых файлов: 112 добавлений и 25 удалений

Просмотреть файл

@ -66,6 +66,25 @@ func (c *Command) parseArguments(args *Args) (err error) {
return
}
func (c *Command) FlagPassed(name string) bool {
found := false
c.Flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func (c *Command) Arg(idx int) string {
args := c.Flag.Args()
if idx < len(args) {
return args[idx]
} else {
return ""
}
}
func (c *Command) Use(subCommand *Command) {
if c.subCommands == nil {
c.subCommands = make(map[string]*Command)

Просмотреть файл

@ -40,10 +40,7 @@ With '--include-drafs', include draft releases in the listing.
* _edit_:
Edit the GitHub release for the specified <TAG> name. Accepts the same
options as _create_ command, with addition of:
With '--publish', set the "draft" property to false.
With '--no-prerelease', set the "prerelease" property to false.
options as _create_ command. Publish a draft with '--draft=false'.
## Options:
-d, --draft
@ -94,9 +91,7 @@ hub(1), git-tag(1)
flagReleaseIncludeDrafts,
flagReleaseShowDownloads,
flagReleaseDraft,
flagReleaseNoDraft,
flagReleasePrerelease,
flagReleaseNoPrerelease bool
flagReleasePrerelease bool
flagReleaseMessage,
flagReleaseFile,
@ -119,8 +114,6 @@ func init() {
cmdEditRelease.Flag.BoolVarP(&flagReleaseDraft, "draft", "d", false, "DRAFT")
cmdEditRelease.Flag.BoolVarP(&flagReleasePrerelease, "prerelease", "p", false, "PRERELEASE")
cmdEditRelease.Flag.BoolVarP(&flagReleaseNoDraft, "publish", "", false, "DRAFT")
cmdEditRelease.Flag.BoolVarP(&flagReleaseNoPrerelease, "no-prerelease", "", false, "PRERELEASE")
cmdEditRelease.Flag.VarP(&flagReleaseAssets, "attach", "a", "ATTACH_ASSETS")
cmdEditRelease.Flag.StringVarP(&flagReleaseMessage, "message", "m", "", "MESSAGE")
cmdEditRelease.Flag.StringVarP(&flagReleaseFile, "file", "f", "", "FILE")
@ -158,7 +151,7 @@ func listReleases(cmd *Command, args *Args) {
}
func showRelease(cmd *Command, args *Args) {
tagName := args.LastParam()
tagName := cmd.Arg(0)
if tagName == "" {
utils.Check(fmt.Errorf("Missing argument TAG"))
}
@ -199,7 +192,7 @@ func showRelease(cmd *Command, args *Args) {
}
func createRelease(cmd *Command, args *Args) {
tagName := args.LastParam()
tagName := cmd.Arg(0)
if tagName == "" {
utils.Check(fmt.Errorf("Missing argument TAG"))
return
@ -224,9 +217,9 @@ func createRelease(cmd *Command, args *Args) {
var body string
var editor *github.Editor
if flagReleaseMessage != "" {
if cmd.FlagPassed("message") {
title, body = readMsg(flagReleaseMessage)
} else if flagReleaseFile != "" {
} else if cmd.FlagPassed("file") {
title, body, err = readMsgFromFile(flagReleaseMessage)
utils.Check(err)
} else {
@ -274,7 +267,7 @@ func createRelease(cmd *Command, args *Args) {
}
func editRelease(cmd *Command, args *Args) {
tagName := args.LastParam()
tagName := cmd.Arg(0)
if tagName == "" {
utils.Check(fmt.Errorf("Missing argument TAG"))
return
@ -294,30 +287,26 @@ func editRelease(cmd *Command, args *Args) {
params := map[string]interface{}{}
commitish := release.TargetCommitish
if flagReleaseCommitish != "" {
if cmd.FlagPassed("commitish") {
params["target_commitish"] = flagReleaseCommitish
commitish = flagReleaseCommitish
}
if flagReleaseDraft {
params["draft"] = true
} else if flagReleaseNoDraft {
params["draft"] = false
if cmd.FlagPassed("draft") {
params["draft"] = flagReleaseDraft
}
if flagReleasePrerelease {
params["prerelease"] = true
} else if flagReleaseNoPrerelease {
params["prerelease"] = false
if cmd.FlagPassed("prerelease") {
params["prerelease"] = flagReleasePrerelease
}
var title string
var body string
var editor *github.Editor
if flagReleaseMessage != "" {
if cmd.FlagPassed("message") {
title, body = readMsg(flagReleaseMessage)
} else if flagReleaseFile != "" {
} else if cmd.FlagPassed("file") {
title, body, err = readMsgFromFile(flagReleaseMessage)
utils.Check(err)
} else {

Просмотреть файл

@ -142,3 +142,82 @@ MARKDOWN
https://github.com/mislav/will_paginate/archive/v1.2.0.zip
https://github.com/mislav/will_paginate/archive/v1.2.0.tar.gz\n
"""
Scenario: Create a release
Given the GitHub API server:
"""
post('/repos/mislav/will_paginate/releases') {
assert :draft => true,
:tag_name => "v1.2.0",
:target_commitish => "master",
:name => "will_paginate 1.2.0: Instant Gratification Monkey",
:body => ""
status 201
json :html_url => "https://github.com/mislav/will_paginate/releases/v1.2.0"
}
"""
When I successfully run `hub release create -dm "will_paginate 1.2.0: Instant Gratification Monkey" v1.2.0`
Then the output should contain exactly:
"""
https://github.com/mislav/will_paginate/releases/v1.2.0\n
"""
Scenario: Create a release with assets
Given the GitHub API server:
"""
post('/repos/mislav/will_paginate/releases') {
status 201
json :html_url => "https://github.com/mislav/will_paginate/releases/v1.2.0",
:upload_url => "https://api.github.com/uploads/assets{?name,label}"
}
post('/uploads/assets') {
assert :name => 'hello-1.2.0.tar.gz',
:label => 'Hello World'
status 201
}
"""
And a file named "hello-1.2.0.tar.gz" with:
"""
TARBALL
"""
When I successfully run `hub release create -m "hello" v1.2.0 -a "./hello-1.2.0.tar.gz#Hello World"`
Then the output should contain exactly:
"""
https://github.com/mislav/will_paginate/releases/v1.2.0
Attaching release asset `./hello-1.2.0.tar.gz'...\n
"""
Scenario: Edit existing release
Given the GitHub API server:
"""
get('/repos/mislav/will_paginate/releases') {
json [
{ url: 'https://api.github.com/repos/mislav/will_paginate/releases/123',
tag_name: 'v1.2.0',
name: 'will_paginate 1.2.0',
draft: true,
prerelease: false,
body: <<MARKDOWN
### Hello to my release
Here is what's broken:
- everything
MARKDOWN
},
]
}
patch('/repos/mislav/will_paginate/releases/123') {
assert :name => 'KITTENS EVERYWHERE',
:draft => false,
:prerelease => nil
json({})
}
"""
Given the git commit editor is "vim"
And the text editor adds:
"""
KITTENS EVERYWHERE
"""
When I successfully run `hub release edit --draft=false v1.2.0`
Then there should be no output