Skip to content

Commit

Permalink
add pin/unpin command
Browse files Browse the repository at this point in the history
  • Loading branch information
snwzd committed Nov 14, 2023
1 parent f7265c4 commit 47781e6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func newListCmd() *listCmd {
status = color.RedString("missing file")
}

if b.Pin {
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s", os.ExpandEnv(b.Path), "*"+b.Version, b.URL, status)
continue
}

fmt.Fprintf(w, "\n %s\t%s\t%s\t%s", os.ExpandEnv(b.Path), b.Version, b.URL, status)
}
fmt.Fprintf(w, "\n\n")
Expand Down
50 changes: 50 additions & 0 deletions cmd/pin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"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()

for _, b := range cfg.Bins {
for _, p := range args {
if b.RemoteName == p {
bin, err := getBinPath(p)
if err != nil {
return err
}
updatedCfg := cfg.Bins[bin]
updatedCfg.Pin = true

err = config.UpsertBinary(updatedCfg)
if err != nil {
return err
}
}

// TODO return error for unmatched ones
}
}

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 @@ -81,6 +81,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
50 changes: 50 additions & 0 deletions cmd/unpin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"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()

for _, b := range cfg.Bins {
for _, p := range args {
if b.RemoteName == p {
bin, err := getBinPath(p)
if err != nil {
return err
}
updatedCfg := cfg.Bins[bin]
updatedCfg.Pin = false

err = config.UpsertBinary(updatedCfg)
if err != nil {
return err
}
}

// TODO return error for unmatched ones
}
}

return nil
},
}

root.cmd = cmd
return root
}
3 changes: 3 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func newUpdateCmd() *updateCmd {
if err != nil {
return err
}
if cfg.Bins[bin].Pin {
return fmt.Errorf("Cannot update a pinned binary")
}
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"`
Pin bool `json:"pin"`
}

func CheckAndLoad() error {
Expand Down

0 comments on commit 47781e6

Please sign in to comment.