Skip to content

Commit

Permalink
feat!: change bin names to params not flags (#4)
Browse files Browse the repository at this point in the history
Instead of `b -i --jq` we do `b -i jq`
  • Loading branch information
fentas authored Apr 2, 2024
1 parent 2c56b60 commit a2a4663
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/cmd/b/main.go",
"args": ["-aui"]
"args": ["-h"]
}
]
}
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ The package includes a `Binary` struct that represents a binary file, including
### 🐾 How to use `b` as binary

```bash
# List all installed binaries and defined in b.yaml
# List all installed binaries and/or defined in b.yaml
b --all

# Print as JSON
b -ao json

# Install all binaries
# Install all binaries defined in b.yaml
b -a --install

# Install or update jq
b -iu --jq
b -iu jq

# Force install jq, overwriting existing binary
b -fi jq

# Upgrade all binaries
b -aiu
Expand Down Expand Up @@ -95,7 +98,7 @@ The `Binary` struct represents a binary file, including its name, file path, ver

```go
bin := binary.Binary{Name: "mybinary", Version: "1.0.0"}
bin.EnsureBinary()
bin.EnsureBinary(true, false)
```

Have a look into [pkg/binary](./pkg/binary/) for more details.
Expand Down
5 changes: 5 additions & 0 deletions pkg/binaries/k9s/k9s.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func Binary(options *binaries.BinaryOptions) *binary.Binary {
Context: context.Background(),
}
}
if options.Envs == nil {
options.Envs = map[string]string{}
}
return &binary.Binary{
Context: options.Context,
Envs: options.Envs,
Expand All @@ -32,6 +35,8 @@ func Binary(options *binaries.BinaryOptions) *binary.Binary {
VersionF: binary.GithubLatest,
IsTarGz: true,
VersionLocalF: func(b *binary.Binary) (string, error) {
// If this is not set, k9s will fail...
b.Envs["K9S_LOGS_DIR"] = "/tmp"
s, err := b.Exec("version", "-s")
if err != nil {
return "", err
Expand Down
13 changes: 6 additions & 7 deletions pkg/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package binary

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

Expand Down Expand Up @@ -94,21 +93,21 @@ func (b *Binary) BinaryExists() bool {
}

func (b *Binary) EnsureBinary(update bool) error {
exists := b.BinaryExists()
if b.File == "" {
return fmt.Errorf("unable to determine binary path")
}

if exists {
if b.BinaryExists() {
if !update {
return nil
}
local := b.LocalBinary()

if local.Version == local.Enforced || local.Enforced == "" && local.Latest == local.Version {
return nil
}
}

return b.DownloadBinary()
}

func (b *Binary) DownloadBinary() error {
err := os.MkdirAll(filepath.Dir(b.File), 0755)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/binary/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (b *Binary) extractSingleFileFromTarGz(stream io.Reader) error {
}

func (b *Binary) downloadBinary() error {
path := b.BinaryPath()
if path == "" {
return fmt.Errorf("unable to determine binary path")
}
var err error
if b.Version == "" && b.VersionF != nil {
b.Version, err = b.VersionF(b)
Expand Down
31 changes: 25 additions & 6 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ type CmdBinaryOptions struct {
all bool
available bool
ensure map[*binary.Binary]*bool
lookup map[string]*binary.Binary
force bool
update bool
install bool
check bool
}
Expand All @@ -34,16 +36,18 @@ func NewCmdBinary(options *CmdBinaryOptions) *cobra.Command {
options = &CmdBinaryOptions{}
}
options.ensure = make(map[*binary.Binary]*bool)
options.lookup = make(map[string]*binary.Binary)
for _, b := range options.Binaries {
options.ensure[b] = new(bool)
options.lookup[b.Name] = b
}

configExample := ""
if !options.NoConfig {
configExample = " and defined in b.yaml"
}
cmd := &cobra.Command{
Use: "b",
Use: "b [flags] [...binaries]",
Short: "Manage all binaries",
Long: "Ensure that all binaries needed are installed and up to date",
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -69,7 +73,10 @@ func NewCmdBinary(options *CmdBinaryOptions) *cobra.Command {
b -a --install
# Install or update jq
b -iu --jq
b -iu jq
# Force install jq, overwriting existing binary
b -fi jq
# Upgrade all binaries
b -aiu
Expand Down Expand Up @@ -100,10 +107,8 @@ func (o *CmdBinaryOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.available, "list", false, "List all available binaries")
}
cmd.Flags().BoolVarP(&o.all, "all", "a", false, all)
for _, b := range o.Binaries {
cmd.Flags().BoolVar(o.ensure[b], b.Name, false, b.Name+" binary")
}
cmd.Flags().BoolVarP(&o.force, "upgrade", "u", false, "Upgrade if already installed")
cmd.Flags().BoolVarP(&o.force, "force", "f", false, "Force download, overwriting existing binaries")
cmd.Flags().BoolVarP(&o.update, "upgrade", "u", false, "Upgrade if already installed")
cmd.Flags().BoolVarP(&o.install, "install", "i", false, "Install if not installed")
cmd.Flags().BoolVarP(&o.check, "check", "c", false, "Check if binary is up to date")
}
Expand All @@ -113,6 +118,20 @@ func (o *CmdBinaryOptions) Complete(cmd *cobra.Command, args []string) error {
return nil
}

if len(args) > 0 {
if o.all {
return cmdutil.UsageErrorf(cmd, "Cannot use --all with arguments")
}

for _, arg := range args {
b, ok := o.lookup[arg]
if !ok {
return cmdutil.UsageErrorf(cmd, "Unknown binary %s", arg)
}
*o.ensure[b] = true
}
}

if o.config != nil {
for _, lb := range *o.config {
for b, do := range o.ensure {
Expand Down
12 changes: 10 additions & 2 deletions pkg/cli/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,22 @@ func (o *CmdBinaryOptions) installBinaries() error {
for b, do := range o.ensure {
if *do {
wg.Add(1)
tracker := pw.AddTracker(fmt.Sprintf("Ensuring %s is installed", b.Name), 0)

go func() {
tracker := pw.AddTracker(fmt.Sprintf("Ensuring %s is installed", b.Name), 0)
b.Tracker = tracker

var err error
if o.force {
err = b.DownloadBinary()
} else {
err = b.EnsureBinary(o.update)
}

progress.ProgressDone(
tracker,
fmt.Sprintf("%s is installed", b.Name),
b.EnsureBinary(o.force),
err,
)
wg.Done()
}()
Expand Down

0 comments on commit a2a4663

Please sign in to comment.