Skip to content

Commit

Permalink
Pass Info struct as pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
tcnksm committed Oct 8, 2014
1 parent 5cb315a commit 7f334ae
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
40 changes: 25 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
repo = flag.String([]string{"r", "-repository"}, "", "Repository name")
token = flag.String([]string{"t", "-token"}, "", "Github API Token")
parallel = flag.Int([]string{"p", "--parallel"}, -1, "Parallelization factor")
flDelete = flag.Bool([]string{"-delete"}, false, "Delete release if it exists")
flDraft = flag.Bool([]string{"-draft"}, false, "Create unpublised release")
flPrerelease = flag.Bool([]string{"-prerelease"}, false, "Create prerelease")
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
Expand All @@ -31,7 +32,7 @@ func debug(v ...interface{}) {
}

func showVersion() {
fmt.Fprintf(os.Stderr, "ghr %s\n", Version)
fmt.Fprintf(os.Stderr, "ghr version %s, build %s \n", Version, GitCommit)
}

func showHelp() {
Expand Down Expand Up @@ -68,6 +69,11 @@ func ghrMain() int {

flag.Parse()

if *flDebug {
os.Setenv("DEBUG", "1")
debug("Run as DEBUG mode")
}

if *flHelp {
showHelp()
return 0
Expand All @@ -78,11 +84,6 @@ func ghrMain() int {
return 0
}

if *flDebug {
os.Setenv("DEBUG", "1")
debug("Run as DEBUG mode")
}

if len(flag.Args()) != 2 {
showHelp()
return 1
Expand All @@ -91,6 +92,12 @@ func ghrMain() int {
tag := flag.Arg(0)
inputPath := flag.Arg(1)

// Limit amount of parallelism
// by number of logic CPU
if *parallel <= 0 {
*parallel = runtime.NumCPU()
}

if *token == "" {
*token = os.Getenv("GITHUB_TOKEN")
if *token == "" {
Expand All @@ -117,13 +124,7 @@ func ghrMain() int {
}
}

// Limit amount of parallelism
// by number of logic CPU
if *parallel <= 0 {
*parallel = runtime.NumCPU()
}

info := Info{
info := &Info{
TagName: tag,
Token: *token,
OwnerName: *owner,
Expand All @@ -140,6 +141,13 @@ func ghrMain() int {
return 1
}

// Relase is exist but delete
if id != -1 && *flDelete {
fmt.Fprintf(os.Stderr, "Delete before upload\n")
return 1
}

// Relase is not exists
if id == -1 {
id, err = CreateNewRelease(info)
if err != nil {
Expand Down Expand Up @@ -214,13 +222,15 @@ Options:
-t, --token Github API Token
-r, --repository Github repository name
-p, --parallel=-1 Amount of parallelism, defaults to number of CPUs
 --delete Delete release if same version exists
--draft Create unpublised release
--prerelease Create prerelease
-h, --help Print this message and quit
-v, --version Print version information and quit
--debug=false Run as DEBUG mode
Example:
$ ghr v1.0.0 pkg/dist/
$ ghr v1.0.2 pkg/dist/tool.zip
$ ghr v1.0.0 dist/
$ ghr --delete v1.0.0 dist/
$ ghr v1.0.2 dist/tool.zip
`
6 changes: 3 additions & 3 deletions release.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const (
RELEASE_URL = "https://api.github.com/repos/%s/%s/releases"
)

func releaseURL(info Info) string {
func releaseURL(info *Info) string {
return fmt.Sprintf(RELEASE_URL, info.OwnerName, info.RepoName)
}

func releaseRequest(info Info) ([]byte, error) {
params := ReleaseRequest{
func releaseRequest(info *Info) ([]byte, error) {
params := &ReleaseRequest{
TagName: info.TagName,
TargetCommitish: info.TargetCommitish,
Draft: info.Draft,
Expand Down
4 changes: 2 additions & 2 deletions release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestReleaseURL(t *testing.T) {
RegisterTestingT(t)

info := Info{
info := &Info{
OwnerName: "tc",
RepoName: "tool",
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestSearchIDByTag(t *testing.T) {
func TestReleaseRequest(t *testing.T) {
RegisterTestingT(t)

info := Info{
info := &Info{
TagName: "v1.0.0",
TargetCommitish: "master",
Draft: false,
Expand Down
6 changes: 3 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func checkStatusCreated(code int, msg string) error {
return nil
}

func GetReleaseID(info Info) (int, error) {
func GetReleaseID(info *Info) (int, error) {
requestURL := releaseURL(info)
debug(requestURL)

Expand All @@ -49,7 +49,7 @@ func GetReleaseID(info Info) (int, error) {
return SearchIDByTag(res.Body, info.TagName)
}

func CreateNewRelease(info Info) (int, error) {
func CreateNewRelease(info *Info) (int, error) {

requestURL := releaseURL(info)
debug(requestURL)
Expand Down Expand Up @@ -79,7 +79,7 @@ func CreateNewRelease(info Info) (int, error) {
return CreatedID(res.Body)
}

func UploadAsset(info Info, path string) error {
func UploadAsset(info *Info, path string) error {

file, err := os.Stat(path)
requestURL := uploadURL(info, file.Name())
Expand Down
2 changes: 1 addition & 1 deletion upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
UPLOAD_URL = "https://uploads.github.com/repos/%s/%s/releases/%d/assets"
)

func uploadURL(info Info, name string) string {
func uploadURL(info *Info, name string) string {
v := url.Values{}
v.Set("name", name)

Expand Down
2 changes: 1 addition & 1 deletion upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func TestUploadURL(t *testing.T) {
RegisterTestingT(t)

info := Info{
info := &Info{
ID: 123,
OwnerName: "tc",
RepoName: "tool",
Expand Down

0 comments on commit 7f334ae

Please sign in to comment.