Skip to content

Commit

Permalink
Extract works
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jun 15, 2023
1 parent 25c4671 commit 2908cc9
Show file tree
Hide file tree
Showing 26 changed files with 747 additions and 108 deletions.
24 changes: 20 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,40 @@ require (
github.com/gookit/color v1.5.3
github.com/imdario/mergo v0.3.13
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/mholt/archiver/v4 v4.0.0-alpha.8
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.3
github.com/u-root/u-root v0.11.0
github.com/wavesoftware/go-commandline v1.0.0
go.uber.org/zap v1.24.0
golang.org/x/oauth2 v0.2.0
golang.org/x/term v0.4.0
golang.org/x/term v0.5.0
sigs.k8s.io/yaml v1.3.0
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/bodgit/plumbing v1.3.0 // indirect
github.com/bodgit/sevenzip v1.4.2 // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/go-eden/common v0.1.14 // indirect
github.com/go-eden/routine v1.0.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/klauspost/compress v1.16.6 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand All @@ -46,17 +57,22 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/wavesoftware/go-retcode v1.0.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
Expand Down
249 changes: 242 additions & 7 deletions go.sum

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f h1:o/kfcElHqOi
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ=
github.com/connesc/cipherio v0.2.1 h1:FGtpTPMbKNNWByNrr9aEBtaJtXjqOzkIXNYJp6OEycw=
github.com/containerd/aufs v1.0.0 h1:2oeJiwX5HstO7shSrPZjrohJZLzK36wvpdmzDRkL/LY=
github.com/containerd/btrfs v1.0.0 h1:osn1exbzdub9L5SouXO5swW4ea/xVdJZ3wokxN5GrnA=
github.com/containerd/cgroups v1.0.3 h1:ADZftAkglvCiD44c77s5YmMqaP2pzVCFZvBmAlBdAP4=
Expand Down Expand Up @@ -498,7 +499,6 @@ github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQ
github.com/docker/libtrust v0.0.0-20150114040149-fa567046d9b1 h1:ZClxb8laGDf5arXfYcAtECDFgAgHklGI8CxgjHnXKJ4=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 h1:cenwrSVm+Z7QLSV/BsnenAOcDXdX4cMv4wP0B/5QbPg=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780 h1:tFh1tRc4CA31yP6qDcu+Trax5wW5GuMxvkIba07qVLY=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/eapache/go-resiliency v1.1.0 h1:1NtRmCAqadE2FN4ZcN6g90TP3uk8cg9rn9eNK2197aU=
Expand Down Expand Up @@ -616,7 +616,6 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2 h1:23T5iq8rbUYlhpt5DB4XJkc6BU31uODLD1o1gKvZmD0=
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a h1:w8hkcTqaFpzKqonE9uMCefW1WDie15eSP/4MssdenaM=
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613 h1:9kfjN3AdxcbsZBf8NjltjWihK2QfBBBZuv91cMFfDHw=
Expand Down Expand Up @@ -947,6 +946,7 @@ github.com/rs/xid v1.2.1 h1:mhH9Nq+C1fY2l1XIpgxIiUOfNpRBYH1kKcr+qfKgjRc=
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/ryancurrah/gomodguard v1.2.3 h1:ww2fsjqocGCAFamzvv/b8IsRduuHHeK2MHTcTxZTQX8=
github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8OUZI9xFw=
github.com/ryanuber/columnize v2.1.0+incompatible h1:j1Wcmh8OrK4Q7GXY+V7SVSY8nUWQxHW5TkBe7YUl+2s=
Expand Down Expand Up @@ -1147,15 +1147,20 @@ golang.org/x/exp v0.0.0-20220823124025-807a23277127 h1:S4NrSKDfihhl3+4jSTgwoIevK
golang.org/x/image v0.0.0-20190802002840-cff245a6509b h1:+qEpEAPhDZ1o0x3tHzZTQDArnOixOzGD9HUJfcg0mb4=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/mobile v0.0.0-20201217150744-e6ae53a27f4f h1:kgfVkAEEQXXQ0qc6dH7n6y37NAYmTFmz0YRwrRjgxKw=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f h1:uF6paiQQebLeSXkrTqHqz0MXhXXS1KgF41eUdBNvxK0=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
Expand Down
9 changes: 2 additions & 7 deletions internal/ght/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package ght
import (
"path"

"github.com/cardil/ghet/pkg/metadata"
"github.com/kirsle/configdir"
configdir "github.com/cardil/ghet/pkg/config/dir"
"github.com/spf13/cobra"
)

Expand All @@ -13,11 +12,7 @@ type Args struct {
}

func (a Args) Defaults() Args {
configPath := configdir.LocalConfig(metadata.Name)
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
panic(err)
}
configPath := configdir.Config()
settingsPth := path.Join(configPath, "settings.yaml")

return Args{
Expand Down
16 changes: 10 additions & 6 deletions internal/ght/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func installCmd(_ *Args) *cobra.Command {
}

type installArgs struct {
site string
version string
basename string
checksums string
repo string
site string
version string
basename string
checksums string
repo string
multipleBinaries bool
}

func (ia *installArgs) defaults() installArgs {
Expand All @@ -56,6 +57,8 @@ func (ia *installArgs) setFlags(c *cobra.Command) {
"if not given a repo name will be used")
fl.StringVar(&ia.checksums, "checksums", defs.checksums,
"a checksums file name")
fl.BoolVar(&ia.multipleBinaries, "multiple-binaries", defs.multipleBinaries,
"if set, will download all binaries from the archive")
c.Args = cobra.ExactArgs(1)
}

Expand Down Expand Up @@ -86,7 +89,8 @@ func (ia *installArgs) parse(ctx context.Context) install.Args {
FileName: ia.checksumsFilename(),
},
},
Site: cfg.Site(ia.site),
Site: cfg.Site(ia.site),
MultipleBinaries: ia.multipleBinaries,
}
args = args.WithDefaults()
return args
Expand Down
37 changes: 37 additions & 0 deletions pkg/config/dir/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dir

import (
"os"

"github.com/cardil/ghet/pkg/metadata"
"github.com/kirsle/configdir"
)

const (
ConfigDirEnvName = "GHET_CONFIG_DIR"
CacheDirEnvName = "GHET_CACHE_DIR"
)

func Config() string {
configPath := os.Getenv(ConfigDirEnvName)
if configPath == "" {
configPath = configdir.LocalConfig(metadata.Name)
}
err := configdir.MakePath(configPath) // Ensure it exists.
if err != nil {
panic(err)
}
return configPath
}

func Cache() string {
cachePath := os.Getenv(CacheDirEnvName)
if cachePath == "" {
cachePath = configdir.LocalCache(metadata.Name)
}
err := configdir.MakePath(cachePath) // Ensure it exists.
if err != nil {
panic(err)
}
return cachePath
}
46 changes: 29 additions & 17 deletions pkg/ghet/download/checksums.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var ErrChecksumMismatch = errors.New("checksum mismatch")
// ErrNotVerifiedAssets is returned when there are no verified assets.
var ErrNotVerifiedAssets = errors.New("not verified assets")

var bsdStyleChecksums = regexp.MustCompile(`^(SHA[0-9]{1,3})\s+\([^)]+\)\s+=\s+([a-fA-F0-9]{32,128})$`)
var bsdStyleChecksums = regexp.MustCompile(`^(SHA[0-9]{1,3})\s+\(([^)]+)\)\s+=\s+([a-fA-F0-9]{32,128})$`)

func verifyChecksums(ctx context.Context, assets []githubapi.Asset, args Args) error {
l := output.LoggerFrom(ctx)
Expand All @@ -51,36 +51,38 @@ func verifyChecksums(ctx context.Context, assets []githubapi.Asset, args Args) e
ca := index.Checksums[0]

if len(index.Checksums) > 1 {
options := make([]string, len(index.Checksums))
for i, c := range index.Checksums {
options[i] = c.Name
}
intwidgets, err := widgets.Interactive(ctx)
iwidgets, err := tui.Interactive[githubapi.Asset](ctx)
if err != nil {
if errors.Is(err, tui.ErrNotInteractive) {
l.Errorf("Number of checksums is %d. Expected just one.", len(index.Checksums))
return fmt.Errorf("%w: %d", ErrTooManyChecksums, len(index.Checksums))
}
return fmt.Errorf("%w: %v", ErrUnexpected, err)
}
selected := intwidgets.Ask(ctx, options,
selected := iwidgets.Choose(ctx, index.Checksums,
"⚠️ More than one checksum file found. Choose proper one")
for _, c := range index.Checksums {
if c.Name == selected {
if c == selected {
ca = c
break
}
}
}
artifacts := append(index.Archives, index.Other...)
if len(artifacts) == 0 {
l.Errorf("No assets to verify")
return fmt.Errorf("%w: %d", ErrNotVerifiedAssets, len(index.Other))
}
defaultArtifact := artifacts[0]

l = l.WithFields(slog.Fields{"checksum": ca.Name})
l.Debug("Verifying checksum")

csp := checksumParser{Asset: ca, Args: args}
if cs, err := csp.parse(ctx); err != nil {
if cs, err := csp.parse(ctx, defaultArtifact); err != nil {
return err
} else {
err = cs.verify(ctx, append(index.Archives, index.Other...), args.Destination)
err = cs.verify(ctx, artifacts, args.Destination)
if err != nil {
return err
}
Expand All @@ -104,7 +106,7 @@ type checksumParser struct {
*checksums
}

func (p *checksumParser) parse(ctx context.Context) (*checksums, error) {
func (p *checksumParser) parse(ctx context.Context, defaultArtifact githubapi.Asset) (*checksums, error) {
l := output.LoggerFrom(ctx)
fp := path.Join(p.Destination, p.Name)
l.Debugf("Parsing checksum: %s", fp)
Expand All @@ -122,7 +124,7 @@ func (p *checksumParser) parse(ctx context.Context) (*checksums, error) {
entries: make([]checksumEntry, 0, 1),
}
for scanner.Scan() {
if err := p.parseLine(ctx, scanner.Text()); err != nil {
if err := p.parseLine(ctx, scanner.Text(), defaultArtifact); err != nil {
return nil, err
}
}
Expand All @@ -134,12 +136,16 @@ func (p *checksumParser) parse(ctx context.Context) (*checksums, error) {
return p.checksums, nil
}

func (p *checksumParser) parseLine(ctx context.Context, line string) error {
func (p *checksumParser) parseLine(
ctx context.Context,
line string,
defaultArtifact githubapi.Asset,
) error {
var entry checksumEntry
if bsdStyleChecksums.MatchString(line) {
entry = p.parseBSDStyleChecksum(ctx, line)
} else {
if e, err := p.parseRegularChecksum(line); err != nil {
if e, err := p.parseRegularChecksum(line, defaultArtifact); err != nil {
return err
} else {
entry = e
Expand All @@ -149,15 +155,21 @@ func (p *checksumParser) parseLine(ctx context.Context, line string) error {
return nil
}

func (p *checksumParser) parseRegularChecksum(line string) (checksumEntry, error) {
func (p *checksumParser) parseRegularChecksum(
line string,
artifact githubapi.Asset,
) (checksumEntry, error) {
fields := strings.Fields(line)
if len(fields) != 2 {
if len(fields) > 2 || len(fields) < 1 {
return checksumEntry{}, unexpected(fmt.Errorf("invalid checksum line: %s", line))
}

entry := checksumEntry{
hash: fields[0],
filename: fields[1],
filename: artifact.Name,
}
if len(fields) == 2 {
entry.filename = fields[1]
}
if algo, err := checksumAlgorithmForHash(entry.hash); err != nil {
return checksumEntry{}, err
Expand Down
10 changes: 3 additions & 7 deletions pkg/ghet/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"strings"

"github.com/1set/gut/yos"
configdir "github.com/cardil/ghet/pkg/config/dir"
githubapi "github.com/cardil/ghet/pkg/github/api"
"github.com/cardil/ghet/pkg/metadata"
"github.com/cardil/ghet/pkg/output"
"github.com/cardil/ghet/pkg/output/tui"
slog "github.com/go-eden/slf4go"
"github.com/kirsle/configdir"
"github.com/pkg/errors"
)

Expand All @@ -34,10 +33,7 @@ func downloadAsset(ctx context.Context, asset assetInfo, args Args) error {
l := output.LoggerFrom(ctx).WithFields(slog.Fields{
"asset": asset.Name,
})
cachePath := configdir.LocalCache(metadata.Name)
if err := configdir.MakePath(cachePath); err != nil {
return errors.WithStack(err)
}
cachePath := configdir.Cache()
cachePath = path.Join(cachePath, fmt.Sprintf("%d", asset.ID))

if fileExists(l, cachePath, asset.Size) {
Expand All @@ -47,7 +43,7 @@ func downloadAsset(ctx context.Context, asset assetInfo, args Args) error {
}

l.Debug("Downloading asset")
cl := http.Client{}
cl := githubapi.FromContext(ctx).Client()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, asset.URL, nil)
if err != nil {
return errors.WithStack(err)
Expand Down

0 comments on commit 2908cc9

Please sign in to comment.