-
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #52 from lukaszraczylo/additional-improvements
additional improvements
- Loading branch information
Showing
24 changed files
with
2,110 additions
and
824 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
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 |
---|---|---|
@@ -1,117 +1,16 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/lukaszraczylo/ask" | ||
graphql "github.com/lukaszraczylo/go-simple-graphql" | ||
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging" | ||
"github.com/melbahja/got" | ||
"github.com/lukaszraczylo/semver-generator/cmd/utils" | ||
) | ||
|
||
func updatePackage() bool { | ||
ghToken, ghTokenSet := os.LookupEnv("GITHUB_TOKEN") | ||
if ghTokenSet { | ||
binaryName := fmt.Sprintf("semver-gen-%s-%s", runtime.GOOS, runtime.GOARCH) | ||
logger.Info(&libpack_logger.LogMessage{Message: "Checking for updates", Pairs: map[string]interface{}{"binaryName": binaryName}}) | ||
gql := graphql.NewConnection() | ||
|
||
gql.SetEndpoint("https://api.github.com/graphql") | ||
gql.SetOutput("mapstring") | ||
// These functions are now in the utils package | ||
// They are kept here as stubs for backward compatibility | ||
|
||
headers := map[string]interface{}{ | ||
"Authorization": fmt.Sprintf("Bearer %s", ghToken), | ||
} | ||
variables := map[string]interface{}{ | ||
"binaryName": binaryName, | ||
} | ||
var query = `query ($binaryName: String) { | ||
repository(name: "semver-generator", owner: "lukaszraczylo") { | ||
latestRelease { | ||
releaseAssets(first: 10, name: $binaryName) { | ||
edges { | ||
node { | ||
name | ||
downloadUrl | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}` | ||
result, err := gql.Query(query, variables, headers) | ||
if err != nil { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to query GitHub API", Pairs: map[string]interface{}{"error": err.Error()}}) | ||
return false | ||
} | ||
|
||
output, ok := ask.For(result, "repository.latestRelease.releaseAssets.edges[0].node.downloadUrl").String("") | ||
if !ok { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to obtain download url for the binary", Pairs: map[string]interface{}{"binary": binaryName, "output": output}}) | ||
return false | ||
} | ||
if flag.Lookup("test.v") == nil && os.Getenv("CI") == "" { | ||
downloadedBinaryPath := fmt.Sprintf("/tmp/%s", binaryName) | ||
g := got.New() | ||
err = g.Download(output, downloadedBinaryPath) | ||
if err != nil { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to download binary", Pairs: map[string]interface{}{"error": err.Error(), "binaryPath": downloadedBinaryPath}}) | ||
return false | ||
} | ||
currentBinary, err := os.Executable() | ||
if err != nil { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to obtain current binary path", Pairs: map[string]interface{}{"error": err.Error()}}) | ||
return false | ||
} | ||
err = os.Rename(downloadedBinaryPath, currentBinary) | ||
if err != nil { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to overwrite current binary", Pairs: map[string]interface{}{"error": err.Error()}}) | ||
return false | ||
} | ||
err = os.Chmod(currentBinary, 0777) | ||
if err != nil { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to make binary executable", Pairs: map[string]interface{}{"error": err.Error()}}) | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
func updatePackage() bool { | ||
return utils.UpdatePackage() | ||
} | ||
|
||
func checkLatestRelease() (string, bool) { | ||
ghToken, ghTokenSet := os.LookupEnv("GITHUB_TOKEN") | ||
if ghTokenSet { | ||
gql := graphql.NewConnection() | ||
gql.SetEndpoint("https://api.github.com/graphql") | ||
headers := map[string]interface{}{ | ||
"Authorization": fmt.Sprintf("bearer %s", ghToken), | ||
} | ||
variables := map[string]interface{}{} | ||
var query = `query { | ||
repository(name: "semver-generator", owner: "lukaszraczylo", followRenames: true) { | ||
releases(last: 2) { | ||
nodes { | ||
tag { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
}` | ||
result, err := gql.Query(query, variables, headers) | ||
if err != nil { | ||
logger.Error(&libpack_logger.LogMessage{Message: "Unable to query GitHub API", Pairs: map[string]interface{}{"error": err.Error()}}) | ||
return "", false | ||
} | ||
output, _ := ask.For(result, "repository.releases.nodes[0].tag.name").String("") | ||
if output == "v1" { | ||
output, _ = ask.For(result, "repository.releases.nodes[1].tag.name").String("") | ||
} | ||
return output, true | ||
} else { | ||
return "[no GITHUB_TOKEN set]", false | ||
} | ||
return utils.CheckLatestRelease() | ||
} |
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
Oops, something went wrong.