github.com/mg98/scriptup@v0.1.0/main.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/mg98/scriptup/pkg/scriptup"
     5  	"github.com/urfave/cli/v2"
     6  	"log"
     7  	"os"
     8  )
     9  
    10  func main() {
    11  	app := NewApp()
    12  	err := app.Run(os.Args)
    13  	if err != nil {
    14  		log.Fatal(err)
    15  	}
    16  }
    17  
    18  // NewApp creates a new command line app
    19  func NewApp() *cli.App {
    20  	app := cli.NewApp()
    21  	app.Name = "scriptup"
    22  	app.Usage = "A lightweight and agnostic script migration tool."
    23  	app.Version = "0.1.0"
    24  
    25  	app.Flags = []cli.Flag{
    26  		&cli.StringFlag{
    27  			Name:    "env",
    28  			Aliases: []string{"e"},
    29  			Value:   "dev",
    30  			Usage:   "specify which configuration to use",
    31  		},
    32  	}
    33  
    34  	app.Commands = []*cli.Command{
    35  		{
    36  			Name:    "new",
    37  			Aliases: []string{"n"},
    38  			Usage:   "Generate a new migration file",
    39  			Action: func(c *cli.Context) error {
    40  				cfg := scriptup.GetConfig(c.String("env"))
    41  				return scriptup.NewMigrationFile(cfg, c.Args().Get(0))
    42  			},
    43  		},
    44  		{
    45  			Name:    "up",
    46  			Aliases: []string{"u"},
    47  			Flags: []cli.Flag{
    48  				&cli.IntFlag{
    49  					Name:  "steps",
    50  					Value: -1,
    51  					Usage: "Limit the number of migrations to run",
    52  				},
    53  			},
    54  			Usage: "Execute recent scripts that have not been migrated yet",
    55  			Action: func(c *cli.Context) error {
    56  				cfg := scriptup.GetConfig(c.String("env"))
    57  				return scriptup.MigrateUp(cfg, c.Int("steps"))
    58  			},
    59  		},
    60  		{
    61  			Name:    "down",
    62  			Aliases: []string{"d"},
    63  			Flags: []cli.Flag{
    64  				&cli.IntFlag{
    65  					Name:  "steps",
    66  					Value: -1,
    67  					Usage: "Limit the number of migrations to run",
    68  				},
    69  			},
    70  			Usage: "Undo recently performed migrations",
    71  			Action: func(c *cli.Context) error {
    72  				cfg := scriptup.GetConfig(c.String("env"))
    73  				return scriptup.MigrateDown(cfg, c.Int("steps"))
    74  			},
    75  		},
    76  		{
    77  			Name:    "status",
    78  			Aliases: []string{"s"},
    79  			Usage:   "Get status about open migrations and which was run last",
    80  			Action: func(c *cli.Context) error {
    81  				cfg := scriptup.GetConfig(c.String("env"))
    82  				return scriptup.Status(cfg)
    83  			},
    84  		},
    85  	}
    86  
    87  	return app
    88  }