Skip to content

Commit

Permalink
feat: add b binary (selfupdate) (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
fentas authored Apr 3, 2024
1 parent de05d62 commit 9c62970
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Have a look into [pkg/binary](./pkg/binary/) for more details.
Have a look into [pkg/binaries](./pkg/binaries/) for prepackaged binaries.

- [argsh](https://github.com/arg-sh/argsh) - Utilities for Bash script quality
- `b` - (Selfupdate) Manage and execute binary files
- [docker-compose](https://github.com/docker/compose) - Define and run multi-container Docker applications
- [gh](https://github.com/cli/cli) - GitHub CLI wrapper
- [hcloud](https://github.com/hetznercloud/cli) - Hetzner Cloud CLI wrapper
Expand Down
6 changes: 4 additions & 2 deletions cmd/b/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/buyoio/b/pkg/binaries"
"github.com/buyoio/b/pkg/binaries/argsh"
"github.com/buyoio/b/pkg/binaries/b"
compose "github.com/buyoio/b/pkg/binaries/docker-compose"
"github.com/buyoio/b/pkg/binaries/gh"
"github.com/buyoio/b/pkg/binaries/hcloud"
Expand All @@ -26,8 +27,8 @@ import (

// Magic variables set by goreleaser
var (
version = "1.0.0" // x-release-please-version
versionPreRelease = "dev"
version = "v1.0.0" // x-release-please-version
versionPreRelease = ""
)

func main() {
Expand All @@ -37,6 +38,7 @@ func main() {
root := cli.NewCmdBinary(&cli.CmdBinaryOptions{
Binaries: []*binary.Binary{
argsh.Binary(o),
b.Binary(o),
compose.Binary(o),
gh.Binary(o),
hcloud.Binary(o),
Expand Down
33 changes: 33 additions & 0 deletions pkg/binaries/b/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package b

import (
"context"
"fmt"
"runtime"

"github.com/buyoio/b/pkg/binaries"
"github.com/buyoio/b/pkg/binary"
)

func Binary(options *binaries.BinaryOptions) *binary.Binary {
if options == nil {
options = &binaries.BinaryOptions{
Context: context.Background(),
}
}
// https://github.com/buyoio/b/releases/download/v1.0.0/b-linux-amd64.tar.gz
return &binary.Binary{
Context: options.Context,
Envs: options.Envs,
Tracker: options.Tracker,
Version: options.Version,
Name: "b",
GitHubRepo: "buyoio/b",
GitHubFile: fmt.Sprintf("b-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH),
VersionF: binary.GithubLatest,
IsTarGz: true,
VersionLocalF: func(b *binary.Binary) (string, error) {
return b.Exec("--version")
},
}
}
19 changes: 18 additions & 1 deletion pkg/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,22 @@ func (b *Binary) DownloadBinary() error {
if err != nil {
return err
}
return b.downloadBinary()
path := b.BinaryPath()
if ex, err := os.Executable(); err != nil || ex != path {
return b.downloadBinary()
}
// Self update
old := path + ".old"
err = os.Rename(path, old)
if err != nil {
return err
}
defer os.Remove(old)

err = b.downloadBinary()
if err != nil {
// Rollback
_ = os.Rename(old, path)
}
return err
}

0 comments on commit 9c62970

Please sign in to comment.