Skip to content

Commit

Permalink
fix: Improve download log in installer ⚓ (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabyx authored Jul 2, 2023
1 parent bcd8032 commit 57501a6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion githooks/cmd/installer/download-binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func downloadBinaries(
log.PanicIfF(deploySettings == nil,
"Could not determine deploy settings.")

err := deploySettings.Download(versionTag, tempDir)
err := deploySettings.Download(log, versionTag, tempDir)
log.AssertNoErrorPanicF(err, "Could not download binaries.")

ext := ""
Expand Down
4 changes: 2 additions & 2 deletions githooks/updates/download/checksums.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
cm "github.com/gabyx/githooks/githooks/common"
)

// verifyChecksums verifies checksums with the signature and the public key, and returns
// verifyChecksumSignature verifies checksums with the signature and the public key, and returns
// the checksums content.
func verifyChecksums(checksums Checksums, publicPGP string) ([]byte, error) {
func verifyChecksumSignature(checksums Checksums, publicPGP string) ([]byte, error) {

checksumFile, err := GetFile(checksums.File.URL)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion githooks/updates/download/deploy-settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const deploySettingsVersion = 1

// IDeploySettings is the common interface for all deploy settings.
type IDeploySettings interface {
Download(versionTag string, dir string) error
Download(log cm.ILogContext, versionTag string, dir string) error
}

// LoadDeploySettings load the deploy settings from `file`.
Expand Down
19 changes: 14 additions & 5 deletions githooks/updates/download/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ type GiteaDeploySettings struct {
}

// Download downloads the version with `versionTag` into `dir` from a Gitea instance.
func (s *GiteaDeploySettings) Download(versionTag string, dir string) error {
return downloadGitea(s.APIUrl, s.Owner, s.Repository, versionTag, dir, s.PublicPGP)
func (s *GiteaDeploySettings) Download(log cm.ILogContext, versionTag string, dir string) error {
return downloadGitea(log, s.APIUrl, s.Owner, s.Repository, versionTag, dir, s.PublicPGP)
}

// Downloads the Githooks release with tag `versionTag` and
// extracts the matched asset into `dir`.
// The assert matches the OS and architecture of the current runtime.
func downloadGitea(url string, owner string, repo string, versionTag string, dir string, publicPGP string) error {
func downloadGitea(
log cm.ILogContext,
url string,
owner string,
repo string,
versionTag string,
dir string,
publicPGP string) error {

client, err := gitea.NewClient(url)
if err != nil {
Expand All @@ -55,13 +62,15 @@ func downloadGitea(url string, owner string, repo string, versionTag string, dir
cm.ErrorF("Could not select asset in repo '%s/%s' at tag '%s'.", owner, repo, versionTag))
}

checksumData, err := verifyChecksums(checksums, publicPGP)
log.InfoF("Verify checksum file '%s'.", checksums.File.URL)
checksumData, err := verifyChecksumSignature(checksums, publicPGP)
if err != nil {
return cm.CombineErrors(err,
cm.ErrorF("Signature verification of update failed."+
"Something is fishy!"))
}

log.InfoF("Downloading file '%s'.", target.URL)
response, err := GetFile(target.URL)
if err != nil {
return cm.CombineErrors(err, cm.ErrorF("Could not download url '%s'.", target.URL))
Expand All @@ -84,7 +93,7 @@ func downloadGitea(url string, owner string, repo string, versionTag string, dir
}
temp.Close()

// Validate checksum.
log.InfoF("Validate checksums.")
err = checkChecksum(temp.Name(), checksumData)
if err != nil {
return cm.CombineErrors(err, cm.ErrorF("Checksum validation failed."))
Expand Down
17 changes: 13 additions & 4 deletions githooks/updates/download/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ type GithubDeploySettings struct {
}

// Download downloads the version with `versionTag` to `dir` from a Github instance.
func (s *GithubDeploySettings) Download(versionTag string, dir string) error {
return downloadGithub(s.Owner, s.Repository, versionTag, dir, s.PublicPGP)
func (s *GithubDeploySettings) Download(log cm.ILogContext, versionTag string, dir string) error {
return downloadGithub(log, s.Owner, s.Repository, versionTag, dir, s.PublicPGP)
}

// Downloads the Githooks release with tag `versionTag` and
// extracts the matched asset into `dir`.
// The assert matches the OS and architecture of the current runtime.
func downloadGithub(owner string, repo string, versionTag string, dir string, publicPGP string) error {
func downloadGithub(
log cm.ILogContext,
owner string,
repo string,
versionTag string,
dir string,
publicPGP string) error {

client := github.NewClient(nil)
rel, _, err := client.Repositories.GetReleaseByTag(context.Background(),
Expand All @@ -58,13 +64,15 @@ func downloadGithub(owner string, repo string, versionTag string, dir string, pu
cm.ErrorF("Could not select asset in repo '%s/%s' at tag '%s'.", owner, repo, versionTag))
}

checksumData, err := verifyChecksums(checksums, publicPGP)
log.InfoF("Verify checksum file '%s'.", checksums.File.URL)
checksumData, err := verifyChecksumSignature(checksums, publicPGP)
if err != nil {
return cm.CombineErrors(err,
cm.ErrorF("Signature verification of update failed."+
"Something is fishy!"))
}

log.InfoF("Downloading file '%s'.", target.URL)
response, err := GetFile(target.URL)
if err != nil {
return cm.CombineErrors(err, cm.ErrorF("Could not download url '%s'.", target.URL))
Expand All @@ -88,6 +96,7 @@ func downloadGithub(owner string, repo string, versionTag string, dir string, pu
temp.Close()

// Validate checksum.
log.InfoF("Validate checksums.")
err = checkChecksum(temp.Name(), checksumData)
if err != nil {
return cm.CombineErrors(err, cm.ErrorF("Checksum validation failed."))
Expand Down
2 changes: 1 addition & 1 deletion githooks/updates/download/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type HTTPDeploySettings struct {

// Download downloads the Githooks from a template URL and
// extracts it into `dir`.
func (s *HTTPDeploySettings) Download(versionTag string, dir string) error {
func (s *HTTPDeploySettings) Download(log cm.ILogContext, versionTag string, dir string) error {
cm.Panic("Not implemented.")

return nil
Expand Down
2 changes: 1 addition & 1 deletion githooks/updates/download/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type LocalDeploySettings struct {

// Download downloads the Githooks from a template URL and
// extracts it into `dir`.
func (s *LocalDeploySettings) Download(versionTag string, dir string) error {
func (s *LocalDeploySettings) Download(log cm.ILogContext, versionTag string, dir string) error {
// Copy everything to director `dir`
pathTmpl := template.Must(template.New("").Parse(s.PathTemplate))

Expand Down

0 comments on commit 57501a6

Please sign in to comment.