Skip to content

Commit

Permalink
feat: add progress bar to downloads (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs committed Sep 17, 2024
1 parent 828c3d2 commit e6d3b4b
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 30 deletions.
2 changes: 1 addition & 1 deletion cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func Cmd() *cobra.Command {
}
proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(false).
WithDefaultText(
"press 'y' when the wallets are funded funded",
"press 'y' when the wallets are funded",
).Show()
if !proceed {
return
Expand Down
9 changes: 5 additions & 4 deletions cmd/rollapp/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
cosmossdktypes "github.com/cosmos/cosmos-sdk/types"
github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types"
dymensionseqtypes "github.com/dymensionxyz/dymension/v3/x/sequencer/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
initrollapp "github.com/dymensionxyz/roller/cmd/rollapp/init"
Expand All @@ -31,9 +35,6 @@ import (
"github.com/dymensionxyz/roller/utils/filesystem"
"github.com/dymensionxyz/roller/utils/rollapp"
sequencerutils "github.com/dymensionxyz/roller/utils/sequencer"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

// TODO: Test sequencing on 35-C and update the price
Expand Down Expand Up @@ -240,7 +241,7 @@ func Cmd() *cobra.Command {
seqAddrInfo.Print(utils.WithName())
proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(false).
WithDefaultText(
"press 'y' when the wallets are funded funded",
"press 'y' when the wallets are funded",
).Show()

if !proceed {
Expand Down
7 changes: 3 additions & 4 deletions cmd/services/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"runtime"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

func RollappCmd() *cobra.Command {
Expand Down Expand Up @@ -86,7 +85,7 @@ func RelayerCmd() *cobra.Command {
pterm.Info.Printf(
"run %s to view the current status of the relayer\n",
pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()).
Sprintf("journalctl -fu relayer"),
Sprintf("roller relayer services logs"),
)
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func PrintInsufficientBalancesIfAny(
// TODO: to util
proceed, _ := pterm.DefaultInteractiveConfirm.WithDefaultValue(false).
WithDefaultText(
"press 'y' when the wallets are funded funded",
"press 'y' when the wallets are funded",
).Show()
if !proceed {
pterm.Info.Println("exiting")
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
Expand All @@ -172,13 +173,14 @@ require (
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rakyll/statik v0.1.7 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/rs/zerolog v1.32.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/schollz/progressbar/v3 v3.15.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down Expand Up @@ -210,7 +212,7 @@ require (
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jwilder/encoding v0.0.0-20170811194829-b4e1701a28ef/go.mod h1:Ct9fl0F6iIOGgxJ5npU/IUOhOhqlVrGjyIZc8/MagT0=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/karalabe/usb v0.0.2/go.mod h1:Od972xHfMJowv7NGVDiWVxk2zxnWgjLlJzE+F4F7AGU=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down Expand Up @@ -1175,6 +1176,8 @@ github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3/go.mod h1:RagcQ7I8Ie
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand Down Expand Up @@ -1363,6 +1366,8 @@ github.com/retailnext/hllpp v1.0.1-0.20180308014038-101a6d2f8b52/go.mod h1:RDpi1
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand All @@ -1389,6 +1394,8 @@ github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWR
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71efZx0=
github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM=
github.com/schollz/progressbar/v3 v3.15.0 h1:cNZmcNiVyea6oofBTg80ZhVXxf3wG/JoAhqCCwopkQo=
github.com/schollz/progressbar/v3 v3.15.0/go.mod h1:ncBdc++eweU0dQoeZJ3loXoAc+bjaallHRIm8pVVeQM=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
Expand Down Expand Up @@ -2002,6 +2009,8 @@ golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
84 changes: 66 additions & 18 deletions utils/dependencies/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dependencies
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
Expand All @@ -11,6 +12,7 @@ import (
"strings"

"github.com/pterm/pterm"
"github.com/schollz/progressbar/v3"

"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/utils/archives"
Expand Down Expand Up @@ -191,7 +193,9 @@ func InstallBinaries(bech32 string, withMockDA bool) error {
}

func InstallBinaryFromRepo(dep types.Dependency, td string) error {
pterm.Debug.Printf("Installing %s\n", dep.Name)
spinner, _ := pterm.DefaultSpinner.Start(
fmt.Sprintf("Installing %s\n", dep.Name),
)
targetDir, err := os.MkdirTemp(os.TempDir(), td)
if err != nil {
return err
Expand All @@ -201,19 +205,19 @@ func InstallBinaryFromRepo(dep types.Dependency, td string) error {
// Clone the repository
err = os.Chdir(targetDir)
if err != nil {
pterm.Error.Println("failed to create a temp directory")
spinner.Fail("failed to create a temp directory")
return err
}

c := exec.Command("git", "clone", dep.Repository, targetDir)
_, err = bash.ExecCommandWithStdout(c)
if err != nil {
pterm.Error.Println("failed to clone")
spinner.Fail("failed to clone")
return err
}
// Change directory to the cloned repo
if err := os.Chdir(targetDir); err != nil {
pterm.Error.Println("failed to create a temp directory")
spinner.Fail("failed to create a temp directory")
return err
}

Expand All @@ -224,32 +228,40 @@ func InstallBinaryFromRepo(dep types.Dependency, td string) error {
}
}

pterm.Info.Printf(
"starting %s build from %s (this can take several minutes)\n",
dep.Name,
dep.Release,
spinner.UpdateText(
fmt.Sprintf(
"starting %s build from %s (this can take several minutes)\n",
dep.Name,
dep.Release,
),
)

// Build the binary
for _, binary := range dep.Binaries {
_, err := bash.ExecCommandWithStdout(binary.BuildCommand)
if err != nil {
spinner.Fail("failed to build")
return err
}

c := exec.Command("sudo", "mv", binary.Binary, binary.BinaryDestination)
if _, err := bash.ExecCommandWithStdout(c); err != nil {
spinner.Fail("failed to install")
return err
}
pterm.Success.Printf(
"Successfully installed %s\n", filepath.Base(binary.BinaryDestination),
spinner.UpdateText(
fmt.Sprintf("Successfully installed %s\n", filepath.Base(binary.BinaryDestination)),
)
}

spinner.Success(fmt.Sprintf("Successfully installed %s\n", dep.Name))
return nil
}

func InstallBinaryFromRelease(dep types.Dependency) error {
pterm.Debug.Printf("Installing %s\n", dep.Name)
spinner, _ := pterm.DefaultSpinner.Start(
fmt.Sprintf("Installing %s\n", dep.Name),
)
goOs := strings.Title(runtime.GOOS)
goArch := strings.ToLower(runtime.GOARCH)
if goArch == "amd64" && dep.Name == "celestia-app" {
Expand Down Expand Up @@ -277,26 +289,62 @@ func InstallBinaryFromRelease(dep types.Dependency) error {
archiveName,
)

err = DownloadRelease(url, targetDir, dep)
spinner.UpdateText(fmt.Sprintf("Downloading %s %s\n", dep.Name, dep.Release))
err = DownloadRelease(url, targetDir, dep, spinner)
if err != nil {
// nolint: errcheck,gosec
spinner.Fail("failed to download release")
return err
}
spinner.UpdateText(fmt.Sprintf("Successfully downloaded %s\n", dep.Name))

pterm.Success.Printf("Successfully installed %s\n", dep.Name)
spinner.Success(fmt.Sprintf("Successfully installed %s\n", dep.Name))
return nil
}

func DownloadRelease(url, destination string, dep types.Dependency) error {
// nolint gosec
resp, err := http.Get(url)
func DownloadRelease(
url, destination string,
dep types.Dependency,
spinner *pterm.SpinnerPrinter,
) error {
// Create a new HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}

// nolint errcheck
// Send the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
err = archives.ExtractTarGz(destination, resp.Body, dep)

// Create a progress bar
bar := progressbar.DefaultBytes(
resp.ContentLength,
"Downloading",
)

// Create a reader that will update the progress bar
reader := progressbar.NewReader(resp.Body, bar)

// Create a pointer to the reader
readerPtr := &reader

// Create a wrapper that implements io.ReadCloser
readCloserWrapper := struct {
io.Reader
io.Closer
}{
Reader: readerPtr,
Closer: resp.Body,
}

// nolint: errcheck,gosec
spinner.Stop()
// Extract the tar.gz file with progress
err = archives.ExtractTarGz(destination, readCloserWrapper, dep)
if err != nil {
return err
}
Expand Down

0 comments on commit e6d3b4b

Please sign in to comment.