github.com/orteth01/up@v0.2.0/internal/cli/stack/stack.go (about)

     1  package stack
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/tj/go-prompt"
     8  	"github.com/tj/kingpin"
     9  
    10  	"github.com/apex/log"
    11  	"github.com/apex/up/internal/cli/root"
    12  	"github.com/apex/up/internal/stats"
    13  	"github.com/apex/up/internal/util"
    14  )
    15  
    16  func init() {
    17  	cmd := root.Command("stack", "Stack resource management.")
    18  
    19  	cmd.Example(`up stack`, "Show status of the stack resources.")
    20  	cmd.Example(`up stack plan`, "Show resource changes.")
    21  	cmd.Example(`up stack apply`, "Apply resource changes.")
    22  	cmd.Example(`up stack delete`, "Delete the stack resources.")
    23  
    24  	plan(cmd)
    25  	apply(cmd)
    26  	delete(cmd)
    27  	status(cmd)
    28  }
    29  
    30  func plan(cmd *kingpin.CmdClause) {
    31  	c := cmd.Command("plan", "Plan configuration changes.")
    32  	c.Example(`up stack plan`, "Show changes planned.")
    33  
    34  	c.Action(func(_ *kingpin.ParseContext) error {
    35  		c, p, err := root.Init()
    36  		if err != nil {
    37  			return errors.Wrap(err, "initializing")
    38  		}
    39  
    40  		stats.Track("Plan Stack", nil)
    41  
    42  		// TODO: multi-region
    43  		return p.PlanStack(c.Regions[0])
    44  	})
    45  }
    46  
    47  func apply(cmd *kingpin.CmdClause) {
    48  	c := cmd.Command("apply", "Apply configuration changes.")
    49  	c.Example(`up stack apply`, "Apply the changes of the previous plan.")
    50  
    51  	c.Action(func(_ *kingpin.ParseContext) error {
    52  		c, p, err := root.Init()
    53  		if err != nil {
    54  			return errors.Wrap(err, "initializing")
    55  		}
    56  
    57  		stats.Track("Apply Stack", nil)
    58  
    59  		// TODO: multi-region
    60  		return p.ApplyStack(c.Regions[0])
    61  	})
    62  }
    63  
    64  func delete(cmd *kingpin.CmdClause) {
    65  	c := cmd.Command("delete", "Delete configured resources.")
    66  	c.Example(`up stack delete`, "Delete stack with confirmation prompt.")
    67  	c.Example(`up stack delete --force`, "Delete stack without confirmation prompt.")
    68  	c.Example(`up stack delete --async`, "Don't wait for deletion to complete.")
    69  	c.Example(`up stack delete -fa`, "Force asynchronous deletion.")
    70  
    71  	force := c.Flag("force", "Skip the confirmation prompt.").Short('f').Bool()
    72  	async := c.Flag("async", "Perform deletion asynchronously.").Short('a').Bool()
    73  
    74  	c.Action(func(_ *kingpin.ParseContext) error {
    75  		c, p, err := root.Init()
    76  		if err != nil {
    77  			return errors.Wrap(err, "initializing")
    78  		}
    79  
    80  		wait := !*async
    81  		defer util.Pad()()
    82  
    83  		stats.Track("Delete Stack", map[string]interface{}{
    84  			"force": *force,
    85  			"wait":  wait,
    86  		})
    87  
    88  		if !*force && !prompt.Confirm("  Really destroy the stack %q?  ", c.Name) {
    89  			fmt.Printf("\n")
    90  			log.Info("aborting")
    91  			return nil
    92  		}
    93  
    94  		// TODO: multi-region
    95  		return p.DeleteStack(c.Regions[0], wait)
    96  	})
    97  }
    98  
    99  func status(cmd *kingpin.CmdClause) {
   100  	c := cmd.Command("status", "Show status of resources.").Default()
   101  
   102  	c.Action(func(_ *kingpin.ParseContext) error {
   103  		c, p, err := root.Init()
   104  		if err != nil {
   105  			return errors.Wrap(err, "initializing")
   106  		}
   107  
   108  		stats.Track("Show Stack", nil)
   109  
   110  		// TODO: multi-region
   111  		return p.ShowStack(c.Regions[0])
   112  	})
   113  }