Skip to content

Commit

Permalink
refactor: add cliui package (#2411)
Browse files Browse the repository at this point in the history
* cliui 1st iteration

* cliui refactor network commands

* fix lint

* configure bus channel size with option

* fix events tests

* add spinner options

* revert import formatting

* fix events tests again

* fix spinner

* do review fixes

Co-authored-by: Petr Ivanov <[email protected]>
  • Loading branch information
ivanovpetr and Petr Ivanov authored May 5, 2022
1 parent 42921ac commit 5f502e1
Show file tree
Hide file tree
Showing 57 changed files with 1,004 additions and 730 deletions.
4 changes: 2 additions & 2 deletions ignite/cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/ignite-hq/cli/ignite/pkg/cliquiz"
"github.com/ignite-hq/cli/ignite/pkg/cliui/cliquiz"
"github.com/ignite-hq/cli/ignite/pkg/cliui/entrywriter"
"github.com/ignite-hq/cli/ignite/pkg/cosmosaccount"
"github.com/ignite-hq/cli/ignite/pkg/entrywriter"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/account_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/cosmos/go-bip39"
"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/cliquiz"
"github.com/ignite-hq/cli/ignite/pkg/cliui/cliquiz"
"github.com/ignite-hq/cli/ignite/pkg/cosmosaccount"
)

Expand Down
9 changes: 5 additions & 4 deletions ignite/cmd/chain_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/chaincmd"
"github.com/ignite-hq/cli/ignite/pkg/cliui/colors"
"github.com/ignite-hq/cli/ignite/services/chain"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ Sample usages:
return c
}

func chainBuildHandler(cmd *cobra.Command, args []string) error {
func chainBuildHandler(cmd *cobra.Command, _ []string) error {
var (
isRelease, _ = cmd.Flags().GetBool(flagRelease)
releaseTargets, _ = cmd.Flags().GetStringSlice(flagReleaseTargets)
Expand Down Expand Up @@ -77,7 +78,7 @@ func chainBuildHandler(cmd *cobra.Command, args []string) error {
return err
}

fmt.Printf("🗃 Release created: %s\n", infoColor(releasePath))
fmt.Printf("🗃 Release created: %s\n", colors.Info(releasePath))

return nil
}
Expand All @@ -88,10 +89,10 @@ func chainBuildHandler(cmd *cobra.Command, args []string) error {
}

if output == "" {
fmt.Printf("🗃 Installed. Use with: %s\n", infoColor(binaryName))
fmt.Printf("🗃 Installed. Use with: %s\n", colors.Info(binaryName))
} else {
binaryPath := filepath.Join(output, binaryName)
fmt.Printf("🗃 Binary built at the path: %s\n", infoColor(binaryPath))
fmt.Printf("🗃 Binary built at the path: %s\n", colors.Info(binaryPath))
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions ignite/cmd/chain_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/chaincmd"
"github.com/ignite-hq/cli/ignite/pkg/cliui/colors"
"github.com/ignite-hq/cli/ignite/services/chain"
)

Expand All @@ -23,7 +24,7 @@ func NewChainInit() *cobra.Command {
return c
}

func chainInitHandler(cmd *cobra.Command, args []string) error {
func chainInitHandler(cmd *cobra.Command, _ []string) error {
chainOption := []chain.Option{
chain.LogLevel(logLevel(cmd)),
chain.KeyringBackend(chaincmd.KeyringBackendTest),
Expand All @@ -47,7 +48,7 @@ func chainInitHandler(cmd *cobra.Command, args []string) error {
return err
}

fmt.Printf("🗃 Initialized. Checkout your chain's home (data) directory: %s\n", infoColor(home))
fmt.Printf("🗃 Initialized. Checkout your chain's home (data) directory: %s\n", colors.Info(home))

return nil
}
29 changes: 2 additions & 27 deletions ignite/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ import (
"path/filepath"
"sort"
"strings"
"sync"
"time"

"github.com/fatih/color"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/cosmosaccount"
"github.com/ignite-hq/cli/ignite/pkg/cosmosver"
"github.com/ignite-hq/cli/ignite/pkg/events"
"github.com/ignite-hq/cli/ignite/pkg/gitpod"
"github.com/ignite-hq/cli/ignite/pkg/goenv"
"github.com/ignite-hq/cli/ignite/pkg/xgenny"
"github.com/ignite-hq/cli/ignite/services/chain"
"github.com/ignite-hq/cli/ignite/services/scaffolder"
"github.com/ignite-hq/cli/ignite/version"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
)

const (
Expand All @@ -35,8 +31,6 @@ const (
checkVersionTimeout = time.Millisecond * 600
)

var infoColor = color.New(color.FgYellow).SprintFunc()

// New creates a new root command for `Ignite CLI` with its sub commands.
func New(ctx context.Context) *cobra.Command {
cobra.EnableCommandSorting = false
Expand Down Expand Up @@ -82,25 +76,6 @@ func logLevel(cmd *cobra.Command) chain.LogLvl {
return chain.LogRegular
}

func printEvents(wg *sync.WaitGroup, bus events.Bus, s *clispinner.Spinner) {
defer wg.Done()

for event := range bus {
switch event.Status {
case events.StatusOngoing:
s.SetText(event.Text())
s.Start()
case events.StatusDone:
icon := event.Icon
if icon == "" {
icon = clispinner.OK
}
s.Stop()
fmt.Printf("%s %s\n", icon, event.Text())
}
}
}

func flagSetPath(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(flagPath, "p", ".", "path of the app")
}
Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_dart.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/cliui/clispinner"
"github.com/ignite-hq/cli/ignite/services/chain"
)

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/cliui/clispinner"
"github.com/ignite-hq/cli/ignite/services/chain"
)

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/cliui/clispinner"
"github.com/ignite-hq/cli/ignite/services/chain"
)

Expand Down
2 changes: 1 addition & 1 deletion ignite/cmd/generate_vuex.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/cliui/clispinner"
"github.com/ignite-hq/cli/ignite/services/chain"
)

Expand Down
52 changes: 23 additions & 29 deletions ignite/cmd/network.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package ignitecmd

import (
"sync"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/cosmosaccount"
"github.com/ignite-hq/cli/ignite/pkg/cosmosclient"
"github.com/ignite-hq/cli/ignite/pkg/events"
Expand Down Expand Up @@ -68,46 +65,49 @@ func NewNetwork() *cobra.Command {

var cosmos *cosmosclient.Client

type NetworkBuilder struct {
AccountRegistry cosmosaccount.Registry
Spinner *clispinner.Spinner
type (
NetworkBuilderOption func(builder *NetworkBuilder)

ev events.Bus
wg *sync.WaitGroup
cmd *cobra.Command
cc cosmosclient.Client
}
NetworkBuilder struct {
AccountRegistry cosmosaccount.Registry

func newNetworkBuilder(cmd *cobra.Command) (NetworkBuilder, error) {
var err error
ev events.Bus
cmd *cobra.Command
cc cosmosclient.Client
}
)

n := NetworkBuilder{
Spinner: clispinner.New(),
ev: events.NewBus(),
wg: &sync.WaitGroup{},
cmd: cmd,
func CollectEvents(ev events.Bus) NetworkBuilderOption {
return func(builder *NetworkBuilder) {
builder.ev = ev
}
}

n.wg.Add(1)
go printEvents(n.wg, n.ev, n.Spinner)
func newNetworkBuilder(cmd *cobra.Command, options ...NetworkBuilderOption) (NetworkBuilder, error) {
var (
err error
n = NetworkBuilder{cmd: cmd}
)

if n.cc, err = getNetworkCosmosClient(cmd); err != nil {
n.Cleanup()
return NetworkBuilder{}, err
}

n.AccountRegistry = n.cc.AccountRegistry

for _, apply := range options {
apply(&n)
}
return n, nil
}

func (n NetworkBuilder) Chain(source networkchain.SourceOption, options ...networkchain.Option) (*networkchain.Chain, error) {
options = append(options, networkchain.CollectEvents(n.ev))

if home := getHome(n.cmd); home != "" {
options = append(options, networkchain.WithHome(home))
}

options = append(options, networkchain.CollectEvents(n.ev))

return networkchain.New(n.cmd.Context(), n.AccountRegistry, source, options...)
}

Expand All @@ -129,12 +129,6 @@ func (n NetworkBuilder) Network(options ...network.Option) (network.Network, err
return network.New(*cosmos, account, options...), nil
}

func (n NetworkBuilder) Cleanup() {
n.Spinner.Stop()
n.ev.Shutdown()
n.wg.Wait()
}

func getNetworkCosmosClient(cmd *cobra.Command) (cosmosclient.Client, error) {
// check preconfigured networks
if nightly && local {
Expand Down
59 changes: 22 additions & 37 deletions ignite/cmd/network_campaign_account.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package ignitecmd

import (
"bytes"
"context"
"fmt"
"strconv"

"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"github.com/ignite-hq/cli/ignite/pkg/clispinner"
"github.com/ignite-hq/cli/ignite/pkg/entrywriter"
"github.com/ignite-hq/cli/ignite/pkg/cliui"
"github.com/ignite-hq/cli/ignite/pkg/cliui/icons"
"github.com/ignite-hq/cli/ignite/services/network"
"github.com/ignite-hq/cli/ignite/services/network/networktypes"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

var (
Expand Down Expand Up @@ -44,42 +41,33 @@ func newNetworkCampaignAccountList() *cobra.Command {
}

func newNetworkCampaignAccountListHandler(cmd *cobra.Command, args []string) error {
nb, campaignID, err := networkChainLaunch(cmd, args)
session := cliui.New()
defer session.Cleanup()

nb, campaignID, err := networkChainLaunch(cmd, args, session)
if err != nil {
return err
}
defer nb.Cleanup()

n, err := nb.Network()
if err != nil {
return err
}

accountSummary := &bytes.Buffer{}

// get all campaign accounts
mainnetAccs, vestingAccs, err := getAccounts(cmd.Context(), n, campaignID)
if err != nil {
return err
}

if len(mainnetAccs)+len(vestingAccs) == 0 {
session.StopSpinner()
return session.Printf("%s %s\n", icons.Info, "no campaign account found")
}

mainnetAccEntries := make([][]string, 0)
for _, acc := range mainnetAccs {
mainnetAccEntries = append(mainnetAccEntries, []string{
acc.Address,
acc.Shares.String(),
})
mainnetAccEntries = append(mainnetAccEntries, []string{acc.Address, acc.Shares.String()})
}
if len(mainnetAccEntries) > 0 {
if err = entrywriter.MustWrite(
accountSummary,
campaignMainnetsAccSummaryHeader,
mainnetAccEntries...,
); err != nil {
return err
}
}

mainnetVestingAccEntries := make([][]string, 0)
for _, acc := range vestingAccs {
mainnetVestingAccEntries = append(mainnetVestingAccEntries, []string{
Expand All @@ -89,22 +77,19 @@ func newNetworkCampaignAccountListHandler(cmd *cobra.Command, args []string) err
strconv.FormatInt(acc.EndTime, 10),
})
}

session.StopSpinner()
if len(mainnetAccEntries) > 0 {
if err = session.PrintTable(campaignMainnetsAccSummaryHeader, mainnetAccEntries...); err != nil {
return err
}
}
if len(mainnetVestingAccEntries) > 0 {
if err = entrywriter.MustWrite(
accountSummary,
campaignVestingAccSummaryHeader,
mainnetVestingAccEntries...,
); err != nil {
if err = session.PrintTable(campaignVestingAccSummaryHeader, mainnetVestingAccEntries...); err != nil {
return err
}
}

nb.Spinner.Stop()
if accountSummary.Len() > 0 {
fmt.Print(accountSummary.String())
} else {
fmt.Printf("%s %s\n", clispinner.Info, "no campaign account found")
}
return nil
}

Expand Down
Loading

0 comments on commit 5f502e1

Please sign in to comment.