3 Commits

Author SHA1 Message Date
43baf8024a Return error if db.JSON decode hook gets an invalid type
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-01-01 19:28:16 -08:00
8a1d0f4f54 Add installmisc target for non-binary files
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2023-01-01 19:17:06 -08:00
806f49c472 Move CLI helper functions into internal/cliutils
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-12-29 12:25:37 -08:00
7 changed files with 145 additions and 120 deletions

View File

@@ -6,8 +6,10 @@ lure: version.txt
clean:
rm -f lure
install: lure
install: lure installmisc
install -Dm755 lure $(DESTDIR)$(PREFIX)/bin/lure
installmisc:
install -Dm755 scripts/completion/bash $(DESTDIR)$(PREFIX)/share/bash-completion/completions/lure
install -Dm755 scripts/completion/zsh $(DESTDIR)$(PREFIX)/share/zsh/site-functions/_lure

View File

@@ -41,6 +41,7 @@ import (
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/distro"
"go.arsenm.dev/lure/download"
"go.arsenm.dev/lure/internal/cliutils"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/cpu"
"go.arsenm.dev/lure/internal/repos"
@@ -186,13 +187,13 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
return nil, nil, err
}
err = promptViewScript(script, vars.Name)
err = cliutils.PromptViewScript(script, vars.Name, cfg.PagerStyle)
if err != nil {
log.Fatal("Failed to prompt user to view build script").Err(err).Send()
}
if !archMatches(vars.Architectures) {
buildAnyway, err := yesNoPrompt("Your system's CPU architecture doesn't match this package. Do you want to build anyway?", true)
buildAnyway, err := cliutils.YesNoPrompt("Your system's CPU architecture doesn't match this package. Do you want to build anyway?", true)
if err != nil {
return nil, nil, err
}
@@ -267,7 +268,7 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
}
log.Info("Installing build dependencies").Send()
installPkgs(ctx, flattenFoundPkgs(found, "install"), notFound, mgr)
installPkgs(ctx, cliutils.FlattenPkgs(found, "install"), notFound, mgr)
}
var builtDeps, builtNames, repoDeps []string
@@ -279,7 +280,7 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
return nil, nil, err
}
scripts := getScriptPaths(flattenFoundPkgs(found, "install"))
scripts := getScriptPaths(cliutils.FlattenPkgs(found, "install"))
for _, script := range scripts {
pkgPaths, pkgNames, err := buildPackage(ctx, script, mgr)
if err != nil {
@@ -490,7 +491,7 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
}
if len(buildDeps) > 0 {
removeBuildDeps, err := yesNoPrompt("Would you like to remove build dependencies?", false)
removeBuildDeps, err := cliutils.YesNoPrompt("Would you like to remove build dependencies?", false)
if err != nil {
return nil, nil, err
}

91
cli.go
View File

@@ -1,91 +0,0 @@
package main
import (
"os"
"github.com/AlecAivazis/survey/v2"
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/internal/db"
"go.arsenm.dev/lure/internal/pager"
)
// pkgPrompt asks the user to choose between multiple packages.
// The user may choose multiple packages.
func pkgPrompt(options []db.Package, verb string) ([]db.Package, error) {
names := make([]string, len(options))
for i, option := range options {
names[i] = option.Repository + "/" + option.Name + " " + option.Version
}
prompt := &survey.MultiSelect{
Options: names,
Message: "Choose which package(s) to " + verb,
}
var choices []int
err := survey.AskOne(prompt, &choices)
if err != nil {
return nil, err
}
out := make([]db.Package, len(choices))
for i, choiceIndex := range choices {
out[i] = options[choiceIndex]
}
return out, nil
}
// yesNoPrompt asks the user a yes or no question, using def as the default answer
func yesNoPrompt(msg string, def bool) (bool, error) {
var answer bool
err := survey.AskOne(
&survey.Confirm{
Message: msg,
Default: def,
},
&answer,
)
return answer, err
}
func promptViewScript(script string, name string) error {
view, err := yesNoPrompt("Would you like to view the build script for "+name, false)
if err != nil {
return err
}
if view {
err = showScript(script, name)
if err != nil {
return err
}
cont, err := yesNoPrompt("Would you still like to continue?", false)
if err != nil {
return err
}
if !cont {
log.Fatal("User chose not to continue after reading script").Send()
}
}
return nil
}
func showScript(path, name string) error {
scriptFl, err := os.Open(path)
if err != nil {
return err
}
defer scriptFl.Close()
str, err := pager.SyntaxHighlightBash(scriptFl, cfg.PagerStyle)
if err != nil {
return err
}
pgr := pager.New(name, str)
return pgr.Run()
}

View File

@@ -26,6 +26,7 @@ import (
"github.com/urfave/cli/v2"
"go.arsenm.dev/lure/distro"
"go.arsenm.dev/lure/internal/cliutils"
"go.arsenm.dev/lure/internal/overrides"
"go.arsenm.dev/lure/internal/repos"
"gopkg.in/yaml.v3"
@@ -51,7 +52,7 @@ func infoCmd(c *cli.Context) error {
os.Exit(1)
}
pkgs := flattenFoundPkgs(found, "show")
pkgs := cliutils.FlattenPkgs(found, "show")
var names []string
all := c.Bool("all")

View File

@@ -25,6 +25,7 @@ import (
"go.arsenm.dev/logger/log"
"github.com/urfave/cli/v2"
"go.arsenm.dev/lure/internal/cliutils"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/db"
"go.arsenm.dev/lure/internal/repos"
@@ -52,7 +53,7 @@ func installCmd(c *cli.Context) error {
log.Fatal("Error finding packages").Err(err).Send()
}
installPkgs(c.Context, flattenFoundPkgs(found, "install"), notFound, mgr)
installPkgs(c.Context, cliutils.FlattenPkgs(found, "install"), notFound, mgr)
return nil
}
@@ -80,24 +81,6 @@ func getScriptPaths(pkgs []db.Package) []string {
return scripts
}
// flattenFoundPkgs attempts to flatten the map of slices of packages into a single slice
// of packages by prompting the users if multiple packages match.
func flattenFoundPkgs(found map[string][]db.Package, verb string) []db.Package {
var outPkgs []db.Package
for _, pkgs := range found {
if len(pkgs) > 1 {
choices, err := pkgPrompt(pkgs, verb)
if err != nil {
log.Fatal("Error prompting for choice of package").Send()
}
outPkgs = append(outPkgs, choices...)
} else if len(pkgs) == 1 {
outPkgs = append(outPkgs, pkgs[0])
}
}
return outPkgs
}
// installScripts builds and installs LURE build scripts
func installScripts(ctx context.Context, mgr manager.Manager, scripts []string) {
for _, script := range scripts {

114
internal/cliutils/prompt.go Normal file
View File

@@ -0,0 +1,114 @@
package cliutils
import (
"os"
"github.com/AlecAivazis/survey/v2"
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/internal/db"
"go.arsenm.dev/lure/internal/pager"
)
// YesNoPrompt asks the user a yes or no question, using def as the default answer
func YesNoPrompt(msg string, def bool) (bool, error) {
var answer bool
err := survey.AskOne(
&survey.Confirm{
Message: msg,
Default: def,
},
&answer,
)
return answer, err
}
// PromptViewScript asks the user if they'd like to see a script,
// shows it if they answer yes, then asks if they'd still like to
// continue, and exits if they answer no.
func PromptViewScript(script, name, style string) error {
view, err := YesNoPrompt("Would you like to view the build script for "+name, false)
if err != nil {
return err
}
if view {
err = ShowScript(script, name, style)
if err != nil {
return err
}
cont, err := YesNoPrompt("Would you still like to continue?", false)
if err != nil {
return err
}
if !cont {
log.Fatal("User chose not to continue after reading script").Send()
}
}
return nil
}
// ShowScript uses the built-in pager to display a script at a
// given path, in the given syntax highlighting style.
func ShowScript(path, name, style string) error {
scriptFl, err := os.Open(path)
if err != nil {
return err
}
defer scriptFl.Close()
str, err := pager.SyntaxHighlightBash(scriptFl, style)
if err != nil {
return err
}
pgr := pager.New(name, str)
return pgr.Run()
}
// FlattenPkgs attempts to flatten the a map of slices of packages into a single slice
// of packages by prompting the user if multiple packages match.
func FlattenPkgs(found map[string][]db.Package, verb string) []db.Package {
var outPkgs []db.Package
for _, pkgs := range found {
if len(pkgs) > 1 {
choices, err := PkgPrompt(pkgs, verb)
if err != nil {
log.Fatal("Error prompting for choice of package").Send()
}
outPkgs = append(outPkgs, choices...)
} else if len(pkgs) == 1 {
outPkgs = append(outPkgs, pkgs[0])
}
}
return outPkgs
}
// PkgPrompt asks the user to choose between multiple packages.
// The user may choose multiple packages.
func PkgPrompt(options []db.Package, verb string) ([]db.Package, error) {
names := make([]string, len(options))
for i, option := range options {
names[i] = option.Repository + "/" + option.Name + " " + option.Version
}
prompt := &survey.MultiSelect{
Options: names,
Message: "Choose which package(s) to " + verb,
}
var choices []int
err := survey.AskOne(prompt, &choices)
if err != nil {
return nil, err
}
out := make([]db.Package, len(choices))
for i, choiceIndex := range choices {
out[i] = options[choiceIndex]
}
return out, nil
}

View File

@@ -33,7 +33,7 @@ import (
"mvdan.cc/sh/v3/syntax"
)
var ErrInvalidType = errors.New("val must be a pointer to a struct")
var ErrNotPointerToStruct = errors.New("val must be a pointer to a struct")
type VarNotFoundError struct {
name string
@@ -43,6 +43,16 @@ func (nfe VarNotFoundError) Error() string {
return "required variable '" + nfe.name + "' could not be found"
}
type InvalidTypeError struct {
name string
vartype string
exptype string
}
func (ite InvalidTypeError) Error() string {
return "variable '" + ite.name + "' is of type " + ite.vartype + ", but " + ite.exptype + " is expected"
}
// Decoder provides methods for decoding variable values
type Decoder struct {
info *distro.OSRelease
@@ -70,6 +80,11 @@ func (d *Decoder) DecodeVar(name string, val any) error {
WeaklyTypedInput: true,
DecodeHook: mapstructure.DecodeHookFuncValue(func(from, to reflect.Value) (interface{}, error) {
if strings.Contains(to.Type().String(), "db.JSON") {
valType := to.FieldByName("Val").Type()
if !from.Type().AssignableTo(valType) {
return nil, InvalidTypeError{name, from.Type().String(), valType.String()}
}
to.FieldByName("Val").Set(from)
return to, nil
}
@@ -97,11 +112,11 @@ func (d *Decoder) DecodeVar(name string, val any) error {
func (d *Decoder) DecodeVars(val any) error {
valKind := reflect.TypeOf(val).Kind()
if valKind != reflect.Pointer {
return ErrInvalidType
return ErrNotPointerToStruct
} else {
elemKind := reflect.TypeOf(val).Elem().Kind()
if elemKind != reflect.Struct {
return ErrInvalidType
return ErrNotPointerToStruct
}
}