Skip to content

Commit

Permalink
fix: protect against one part install targets
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Jul 22, 2024
1 parent 9176d80 commit 0ddf21d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
5 changes: 4 additions & 1 deletion pkg/commands/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Execute(c *cli.Context) error {
_ = os.MkdirAll(metadataDir, 0755)
_ = os.MkdirAll(downloadsDir, 0755)

source := source2.New(c.Args().First(), &source2.Options{
source, err := source2.New(c.Args().First(), &source2.Options{
OS: c.String("os"),
Arch: c.String("arch"),
HomeDir: homeDir,
Expand All @@ -40,6 +40,9 @@ func Execute(c *cli.Context) error {
MetadataDir: metadataDir,
DownloadsDir: downloadsDir,
})
if err != nil {
return err
}

fmt.Println(" source: ", source.GetSource())
fmt.Println(" app: ", source.GetApp())
Expand Down
2 changes: 2 additions & 0 deletions pkg/osconfig/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func New(os, arch string) *OS {
switch arch {
case AMD64:
newOS.Architectures = append(newOS.Architectures, "x86_64", "64bit", "64")
case ARM64:
newOS.Architectures = append(newOS.Architectures, "aarch64")
}

return newOS
Expand Down
1 change: 1 addition & 0 deletions pkg/source/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *GitHub) Run(ctx context.Context, version, githubToken string) error {

s.client = github.NewClient(httpcache.NewTransport(diskcache.New(cacheFile)).Client())
if githubToken != "" {
logrus.Debug("auth token provided")
s.client = s.client.WithAuthToken(githubToken)
}

Expand Down
22 changes: 14 additions & 8 deletions pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package source

import (
"context"
"fmt"
"strings"

"github.com/ekristen/distillery/pkg/asset"
Expand Down Expand Up @@ -88,7 +89,7 @@ func (s *Source) Cleanup() error {
return s.Binary.Cleanup()
}

func New(source string, opts *Options) ISource {
func New(source string, opts *Options) (ISource, error) {
version := "latest"
versionParts := strings.Split(source, "@")
if len(versionParts) > 1 {
Expand All @@ -97,48 +98,53 @@ func New(source string, opts *Options) ISource {
}

parts := strings.Split(source, "/")

if len(parts) == 1 {
return nil, fmt.Errorf("invalid install source, expect format of owner/repo or owner/repo@version")
}

if len(parts) == 2 {
// could be github or homebrew or hashicorp
if parts[0] == "homebrew" {
return &Homebrew{
Source: Source{Options: opts},
Formula: parts[1],
Version: version,
}
}, nil
} else if parts[0] == "hashicorp" {
return &Hashicorp{
Source: Source{Options: opts},
Owner: parts[1],
Repo: parts[1],
Version: version,
}
}, nil
}

return &GitHub{
Source: Source{Options: opts},
Owner: parts[0],
Repo: parts[1],
Version: version,
}
}, nil
} else if len(parts) >= 3 {
if strings.HasPrefix(parts[0], "github") {
return &GitHub{
Source: Source{Options: opts},
Owner: parts[1],
Repo: parts[2],
Version: version,
}
}, nil
} else if strings.HasPrefix(parts[0], "gitlab") {
return &GitLab{
Source: Source{Options: opts},
Owner: parts[1],
Repo: parts[2],
Version: version,
}
}, nil
}

return nil
return nil, nil
}

return nil
return nil, nil
}

0 comments on commit 0ddf21d

Please sign in to comment.