-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from remind101/updater
Add flag to update the binary.
- Loading branch information
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
if [ -z "$1" ]; then | ||
echo "need version number. make release VERSION=0.0.3" | ||
exit | ||
fi | ||
|
||
goxc -bc="linux,darwin" -d build -pv="$1" -tasks-=package | ||
|
||
find $(dirname $0)/../build/$1/* -type d -exec mv -n -- {}/deploy {}_deploy \; | ||
rm -R -- $(dirname $0)/../build/$1/*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package deploy | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/remind101/deploy/Godeps/_workspace/src/github.com/inconshreveable/go-update" | ||
"github.com/remind101/deploy/Godeps/_workspace/src/github.com/octokit/go-octokit/octokit" | ||
) | ||
|
||
const GitHubHost = "github.com" | ||
|
||
type Updater struct { | ||
Host string | ||
CurrentVersion string | ||
} | ||
|
||
func NewUpdater() *Updater { | ||
version := Version | ||
return &Updater{ | ||
Host: GitHubHost, | ||
CurrentVersion: version, | ||
} | ||
} | ||
|
||
func (updater *Updater) Update() (err error) { | ||
releaseName, version := updater.latestReleaseNameAndVersion() | ||
|
||
if version == "" { | ||
fmt.Println("There is no newer version of Deploy 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) { | ||
downloadURL := fmt.Sprintf("https://%s/remind101/deploy/releases/download/%s/%s_%s_deploy", updater.Host, releaseName, runtime.GOOS, runtime.GOARCH) | ||
|
||
fmt.Printf("Downloading %s...", version) | ||
err, _ = update.New().FromUrl(downloadURL) | ||
if err != nil { | ||
fmt.Printf("Update failed: %v\n", err) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (updater *Updater) latestReleaseNameAndVersion() (name, version string) { | ||
client := octokit.NewClient(nil) | ||
|
||
url, err := octokit.ReleasesURL.Expand(octokit.M{"owner": "remind101", "repo": "deploy"}) | ||
if err != nil { | ||
return | ||
} | ||
|
||
releases, result := client.Releases(url).All() | ||
if result.HasError() { | ||
err = fmt.Errorf("Error getting Deploy release: %s", result.Err) | ||
return | ||
} | ||
|
||
if len(releases) == 0 { | ||
err = fmt.Errorf("No Deploy release is available") | ||
return | ||
} | ||
|
||
name = releases[0].TagName | ||
version = strings.TrimPrefix(name, "v") | ||
return | ||
} |