Skip to content

Commit

Permalink
feat: select which applications to install
Browse files Browse the repository at this point in the history
  • Loading branch information
5n7-sk committed Dec 6, 2020
1 parent 8feb9a1 commit 1b3b396
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
38 changes: 37 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion cmd/vin/cmd/cmd_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
8 changes: 8 additions & 0 deletions vin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1b3b396

Please sign in to comment.