Skip to content

Commit

Permalink
feat: clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Sep 2, 2024
1 parent 10164e5 commit a574fe4
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ekristen/distillery/pkg/common"

_ "github.com/ekristen/distillery/pkg/commands/clean"
_ "github.com/ekristen/distillery/pkg/commands/info"
_ "github.com/ekristen/distillery/pkg/commands/install"
_ "github.com/ekristen/distillery/pkg/commands/list"
Expand Down
115 changes: 115 additions & 0 deletions pkg/commands/clean/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package clean

import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/urfave/cli/v2"

"github.com/ekristen/distillery/pkg/common"
)

func Execute(c *cli.Context) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}

binDir := filepath.Join(homeDir, fmt.Sprintf(".%s", common.NAME), "bin")

sims := make(map[string]map[string]string)
targets := make([]string, 0)
bins := make([]string, 0)

_ = filepath.Walk(binDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

fileInfo, err := os.Lstat(path)
if err != nil {
return err
}

if fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
simpleName := info.Name()
version := "latest"
parts := strings.Split(info.Name(), "@")
if len(parts) > 1 {
simpleName = parts[0]
version = parts[1]
}

// Get the target of the symlink
target, err := os.Readlink(path)
if err != nil {
return err
}

targets = append(targets, target)

if _, ok := sims[simpleName]; !ok {
sims[simpleName] = make(map[string]string)
}

sims[simpleName][version] = path
} else {
bins = append(bins, path)
}

return nil
})

fmt.Println("orphaned binaries:")
for _, path := range bins {
found := false

for _, p := range targets {
if path == p {
found = true
break
}
}

if found {
continue
}

fmt.Println(" - ", path)

if c.Bool("no-dry-run") {
if err := os.Remove(path); err != nil {
return err
}
}
}

return nil
}

func Flags() []cli.Flag {
return []cli.Flag{
&cli.BoolFlag{
Name: "no-dry-run",
Usage: "Perform all actions",
},
}
}

func init() {
cmd := &cli.Command{
Name: "clean",
Usage: "clean",
Description: `cleanup`,
Flags: append(Flags(), common.Flags()...),
Action: Execute,
}

common.RegisterCommand(cmd)
}
5 changes: 0 additions & 5 deletions pkg/commands/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ import (
"runtime"

"github.com/apex/log"
clilog "github.com/apex/log/handlers/cli"

"github.com/urfave/cli/v2"

"github.com/ekristen/distillery/pkg/common"
)

func Execute(c *cli.Context) error {
log.SetHandler(clilog.Default)
log.SetLevel(log.DebugLevel)

homeDir, err := os.UserHomeDir()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/source/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (s *GitHub) FindRelease(ctx context.Context) error {
for _, r := range releases {
includePreReleases := s.Options.Settings["include-pre-releases"].(bool)
if includePreReleases && r.GetPrerelease() {
s.Version = strings.TrimPrefix(r.GetTagName(), "v")
release = r
break
}
Expand Down

0 comments on commit a574fe4

Please sign in to comment.