github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/client/cli/store/util.go (about)

     1  package cli
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/tickoalcantara12/micro/v3/service/store"
     7  	"github.com/pkg/errors"
     8  	"github.com/urfave/cli/v2"
     9  )
    10  
    11  // makeStore is a helper function that creates a store for snapshot and restore
    12  func makeStore(ctx *cli.Context) (store.Store, error) {
    13  	builtinStore, err := getStore(ctx.String("store"))
    14  	if err != nil {
    15  		return nil, errors.Wrap(err, "makeStore")
    16  	}
    17  	s := builtinStore(
    18  		store.Nodes(strings.Split(ctx.String("nodes"), ",")...),
    19  		store.Database(ctx.String("database")),
    20  		store.Table(ctx.String("table")),
    21  	)
    22  	if err := s.Init(); err != nil {
    23  		return nil, errors.Wrapf(err, "Couldn't init %s store", ctx.String("store"))
    24  	}
    25  	return s, nil
    26  }
    27  
    28  // makeStores is a helper function that sets up 2 stores for sync
    29  func makeStores(ctx *cli.Context) (store.Store, store.Store, error) {
    30  	fromBuilder, err := getStore(ctx.String("from-backend"))
    31  	if err != nil {
    32  		return nil, nil, errors.Wrap(err, "from store")
    33  	}
    34  	toBuilder, err := getStore(ctx.String("to-backend"))
    35  	if err != nil {
    36  		return nil, nil, errors.Wrap(err, "to store")
    37  	}
    38  	from := fromBuilder(
    39  		store.Nodes(strings.Split(ctx.String("from-nodes"), ",")...),
    40  		store.Database(ctx.String("from-database")),
    41  		store.Table(ctx.String("from-table")),
    42  	)
    43  	if err := from.Init(); err != nil {
    44  		return nil, nil, errors.Wrapf(err, "from: couldn't init %s", ctx.String("from-backend"))
    45  	}
    46  	to := toBuilder(
    47  		store.Nodes(strings.Split(ctx.String("to-nodes"), ",")...),
    48  		store.Database(ctx.String("to-database")),
    49  		store.Table(ctx.String("to-table")),
    50  	)
    51  	if err := to.Init(); err != nil {
    52  		return nil, nil, errors.Wrapf(err, "to: couldn't init %s", ctx.String("to-backend"))
    53  	}
    54  	return from, to, nil
    55  }
    56  
    57  func getStore(s string) (func(...store.StoreOption) store.Store, error) {
    58  	// builtinStore, exists := cmd.DefaultStores[s]
    59  	// if !exists {
    60  	// 	return nil, errors.Errorf("store %s is not an implemented store - check your plugins", s)
    61  	// }
    62  	// return builtinStore, nil
    63  	return nil, nil
    64  }