Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
🌱 Use maximum verbosity for errors in command/get.go (#75)
Browse files Browse the repository at this point in the history
* 🌱 Use maximum verbosity for errors in command/get.go

* 🌱 Fix typo, use checksum variable

* πŸ› Fix linter

* Update command/get.go

Co-authored-by: Wilson E. Husin <[email protected]>

* Update command/get.go

Co-authored-by: Wilson E. Husin <[email protected]>

* Update command/get.go

Co-authored-by: Wilson E. Husin <[email protected]>

* 🌱 Remove extraneous fmt.Errorf for symLink err

Co-authored-by: Wilson E. Husin <[email protected]>
  • Loading branch information
chrisyxlee and wilsonehusin authored Apr 22, 2022
1 parent c5f0d3d commit c718d35
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions command/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package command

import (
"context"
"fmt"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -62,13 +63,19 @@ func symlink(binDir, progDir string, p *program.Lock) error {
func Get(ctx context.Context, conf *config.Runtime, p *program.Lock) error {
archiveName, err := p.ArchiveName(conf.OS, conf.Arch)
if err != nil {
return err
return fmt.Errorf("get archive name: %w", err)
}

checksum, ok := p.Checksums[archiveName]
if !ok {
return fmt.Errorf("unrecognized checksum reference '%s'", archiveName)
}

progDir := filepath.Join(conf.ProgDir, p.Checksums[archiveName].Binary+"-"+p.Name)
progDir := filepath.Join(conf.ProgDir, checksum.Binary+"-"+p.Name)
if err := Verify(ctx, conf, p); err == nil {
// Re-run symlink to renew atime and mtime, so that GNU Make will not rebuild in the future
internal.Log().Debug().Str("program", p.Name).Msg("found valid existing, re-linking")
// symLink already returns context in the error
return symlink(conf.BinDir, progDir, p)
}

Expand All @@ -90,24 +97,27 @@ func Get(ctx context.Context, conf *config.Runtime, p *program.Lock) error {

a, err := p.DownloadArchive(ctx, &download.HTTP{UseCache: conf.UseCache}, conf.OS, conf.Arch)
if err != nil {
return err
return fmt.Errorf("download archive: %w", err)
}

internal.Log().Debug().Str("program", p.Name).Msg("extracting archive")
bin, err := a.Extract(p.Name)
if err != nil {
return err
return fmt.Errorf("extract archive: %w", err)
}

internal.Log().Debug().Str("program", p.Name).Msg("found binary")

fullProgDir := filepath.Join(conf.BinDir, progDir)
if err = os.MkdirAll(fullProgDir, 0755); err != nil {
return err
if err := os.MkdirAll(fullProgDir, 0755); err != nil {
return fmt.Errorf("ensuring directory existence '%s': %w", fullProgDir, err)
}

binPath := filepath.Join(fullProgDir, p.Name)
err = os.WriteFile(binPath, bin, 0755)
if err != nil {
return err
if err := os.WriteFile(binPath, bin, 0755); err != nil {
return fmt.Errorf("write file '%s': %w", binPath, err)
}

internal.Log().Debug().Str("output", binPath).Str("program", p.Name).Msg("downloaded")

return symlink(conf.BinDir, progDir, p)
Expand Down

0 comments on commit c718d35

Please sign in to comment.