vanity/cmd/add.go

80 lines
1.5 KiB
Go

package cmd
import (
"github.com/AlecAivazis/survey/v2"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver"
"go.jolheiser.com/vanity/modules/config"
)
var Add = cli.Command{
Name: "add",
Usage: "Add a package",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Overwrite existing package without prompt",
},
},
Action: doAdd,
}
func doAdd(ctx *cli.Context) error {
questions := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "Name"},
Validate: survey.Required,
},
{
Name: "path",
Prompt: &survey.Input{Message: "Path"},
Validate: survey.Required,
},
{
Name: "repo",
Prompt: &survey.Input{Message: "Repository HTTP(S) URL"},
Validate: survey.Required,
},
{
Name: "ssh",
Prompt: &survey.Input{Message: "Repository SSH URL"},
Validate: survey.Required,
},
{
Name: "description",
Prompt: &survey.Input{Message: "Description"},
Validate: survey.Required,
},
}
answers := struct {
Name string
Path string
Repo string
SSH string
Description string
}{}
if err := survey.Ask(questions, &answers); err != nil {
return err
}
pkg := config.Package{
Name: answers.Name,
Path: answers.Path,
Repo: answers.Repo,
SSH: answers.SSH,
Description: answers.Description,
}
config.AddPackages(ctx.Bool("force"), pkg)
if err := config.Save(); err != nil {
return err
}
beaver.Infof("Added `%s` to vanity.", pkg.Name)
return nil
}