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

     1  // Package cli implements the `micro store` subcommands
     2  // for example:
     3  //   micro store snapshot
     4  //   micro store restore
     5  //   micro store sync
     6  package cli
     7  
     8  import (
     9  	"github.com/tickoalcantara12/micro/v3/cmd"
    10  	"github.com/tickoalcantara12/micro/v3/util/helper"
    11  	"github.com/urfave/cli/v2"
    12  )
    13  
    14  func init() {
    15  	cmd.Register(&cli.Command{
    16  		Name:   "store",
    17  		Usage:  "Commands for accessing the store",
    18  		Action: helper.UnexpectedSubcommand,
    19  		Subcommands: []*cli.Command{
    20  			{
    21  				Name:      "read",
    22  				Usage:     "read a record from the store",
    23  				UsageText: `micro store read [options] key`,
    24  				Action:    read,
    25  				Flags: []cli.Flag{
    26  					&cli.StringFlag{
    27  						Name:    "database",
    28  						Aliases: []string{"d"},
    29  						Usage:   "database to write to",
    30  						Value:   "micro",
    31  					},
    32  					&cli.StringFlag{
    33  						Name:    "table",
    34  						Aliases: []string{"t"},
    35  						Usage:   "table to write to",
    36  						Value:   "micro",
    37  					},
    38  					&cli.BoolFlag{
    39  						Name:    "prefix",
    40  						Aliases: []string{"p"},
    41  						Usage:   "read prefix",
    42  						Value:   false,
    43  					},
    44  					&cli.BoolFlag{
    45  						Name:    "suffix",
    46  						Aliases: []string{"s"},
    47  						Usage:   "read suffix",
    48  						Value:   false,
    49  					},
    50  					&cli.UintFlag{
    51  						Name:    "limit",
    52  						Aliases: []string{"l"},
    53  						Usage:   "list limit",
    54  					},
    55  					&cli.StringFlag{
    56  						Name:  "order",
    57  						Usage: "Set the order of records e.g asc or desc",
    58  					},
    59  					&cli.UintFlag{
    60  						Name:    "offset",
    61  						Aliases: []string{"o"},
    62  						Usage:   "list offset",
    63  					},
    64  					&cli.BoolFlag{
    65  						Name:    "verbose",
    66  						Aliases: []string{"v"},
    67  						Usage:   "show keys and headers (only values shown by default)",
    68  						Value:   false,
    69  					},
    70  					&cli.StringFlag{
    71  						Name:  "output",
    72  						Usage: "output format (json, table)",
    73  						Value: "table",
    74  					},
    75  				},
    76  			},
    77  			{
    78  				Name:      "list",
    79  				Usage:     "list all keys from a store",
    80  				UsageText: `micro store list [options]`,
    81  				Action:    list,
    82  				Flags: []cli.Flag{
    83  					&cli.StringFlag{
    84  						Name:    "database",
    85  						Aliases: []string{"d"},
    86  						Usage:   "database to list from",
    87  						Value:   "micro",
    88  					},
    89  					&cli.StringFlag{
    90  						Name:    "table",
    91  						Aliases: []string{"t"},
    92  						Usage:   "table to write to",
    93  						Value:   "micro",
    94  					},
    95  					&cli.StringFlag{
    96  						Name:  "output",
    97  						Usage: "output format (json)",
    98  					},
    99  					&cli.StringFlag{
   100  						Name:  "order",
   101  						Usage: "Set the order of records e.g asc or desc",
   102  					},
   103  					&cli.BoolFlag{
   104  						Name:    "prefix",
   105  						Aliases: []string{"p"},
   106  						Usage:   "list prefix",
   107  						Value:   false,
   108  					},
   109  					&cli.UintFlag{
   110  						Name:    "limit",
   111  						Aliases: []string{"l"},
   112  						Usage:   "list limit",
   113  					},
   114  					&cli.UintFlag{
   115  						Name:    "offset",
   116  						Aliases: []string{"o"},
   117  						Usage:   "list offset",
   118  					},
   119  				},
   120  			},
   121  			{
   122  				Name:      "write",
   123  				Usage:     "write a record to the store",
   124  				UsageText: `micro store write [options] key value`,
   125  				Action:    write,
   126  				Flags: []cli.Flag{
   127  					&cli.StringFlag{
   128  						Name:    "expiry",
   129  						Aliases: []string{"e"},
   130  						Usage:   "expiry in time.ParseDuration format",
   131  						Value:   "",
   132  					},
   133  					&cli.StringFlag{
   134  						Name:    "database",
   135  						Aliases: []string{"d"},
   136  						Usage:   "database to write to",
   137  						Value:   "micro",
   138  					},
   139  					&cli.StringFlag{
   140  						Name:    "table",
   141  						Aliases: []string{"t"},
   142  						Usage:   "table to write to",
   143  						Value:   "micro",
   144  					},
   145  				},
   146  			},
   147  			{
   148  				Name:      "delete",
   149  				Usage:     "delete a key from the store",
   150  				UsageText: `micro store delete [options] key`,
   151  				Action:    delete,
   152  				Flags: []cli.Flag{
   153  					&cli.StringFlag{
   154  						Name:  "database",
   155  						Usage: "database to delete from",
   156  						Value: "micro",
   157  					},
   158  					&cli.StringFlag{
   159  						Name:  "table",
   160  						Usage: "table to delete from",
   161  						Value: "micro",
   162  					},
   163  				},
   164  			},
   165  			{
   166  				Name:   "databases",
   167  				Usage:  "List all databases known to the store service",
   168  				Action: databases,
   169  				Flags: []cli.Flag{
   170  					&cli.StringFlag{
   171  						Name:  "store",
   172  						Usage: "store service to call",
   173  						Value: "store",
   174  					},
   175  				},
   176  			},
   177  			{
   178  				Name:   "tables",
   179  				Usage:  "List all tables in the specified database known to the store service",
   180  				Action: tables,
   181  				Flags: []cli.Flag{
   182  					&cli.StringFlag{
   183  						Name:  "store",
   184  						Usage: "store service to call",
   185  						Value: "store",
   186  					},
   187  					&cli.StringFlag{
   188  						Name:    "database",
   189  						Aliases: []string{"d"},
   190  						Usage:   "database to list tables of",
   191  						Value:   "micro",
   192  					},
   193  				},
   194  			},
   195  			{
   196  				Name:   "snapshot",
   197  				Usage:  "Back up a store",
   198  				Action: snapshot,
   199  				Flags: append(CommonFlags,
   200  					&cli.StringFlag{
   201  						Name:    "destination",
   202  						Usage:   "Backup destination",
   203  						Value:   "file:///tmp/store-snapshot",
   204  						EnvVars: []string{"MICRO_SNAPSHOT_DESTINATION"},
   205  					},
   206  				),
   207  			},
   208  			{
   209  				Name:   "sync",
   210  				Usage:  "Copy all records of one store into another store",
   211  				Action: sync,
   212  				Flags:  SyncFlags,
   213  			},
   214  			{
   215  				Name:   "restore",
   216  				Usage:  "restore a store snapshot",
   217  				Action: restore,
   218  				Flags: append(CommonFlags,
   219  					&cli.StringFlag{
   220  						Name:  "source",
   221  						Usage: "Backup source",
   222  						Value: "file:///tmp/store-snapshot",
   223  					},
   224  				),
   225  			},
   226  		},
   227  	})
   228  }
   229  
   230  // CommonFlags are flags common to cli commands snapshot and restore
   231  var CommonFlags = []cli.Flag{
   232  	&cli.StringFlag{
   233  		Name:    "nodes",
   234  		Usage:   "Comma separated list of Nodes to pass to the store backend",
   235  		EnvVars: []string{"MICRO_STORE_NODES"},
   236  	},
   237  	&cli.StringFlag{
   238  		Name:    "database",
   239  		Usage:   "Database option to pass to the store backend",
   240  		EnvVars: []string{"MICRO_STORE_DATABASE"},
   241  	},
   242  	&cli.StringFlag{
   243  		Name:    "table",
   244  		Usage:   "Table option to pass to the store backend",
   245  		EnvVars: []string{"MICRO_STORE_TABLE"},
   246  	},
   247  }