github.com/pluralsh/plural-cli@v0.9.5/cmd/plural/bundle.go (about)

     1  package plural
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/urfave/cli"
     7  
     8  	"github.com/pluralsh/plural-cli/pkg/api"
     9  	"github.com/pluralsh/plural-cli/pkg/bundle"
    10  	"github.com/pluralsh/plural-cli/pkg/manifest"
    11  	"github.com/pluralsh/plural-cli/pkg/utils"
    12  )
    13  
    14  func (p *Plural) bundleCommands() []cli.Command {
    15  	return []cli.Command{
    16  		{
    17  			Name:      "list",
    18  			Usage:     "lists bundles for a repository",
    19  			ArgsUsage: "REPO",
    20  			Action:    latestVersion(rooted(requireArgs(p.bundleList, []string{"repo"}))),
    21  		},
    22  		{
    23  			Name:      "install",
    24  			Usage:     "installs a bundle and writes the configuration to this installation's context",
    25  			ArgsUsage: "REPO NAME",
    26  			Flags: []cli.Flag{
    27  				cli.BoolFlag{
    28  					Name:  "refresh",
    29  					Usage: "re-enter the configuration for this bundle",
    30  				},
    31  			},
    32  			Action: tracked(latestVersion(rooted(p.bundleInstall)), "bundle.install"),
    33  		},
    34  	}
    35  }
    36  
    37  func (p *Plural) stackCommands() []cli.Command {
    38  	return []cli.Command{
    39  		{
    40  			Name:      "install",
    41  			Usage:     "installs a plural stack for your current provider",
    42  			ArgsUsage: "NAME",
    43  			Flags: []cli.Flag{
    44  				cli.BoolFlag{
    45  					Name:  "refresh",
    46  					Usage: "re-enter the configuration for all bundles",
    47  				},
    48  			},
    49  			Action: tracked(latestVersion(rooted(requireArgs(p.stackInstall, []string{"stack-name"}))), "stack.install"),
    50  		},
    51  		{
    52  			Name:  "list",
    53  			Usage: "lists stacks to potentially install",
    54  			Flags: []cli.Flag{
    55  				cli.BoolTFlag{
    56  					Name:  "account",
    57  					Usage: "only list stacks within your account",
    58  				},
    59  			},
    60  			Action: latestVersion(rooted(p.stackList)),
    61  		},
    62  	}
    63  }
    64  
    65  func (p *Plural) bundleList(c *cli.Context) error {
    66  	repo := c.Args().Get(0)
    67  	p.InitPluralClient()
    68  	recipes, err := p.listRecipes(repo)
    69  	if err != nil {
    70  		return api.GetErrorResponse(err, "ListRecipes")
    71  	}
    72  
    73  	headers := []string{"Name", "Description", "Provider", "Install Command"}
    74  	return utils.PrintTable(recipes, headers, func(recipe *api.Recipe) ([]string, error) {
    75  		return []string{recipe.Name, recipe.Description, recipe.Provider, fmt.Sprintf("plural bundle install %s %s", repo, recipe.Name)}, nil
    76  	})
    77  }
    78  
    79  func (p *Plural) bundleInstall(c *cli.Context) (err error) {
    80  	args := c.Args()
    81  	p.InitPluralClient()
    82  	repo := args.Get(0)
    83  	if repo == "" {
    84  		return fmt.Errorf("REPO argument required, try running `plural bundle install REPO` for the app you want to install")
    85  	}
    86  
    87  	bdl := args.Get(1)
    88  	if bdl == "" {
    89  		recipes, err := p.listRecipes(args.Get(0))
    90  		if err != nil {
    91  			return err
    92  		}
    93  		for _, recipe := range recipes {
    94  			if recipe.Primary {
    95  				bdl = recipe.Name
    96  				break
    97  			}
    98  		}
    99  
   100  		if bdl == "" {
   101  			return fmt.Errorf("you need to specify a bundle name, run `plural bundle list %s` to find eligible bundles then `plural bundle install %s <name>` to install", repo, repo)
   102  		}
   103  	}
   104  
   105  	err = bundle.Install(p.Client, repo, bdl, c.Bool("refresh"))
   106  	utils.Note("To edit the configuration you've just entered, edit the context.yaml file at the root of your repo, or run with the --refresh flag\n")
   107  	return
   108  }
   109  
   110  func (p *Plural) stackInstall(c *cli.Context) (err error) {
   111  	name := c.Args().Get(0)
   112  	man, err := manifest.FetchProject()
   113  	if err != nil {
   114  		return
   115  	}
   116  
   117  	p.InitPluralClient()
   118  	err = bundle.Stack(p.Client, name, man.Provider, c.Bool("refresh"))
   119  	utils.Note("To edit the configuration you've just entered, edit the context.yaml file at the root of your repo, or run with the --refresh flag\n")
   120  	return
   121  }
   122  
   123  func (p *Plural) stackList(c *cli.Context) (err error) {
   124  	p.InitPluralClient()
   125  	stacks, err := p.ListStacks(c.Bool("account"))
   126  	if err != nil {
   127  		return api.GetErrorResponse(err, "ListStacks")
   128  	}
   129  
   130  	headers := []string{"Name", "Description", "Featured"}
   131  	return utils.PrintTable(stacks, headers, func(s *api.Stack) ([]string, error) {
   132  		return []string{s.Name, s.Description, fmt.Sprintf("%v", s.Featured)}, nil
   133  	})
   134  }
   135  
   136  func (p *Plural) listRecipes(repo string) (res []*api.Recipe, err error) {
   137  	man, err := manifest.FetchProject()
   138  	if err != nil {
   139  		return
   140  	}
   141  	res, err = p.ListRecipes(repo, man.Provider)
   142  	return
   143  }