Skip to content

Commit

Permalink
dont't show and remove checksum files by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Apr 12, 2023
1 parent 0ed36ec commit 1cb8a34
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ downloaded 4 + 1 checksum file(s)
verified 4

> ghrel -l -p '*linux*' jreisinger/ghrel
Asset Checksum file Updated Size Download count
----- ------------- ------- ---- --------------
checksums.txt true 2023-04-12 976 1
ghrel_0.7.0_linux_386.tar.gz false 2023-04-12 2028791 1
ghrel_0.7.0_linux_amd64.tar.gz false 2023-04-12 2142561 1
ghrel_0.7.0_linux_arm64.tar.gz false 2023-04-12 1972445 1
ghrel_0.7.0_linux_armv6.tar.gz false 2023-04-12 2006885 1
Asset Updated Size Download count
----- ------- ---- --------------
ghrel_0.7.1_linux_386.tar.gz 2023-04-12 2029844 2
ghrel_0.7.1_linux_amd64.tar.gz 2023-04-12 2143213 2
ghrel_0.7.1_linux_arm64.tar.gz 2023-04-12 1973067 2
ghrel_0.7.1_linux_armv6.tar.gz 2023-04-12 2007574 2
```

To use ghrel, download a [binary](https://github.com/jreisinger/ghrel/releases) for your system and architecture or `go install github.com/jreisinger/ghrel@latest`.
15 changes: 9 additions & 6 deletions asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ func Download(a Asset) error {
return nil
}

// Print prints table of assets.
func Print(assets []Asset) {
const format = "%v\t%v\t%v\t%v\t%v\n"
// List prints table of assets.
func List(assets []Asset, showChecksumFiles bool) {
const format = "%v\t%v\t%v\t%v\n"
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(tw, format, "Asset", "Checksum file", "Updated", "Size", "Download count")
fmt.Fprintf(tw, format, "-----", "-------------", "-------", "----", "--------------")
fmt.Fprintf(tw, format, "Asset", "Updated", "Size", "Download count")
fmt.Fprintf(tw, format, "-----", "-------", "----", "--------------")
for _, a := range assets {
fmt.Fprintf(tw, format, a.Name, a.IsChecksumFile, a.UpdatedAt.Format("2006-01-02"), a.Size, a.DownloadCount)
if a.IsChecksumFile && !showChecksumFiles {
continue
}
fmt.Fprintf(tw, format, a.Name, a.UpdatedAt.Format("2006-01-02"), a.Size, a.DownloadCount)
}
tw.Flush()
}
Expand Down
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"github.com/jreisinger/ghrel/checksum"
)

var l = flag.Bool("l", false, "just list assets")
var p = flag.String("p", "", "only assets matching shell `pattern` (doesn't apply to checksums files)")
var c = flag.Bool("c", false, "keep (or list) checksum files")
var l = flag.Bool("l", false, "only list assets")
var p = flag.String("p", "", "assets matching shell `pattern` (doesn't apply to checksum files)")

func main() {
flag.Usage = func() {
Expand All @@ -39,7 +40,7 @@ func main() {
}

if *l {
asset.Print(assets)
asset.List(assets, *c)
os.Exit(0)
}

Expand All @@ -65,6 +66,15 @@ func main() {
}
wg.Wait()

// Remove checksum files.
defer func() {
if !*c {
if err := removeChecksumFiles(assets); err != nil {
log.Print(err)
}
}
}()

// Print download statistics.
nFiles, nCheckumsFiles := asset.Count(assets)
fmt.Printf("downloaded\t%d + %d checksum file(s)\n", nFiles, nCheckumsFiles)
Expand Down Expand Up @@ -107,3 +117,14 @@ Asset:
}
fmt.Printf("verified\t%d\n", verifiedFiles)
}

func removeChecksumFiles(assets []asset.Asset) error {
for _, a := range assets {
if a.IsChecksumFile {
if err := os.Remove(a.Name); err != nil {
return err
}
}
}
return nil
}

0 comments on commit 1cb8a34

Please sign in to comment.