Skip to content

Commit

Permalink
feat: skip if already installed
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Nov 10, 2024
1 parent d3ef006 commit ff63d2d
Show file tree
Hide file tree
Showing 17 changed files with 580 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ signs:
checksum:
name_template: "checksums.txt"
snapshot:
name_template: '{{ trimprefix .Summary "v" }}'
version_template: '{{ trimprefix .Summary "v" }}'
# We are skipping changelog because we are using semantic release
changelog:
disable: true
6 changes: 3 additions & 3 deletions pkg/commands/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func Execute(c *cli.Context) error {
log.Infof(" arch: %s", runtime.GOARCH)
fmt.Println("")
log.Infof("configuration")
log.Infof(" home: %s", cfg.HomePath)
log.Infof(" home: %s", cfg.Path)
log.Infof(" bin: %s", cfg.BinPath)
log.Infof(" opt: %s", cfg.OptPath)
log.Infof(" opt: %s", cfg.GetOptPath())
log.Infof(" cache: %s", cfg.CachePath)
fmt.Println("")
log.Warnf("To cleanup all of distillery, remove the following directories:")
log.Warnf(" - %s", cfg.GetCachePath())
log.Warnf(" - %s", cfg.BinPath)
log.Warnf(" - %s", cfg.OptPath)
log.Warnf(" - %s", cfg.GetOptPath())

return nil
}
Expand Down
48 changes: 41 additions & 7 deletions pkg/commands/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ekristen/distillery/pkg/common"
"github.com/ekristen/distillery/pkg/config"
"github.com/ekristen/distillery/pkg/inventory"
"github.com/ekristen/distillery/pkg/provider"
)

Expand All @@ -32,6 +33,8 @@ func Execute(c *cli.Context) error {
_ = c.Set("include-pre-releases", "true")
}

inv := inventory.New(os.DirFS(cfg.BinPath), cfg.BinPath, cfg.GetOptPath(), cfg)

src, err := NewSource(c.Args().First(), &provider.Options{
OS: c.String("os"),
Arch: c.String("arch"),
Expand All @@ -40,6 +43,7 @@ func Execute(c *cli.Context) error {
"version": c.String("version"),
"github-token": c.String("github-token"),
"gitlab-token": c.String("gitlab-token"),
"no-signature-verify": c.String("no-signature-verify"),
"no-checksum-verify": c.Bool("no-checksum-verify"),
"no-score-check": c.Bool("no-score-check"),
"include-pre-releases": c.Bool("include-pre-releases"),
Expand All @@ -49,15 +53,42 @@ func Execute(c *cli.Context) error {
return err
}

var userFlags []string
if c.Bool("include-pre-releases") {
userFlags = append(userFlags, "including pre-releases")
}

log.Infof("distillery/%s", common.AppVersion.Summary)
log.Infof(" source: %s", src.GetSource())
log.Infof(" app: %s", src.GetApp())
log.Infof("version: %s", c.String("version"))
log.Infof(" os: %s", c.String("os"))
log.Infof(" arch: %s", c.String("arch"))
for _, flag := range userFlags {
log.Infof(" flag: %s", flag)
}

if c.Bool("include-pre-releases") {
log.Infof("including pre-releases")
log.Infof("source: %s", src.GetSource())
log.Infof("app: %s", src.GetApp())
log.Infof("os: %s", c.String("os"))
log.Infof("arch: %s", c.String("arch"))

if c.String("version") == common.Latest {
log.Infof("determining latest version")
} else {
log.Infof("version: %s", c.String("version"))
}

if err := src.PreRun(c.Context); err != nil {
return err
}

if c.String("version") == common.Latest {
log.Infof("version: %s", src.GetVersion())
}

if c.String("version") == "latest" && !c.Bool("force") {
latestInstalled := inv.GetLatestVersion(fmt.Sprintf("%s/%s", src.GetSource(), src.GetApp()))
if latestInstalled.Version == src.GetVersion() {
log.Warnf("already installed")
log.Infof("reinstall with --force (%s)", time.Since(start))
return nil
}
}

if err := src.Run(c.Context); err != nil {
Expand Down Expand Up @@ -183,6 +214,9 @@ func Flags() []cli.Flag {
Name: "no-score-check",
Usage: "disable scoring check",
},
&cli.BoolFlag{
Name: "force",
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Execute(c *cli.Context) error {
func init() {
cmd := &cli.Command{
Name: "list",
Usage: "list",
Usage: "list installed binaries and versions",
Description: `list installed binaries and versions`,
Before: common.Before,
Flags: common.Flags(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Execute(c *cli.Context) error {
return err
}

path := filepath.Join(cfg.OptPath, src.GetSource(), src.GetOwner(), src.GetRepo())
path := filepath.Join(cfg.GetOptPath(), src.GetSource(), src.GetOwner(), src.GetRepo())

logrus.Trace("path: ", path)

Expand Down
6 changes: 6 additions & 0 deletions pkg/common/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package common

const (
Unknown = "unknown"
Latest = "latest"
)
25 changes: 11 additions & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ import (
)

type Config struct {
// HomePath - path to store the configuration files, this path is set by default based on the operating system type
// Path - path to store the configuration files, this path is set by default based on the operating system type
// and your user's home directory. Typically, this is set to $HOME/.distillery
HomePath string `yaml:"home_path" toml:"home_path"`
Path string `yaml:"path" toml:"path"`

// BinPath - path to create symlinks for your binaries, this path is set by default based on the operating system type
// This is the path that is added to your PATH environment variable. Typically, this is set to $HOME/.distillery/bin
// This allows you to override the location for symlinks. For example, you can instead put them all in /usr/local/bin
BinPath string `yaml:"bin_path" toml:"bin_path"`

// OptPath - path to store the binaries that are installed, this path is set by default based on the operating
// system type. This is where the symlinks in the BinPath point to. Typically, this is set to $HOME/.distillery/opt
OptPath string `yaml:"opt_path" toml:"opt_path"`

// CachePath - path to store cache files, this path is set by default based on the operating system type
CachePath string `yaml:"cache_path" toml:"cache_path"`

Expand Down Expand Up @@ -55,8 +52,12 @@ func (c *Config) GetDownloadsPath() string {
return filepath.Join(c.CachePath, common.NAME, "downloads")
}

func (c *Config) GetOptPath() string {
return filepath.Join(c.Path, "opt")
}

func (c *Config) MkdirAll() error {
paths := []string{c.BinPath, c.OptPath, c.CachePath, c.GetMetadataPath(), c.GetDownloadsPath()}
paths := []string{c.BinPath, c.GetOptPath(), c.CachePath, c.GetMetadataPath(), c.GetDownloadsPath()}

for _, path := range paths {
err := os.MkdirAll(path, 0755)
Expand Down Expand Up @@ -102,12 +103,12 @@ func New(path string) (*Config, error) {
cfg.DefaultSource = "github"
}

if cfg.HomePath == "" {
if cfg.Path == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return cfg, err
}
cfg.HomePath = filepath.Join(homeDir, fmt.Sprintf(".%s", common.NAME))
cfg.Path = filepath.Join(homeDir, fmt.Sprintf(".%s", common.NAME))
}

if cfg.CachePath == "" {
Expand All @@ -119,11 +120,7 @@ func New(path string) (*Config, error) {
}

if cfg.BinPath == "" {
cfg.BinPath = filepath.Join(cfg.HomePath, "bin")
}

if cfg.OptPath == "" {
cfg.OptPath = filepath.Join(cfg.HomePath, "opt")
cfg.BinPath = filepath.Join(cfg.Path, "bin")
}

return cfg, nil
Expand Down
22 changes: 22 additions & 0 deletions pkg/inventory/bin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package inventory

import "path/filepath"

type Bin struct {
Name string
Versions []*Version
Source string
Owner string
Repo string
}

func (b *Bin) GetInstallPath(base string) string {
return filepath.Join(base, b.Source, b.Owner, b.Repo)
}

type Version struct {
Version string
Path string
Latest bool
Target string
}
Loading

0 comments on commit ff63d2d

Please sign in to comment.