-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
language: go | ||
go: | ||
- '1.8' | ||
- '1.8' | ||
before_script: | ||
- go get -u ./... | ||
- go get -u ./... | ||
script: | ||
- go test -cover ./... | ||
- go test -cover ./... | ||
before_deploy: | ||
- go get -u github.com/inconshreveable/mousetrap | ||
- make build | ||
deploy: | ||
provider: releases | ||
api_key: | ||
secure: HqimbJ16THwHzXi/1Qznhi4rSH7nu01+ziZR5zVj1J3XcEBzyD9Wu/yM5ZojEt190M+0yYJrADKq8ZvVQLvmbxM05mNN7a8oJlQI0IOvOEO0GCggicXPaLPiYMzEU+/IteCyfYmOtf4S2E59Lly4GnUXkR5wqE/itrKjfMlGCplP1ooYTD6+FltI7BCXN5bJPX271Aack7KAS/pz6S0T9jUpQ4eA5Fp/+C14aXLcV9KJfsGYgLNhAI2unClqUOMJT7mQQoAkaAERw77t4MAT0Z6unL/EGvvF9NQNi6UD8QLqfjyqPv5HcT+GP+MzYjR8NBOreGtufCGiQx4mM8AvTOkfRkNWgfHAVVA2kha/CMqScHYuzWpVbbLltmnuNoLnuMWXmC5LtaMZtJwIsDnYoz7fztFcbazGVVvEKVUvARfGyM29jZNNbbMiv92AFjRYF/1PegAs/MJ5TUzfTKD477McDY6CBHWA5L46RFoX+2klKq2M8vDZsThw3MvObJTrLAJw3ffeyIMV2k5Hsq8epHJNJ0Y+LSO+uxBqchXnhuhJLSjTmJXRGdKY6/bR7oQNVeW0Oi6nLuLE9HX6eIaUITpUvRgdMVfgf1ph8zjtEPygoedaY8OFQ4oLeN4dKr4q3OJy1p872uiHRtPvm0nJx7TgBzdd0EqOQh5lS5r/Si8= | ||
skip_cleanup: true | ||
file: | ||
- ian-darwin-amd64 | ||
- ian-linux-amd64 | ||
- ian-windows-amd64.exe | ||
on: | ||
repo: thylong/ian | ||
tags: true |
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
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,137 @@ | ||
// Copyright © 2016 Theotime LEVEQUE [email protected] | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/mitchellh/ioprogress" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var httpGet = http.Get | ||
var version string | ||
var ianLastCommit = "https://api.github.com/repos/thylong/ian/commits/master" | ||
var ianLastRelease = "https://api.github.com/repos/thylong/ian/releases/latest" | ||
|
||
func init() { | ||
RootCmd.AddCommand(selfUpdateCmd) | ||
} | ||
|
||
var selfUpdateCmd = &cobra.Command{ | ||
Use: "self-update", | ||
Short: "Update ian to the last version", | ||
Long: `Update ian to the last version.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
localVersion := viper.GetString("VERSION") | ||
|
||
resp, err := httpGet(ianLastCommit) | ||
if err != nil { | ||
fmt.Printf("%v Cannot retrieve ian's last version infos\n", color.RedString("Error:")) | ||
return | ||
} | ||
content, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
fmt.Printf("%v Cannot retrieve ian's last version infos\n", color.RedString("Error:")) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
var jsonContent map[string]interface{} | ||
err = json.Unmarshal(content, &jsonContent) | ||
if err != nil { | ||
fmt.Printf("%v Cannot retrieve ian's last version\n", color.RedString("Error:")) | ||
return | ||
} | ||
remoteVersion := jsonContent["sha"].(string)[:7] | ||
|
||
if localVersion != remoteVersion { | ||
fmt.Printf("ian version outdated... \n") | ||
resp, err := httpGet(ianLastRelease) | ||
if err != nil { | ||
fmt.Printf("%v Cannot retrieve ian's last release infos\n", color.RedString("Error:")) | ||
return | ||
} | ||
content, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
fmt.Printf("%v Cannot retrieve ian's last release infos\n", color.RedString("Error:")) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
var lastReleaseContent map[string]interface{} | ||
err = json.Unmarshal(content, &lastReleaseContent) | ||
if err != nil { | ||
fmt.Printf("%v Cannot retrieve ian's last version\n", color.RedString("Error:")) | ||
return | ||
} | ||
fmt.Println(lastReleaseContent) | ||
downloadURL := lastReleaseContent["assets"].([]interface{})[0].(map[string]interface{})["browser_download_url"] | ||
resp, err = httpGet(downloadURL.(string)) | ||
if err != nil { | ||
fmt.Printf("%v Cannot download ian's last version\n", color.RedString("Error:")) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
progressR := &ioprogress.Reader{ | ||
Reader: resp.Body, | ||
Size: resp.ContentLength, | ||
DrawInterval: 500 * time.Millisecond, | ||
DrawFunc: ioprogress.DrawTerminalf(os.Stdout, func(progress, total int64) string { | ||
bar := ioprogress.DrawTextFormatBar(40) | ||
return fmt.Sprintf("%s %20s", bar(progress, total), ioprogress.DrawTextFormatBytes(progress, total)) | ||
}), | ||
} | ||
|
||
data, err := ioutil.ReadAll(progressR) | ||
if err != nil { | ||
fmt.Printf("%v ian's last version seems broken\n", color.RedString("Error:")) | ||
return | ||
} | ||
dest, err := os.Executable() | ||
if err != nil { | ||
fmt.Printf("%v ian's last version seems not executable\n", color.RedString("Error:")) | ||
return | ||
} | ||
|
||
// Move the old version to a backup path that we can recover from | ||
// in case the upgrade fails | ||
destBackup := dest + ".bak" | ||
if _, err := os.Stat(dest); err == nil { | ||
os.Rename(dest, destBackup) | ||
} | ||
|
||
fmt.Printf("Downloading ian's new version to %s\n", dest) | ||
if err := ioutil.WriteFile(dest, data, 0755); err != nil { | ||
os.Rename(destBackup, dest) | ||
fmt.Printf("%v Failed to update ian\n", color.RedString("Error:")) | ||
return | ||
} | ||
|
||
// Removing backup | ||
os.Remove(destBackup) | ||
|
||
fmt.Printf("ian updated with success to version %s\n", remoteVersion) | ||
} | ||
}, | ||
} |
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