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 authored 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.