diff --git a/cli.go b/cli.go index e3cac5a..23df3de 100644 --- a/cli.go +++ b/cli.go @@ -33,8 +33,36 @@ func (c *CLI) defaultTokenPath() (string, error) { return filepath.Join(cfg, "vin", "token.json"), nil } +func (c *CLI) selectApps(v Vin) ([]App, error) { + // allApps is a map for referencing applications by repository name + allApps := make(map[string]App) + for _, app := range v.Apps { + allApps[app.Repo] = app + } + + repos := make([]string, 0) + prompt := &survey.MultiSelect{ + Message: "select applications to install", + Options: v.repos(), + } + if err := survey.AskOne(prompt, &repos); err != nil { + return nil, err + } + + apps := make([]App, 0) + for _, repo := range repos { + apps = append(apps, allApps[repo]) + } + return apps, nil +} + +// CLIOptions represents options for the CIL. +type CLIOptions struct { + SelectApps bool +} + // Run runs the CLI. -func (c *CLI) Run() error { +func (c *CLI) Run(opt CLIOptions) error { configPath, err := c.defaultConfigPath() if err != nil { return err @@ -50,6 +78,14 @@ func (c *CLI) Run() error { return err } + if opt.SelectApps { + apps, err := c.selectApps(*v) + if err != nil { + return err + } + v.Apps = apps + } + for _, app := range v.Apps { urls := app.suitableAssetURLs() if len(urls) == 0 { diff --git a/cmd/vin/cmd/cmd_get.go b/cmd/vin/cmd/cmd_get.go index dccc2c7..dcc4954 100644 --- a/cmd/vin/cmd/cmd_get.go +++ b/cmd/vin/cmd/cmd_get.go @@ -5,9 +5,14 @@ import ( "github.com/spf13/cobra" ) +var selectApps bool + func runGet(cmd *cobra.Command, args []string) error { cli := vin.NewCLI() - return cli.Run() + opt := vin.CLIOptions{ + SelectApps: selectApps, + } + return cli.Run(opt) } var getCmd = &cobra.Command{ @@ -18,5 +23,7 @@ var getCmd = &cobra.Command{ } func init() { //nolint:gochecknoinits + getCmd.Flags().BoolVarP(&selectApps, "select", "s", false, "select applications to install") + rootCmd.AddCommand(getCmd) } diff --git a/vin.go b/vin.go index f404193..6915ebe 100644 --- a/vin.go +++ b/vin.go @@ -99,3 +99,11 @@ func New(configPath, tokenPath string) (*Vin, error) { } return &v, nil } + +func (v *Vin) repos() []string { + r := make([]string, 0) + for _, app := range v.Apps { + r = append(r, app.Repo) + } + return r +}