Code cleaning

Using gofumpt. Add documentation.
Some fixes on type checking and const icon type declaration.
This commit is contained in:
2024-04-05 07:56:27 +02:00
parent 2aad5d4b9d
commit 441b30a570
16 changed files with 86 additions and 93 deletions

View File

@@ -14,8 +14,10 @@ import (
"golang.org/x/mod/semver"
)
var exe, _ = os.Executable()
var Version = "master" // reset by cmd/main.go
var (
exe, _ = os.Executable()
Version = "master" // reset by cmd/main.go
)
// Asset is a github asset from release url.
type Asset struct {
@@ -25,7 +27,6 @@ type Asset struct {
// CheckLatestVersion check katenary latest version from release and propose to download it
func CheckLatestVersion() (string, []Asset, error) {
githuburl := "https://api.github.com/repos/metal3d/katenary/releases/latest"
// Create a HTTP client with 1s timeout
client := &http.Client{
@@ -45,7 +46,7 @@ func CheckLatestVersion() (string, []Asset, error) {
defer resp.Body.Close()
// Get tag_name from the json response
var release = struct {
release := struct {
TagName string `json:"tag_name"`
Assets []Asset `json:"assets"`
PreRelease bool `json:"prerelease"`
@@ -57,19 +58,19 @@ func CheckLatestVersion() (string, []Asset, error) {
// if it's a prerelease, don't update
if release.PreRelease {
return "", nil, errors.New("Prerelease detected, not updating")
return "", nil, errors.New("prerelease detected, not updating")
}
// no tag, don't update
if release.TagName == "" {
return "", nil, errors.New("No release found")
return "", nil, errors.New("no release found")
}
// compare the current version, if the current version is the same or lower than the latest version, don't update
versions := []string{Version, release.TagName}
semver.Sort(versions)
if versions[1] == Version {
return "", nil, errors.New("Current version is the latest version")
return "", nil, errors.New("current version is the latest version")
}
return release.TagName, release.Assets, nil
@@ -77,7 +78,6 @@ func CheckLatestVersion() (string, []Asset, error) {
// DownloadLatestVersion will download the latest version of katenary.
func DownloadLatestVersion(assets []Asset) error {
defer func() {
if r := recover(); r != nil {
os.Rename(exe+".old", exe)
@@ -117,7 +117,7 @@ func DownloadLatestVersion(assets []Asset) error {
}
default:
fmt.Println("Unsupported OS")
err = errors.New("Unsupported OS")
err = errors.New("unsupported OS")
}
}
if err == nil {
@@ -138,7 +138,7 @@ func DownloadFile(url, exe string) error {
return err
}
defer resp.Body.Close()
fp, err := os.OpenFile(exe, os.O_WRONLY|os.O_CREATE, 0755)
fp, err := os.OpenFile(exe, os.O_WRONLY|os.O_CREATE, 0o755)
if err != nil {
return err
}