Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pin/unpin command #180

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ bin list # List current binaries and it's versions
bin prune # Removes from the DB missing binaries
bin remove <bin>... # Deletes one or more binaries
bin update [bin]... # Scans binaries and prompts for update
bin pin <bin>... # Pins current version of one or more binaries
bin unpin <bin>... # Unpins one or more binaries
```

## FAQ
Expand Down
5 changes: 5 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func newListCmd() *listCmd {
status = color.RedString("missing file")
}

if b.Pinned {
fmt.Printf("\n%s %s %s %s", _rPad(p, pL), "*"+_rPad(b.Version, vL), _rPad(b.URL, uL), status)
continue
}

fmt.Printf("\n%s %s %s %s", _rPad(p, pL), _rPad(b.Version, vL), _rPad(b.URL, uL), status)
}
fmt.Print("\n\n")
Expand Down
63 changes: 63 additions & 0 deletions cmd/pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/marcosnils/bin/pkg/config"
"github.com/spf13/cobra"
)

type pinCmd struct {
cmd *cobra.Command
}

func newPinCmd() *pinCmd {
root := &pinCmd{}

cmd := &cobra.Command{
Use: "pin [<name> | <paths...>]",
Short: "Pins current version of the binaries",
SilenceUsage: true,
Args: cobra.MinimumNArgs(1),
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.Get()

binsToPin := map[string]*config.Binary{}

// To pin
if len(args) > 0 {
for _, a := range args {
bin, err := getBinPath(a)
if err != nil {
return err
}
binsToPin[a] = cfg.Bins[bin]
}
} else {
return nil
}

pinned := []string{}

// Pinning
for name, bin := range binsToPin {
bin.Pinned = true
err := config.UpsertBinary(bin)
if err != nil {
return err
}
pinned = append(pinned, name)
continue
}

log.Infof("Pinned " + strings.Join(pinned, " "))

return nil
},
}

root.cmd = cmd
return root
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func newRootCmd(version string, exit func(int)) *rootCmd {
newInstallCmd().cmd,
newEnsureCmd().cmd,
newUpdateCmd().cmd,
newPinCmd().cmd,
newUnpinCmd().cmd,
newRemoveCmd().cmd,
newListCmd().cmd,
newPruneCmd().cmd,
Expand Down
63 changes: 63 additions & 0 deletions cmd/unpin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"strings"

"github.com/apex/log"
"github.com/marcosnils/bin/pkg/config"
"github.com/spf13/cobra"
)

type unpinCmd struct {
cmd *cobra.Command
}

func newUnpinCmd() *unpinCmd {
root := &unpinCmd{}

cmd := &cobra.Command{
Use: "unpin [<name> | <paths...>]",
Short: "Unpins current version of the binaries",
SilenceUsage: true,
Args: cobra.MinimumNArgs(1),
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := config.Get()

binsToUnpin := map[string]*config.Binary{}

// To unpin
if len(args) > 0 {
for _, a := range args {
bin, err := getBinPath(a)
if err != nil {
return err
}
binsToUnpin[a] = cfg.Bins[bin]
}
} else {
return nil
}

unpinned := []string{}

// Unpinning
for name, bin := range binsToUnpin {
bin.Pinned = false
err := config.UpsertBinary(bin)
if err != nil {
return err
}
unpinned = append(unpinned, name)
continue
}

log.Infof("Unpinned " + strings.Join(unpinned, " "))

return nil
},
}

root.cmd = cmd
return root
}
4 changes: 4 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func newUpdateCmd() *updateCmd {
if err != nil {
return err
}
if cfg.Bins[bin].Pinned {
log.Infof("%s is a pinned binary", a)
continue
}
binsToProcess[bin] = cfg.Bins[bin]
}
} else {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Binary struct {
// the package path in config so we don't ask the user to select
// the path again when upgrading
PackagePath string `json:"package_path"`
Pinned bool `json:"pinned"`
}

func CheckAndLoad() error {
Expand Down
Loading