Skip to content

Commit

Permalink
feat(cmd): add node type page to the startup assistant (#1431)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Jul 24, 2024
1 parent c54b926 commit 43dcd1b
Show file tree
Hide file tree
Showing 13 changed files with 749 additions and 382 deletions.
6 changes: 3 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,18 +634,18 @@ func MakeValidatorKey(walletInstance *wallet.Wallet, valAddrsInfo []vault.Addres
return valKeys, nil
}

func TerminalProgressBar(totalSize, barWidth int, showBytes bool) *progressbar.ProgressBar {
func TerminalProgressBar(totalSize int64, barWidth int) *progressbar.ProgressBar {
if barWidth < 15 {
barWidth = 15
}

opts := []progressbar.Option{
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowBytes(showBytes),
progressbar.OptionSetWidth(barWidth),
progressbar.OptionSetElapsedTime(false),
progressbar.OptionSetPredictTime(false),
progressbar.OptionShowDescriptionAtLineEnd(),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
Expand All @@ -655,5 +655,5 @@ func TerminalProgressBar(totalSize, barWidth int, showBytes bool) *progressbar.P
}),
}

return progressbar.NewOptions(totalSize, opts...)
return progressbar.NewOptions64(totalSize, opts...)
}
87 changes: 31 additions & 56 deletions cmd/daemon/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/gofrs/flock"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/util"
"github.com/spf13/cobra"
)
Expand All @@ -21,7 +19,7 @@ func buildImportCmd(parentCmd *cobra.Command) {
parentCmd.AddCommand(importCmd)

workingDirOpt := addWorkingDirOption(importCmd)
serverAddrOpt := importCmd.Flags().String("server-addr", "https://download.pactus.org",
serverAddrOpt := importCmd.Flags().String("server-addr", cmd.DefaultSnapshotURL,
"import server address")

importCmd.Run = func(c *cobra.Command, _ []string) {
Expand All @@ -46,39 +44,25 @@ func buildImportCmd(parentCmd *cobra.Command) {
return
}

storeDir, _ := filepath.Abs(conf.Store.StorePath())
if !util.IsDirNotExistsOrEmpty(storeDir) {
cmd.PrintErrorMsgf("The data directory is not empty: %s", conf.Store.StorePath())

return
}
cmd.PrintLine()

snapshotURL := *serverAddrOpt
importer, err := cmd.NewImporter(
gen.ChainType(),
snapshotURL,
conf.Store.DataPath(),
)
cmd.FatalErrorCheck(err)

switch gen.ChainType() {
case genesis.Mainnet:
snapshotURL += "/mainnet/"
case genesis.Testnet:
snapshotURL += "/testnet/"
case genesis.Localnet:
cmd.PrintErrorMsgf("Unsupported chain type: %s", gen.ChainType())

return
}

metadata, err := cmd.GetSnapshotMetadata(c.Context(), snapshotURL)
if err != nil {
cmd.PrintErrorMsgf("Failed to get snapshot metadata: %s", err)

return
}
metadata, err := importer.GetMetadata(c.Context())
cmd.FatalErrorCheck(err)

snapshots := make([]string, 0, len(metadata))

for _, m := range metadata {
item := fmt.Sprintf("snapshot %s (%s)",
parseTime(m.CreatedAt).Format("2006-01-02"),
util.FormatBytesToHumanReadable(m.TotalSize),
m.CreatedAtTime().Format("2006-01-02"),
util.FormatBytesToHumanReadable(m.Data.Size),
)

snapshots = append(snapshots, item)
Expand All @@ -89,34 +73,32 @@ func buildImportCmd(parentCmd *cobra.Command) {
choice := cmd.PromptSelect("Please select a snapshot", snapshots)

selected := metadata[choice]
tmpDir := util.TempDirPath()
extractPath := fmt.Sprintf("%s/data", tmpDir)

err = os.MkdirAll(extractPath, 0o750)
cmd.FatalErrorCheck(err)
cmd.TrapSignal(func() {
_ = fileLock.Unlock()
_ = importer.Cleanup()
})

cmd.PrintLine()

zipFileList := cmd.DownloadManager(
importer.Download(
c.Context(),
&selected,
snapshotURL,
tmpDir,
downloadProgressBar,
)

for _, zFile := range zipFileList {
err := cmd.ExtractAndStoreFile(zFile, extractPath)
cmd.FatalErrorCheck(err)
}
cmd.PrintLine()
cmd.PrintLine()
cmd.PrintInfoMsgf("Extracting files...")

err = os.MkdirAll(filepath.Dir(conf.Store.StorePath()), 0o750)
err = importer.ExtractAndStoreFiles()
cmd.FatalErrorCheck(err)

err = cmd.CopyAllFiles(extractPath, conf.Store.StorePath())
cmd.PrintInfoMsgf("Moving data...")
err = importer.MoveStore()
cmd.FatalErrorCheck(err)

err = os.RemoveAll(tmpDir)
err = importer.Cleanup()
cmd.FatalErrorCheck(err)

_ = fileLock.Unlock()
Expand All @@ -131,19 +113,12 @@ func buildImportCmd(parentCmd *cobra.Command) {
}

func downloadProgressBar(fileName string, totalSize, downloaded int64, _ float64) {
bar := cmd.TerminalProgressBar(int(totalSize), 30, true)
bar.Describe(fileName)
err := bar.Add(int(downloaded))
bar := cmd.TerminalProgressBar(totalSize, 30)
bar.Describe(fmt.Sprintf("%s (%s/%s)",
fileName,
util.FormatBytesToHumanReadable(uint64(downloaded)),
util.FormatBytesToHumanReadable(uint64(totalSize)),
))
err := bar.Add64(downloaded)
cmd.FatalErrorCheck(err)
}

func parseTime(dateString string) time.Time {
const layout = "2006-01-02T15:04:05.000000"

parsedTime, err := time.Parse(layout, dateString)
if err != nil {
return time.Time{}
}

return parsedTime
}
7 changes: 2 additions & 5 deletions cmd/daemon/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
fileLock := flock.New(lockFilePath)

locked, err := fileLock.TryLock()
if err != nil {
// handle unable to attempt to acquire lock
cmd.FatalErrorCheck(err)
}
cmd.FatalErrorCheck(err)

if !locked {
cmd.PrintWarnMsgf("Could not lock '%s', another instance is running?", lockFilePath)
Expand Down Expand Up @@ -114,7 +111,7 @@ func buildPruneCmd(parentCmd *cobra.Command) {
}

func pruningProgressBar(prunedCount, skippedCount, totalCount uint32) {
bar := cmd.TerminalProgressBar(int(totalCount), 30, false)
bar := cmd.TerminalProgressBar(int64(totalCount), 30)
bar.Describe(fmt.Sprintf("Pruned: %d | Skipped: %d", prunedCount, skippedCount))
err := bar.Add(int(prunedCount + skippedCount))
cmd.FatalErrorCheck(err)
Expand Down
5 changes: 1 addition & 4 deletions cmd/daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func buildStartCmd(parentCmd *cobra.Command) {
fileLock := flock.New(lockFilePath)

locked, err := fileLock.TryLock()
if err != nil {
// handle unable to attempt to acquire lock
cmd.FatalErrorCheck(err)
}
cmd.FatalErrorCheck(err)

if !locked {
cmd.PrintWarnMsgf("Could not lock '%s', another instance is running?", lockFilePath)
Expand Down
Loading

0 comments on commit 43dcd1b

Please sign in to comment.