Compare commits

...

3 Commits

Author SHA1 Message Date
Elara 7f7701ad03 Add info command 2022-09-26 14:42:17 -07:00
Elara 503328bf11 Add scripts to package 2022-09-26 14:38:14 -07:00
Elara cd6990fe45 Remove unneeded WriteFunc() function 2022-09-26 14:07:13 -07:00
4 changed files with 155 additions and 33 deletions

View File

@ -66,6 +66,18 @@ type BuildVars struct {
Sources []string `sh:"sources"`
Checksums []string `sh:"checksums"`
Backup []string `sh:"backup"`
Scripts Scripts `sh:"scripts"`
}
type Scripts struct {
PreInstall string `sh:"preinstall"`
PostInstall string `sh:"postinstall"`
PreRemove string `sh:"preremove"`
PostRemove string `sh:"postinstall"`
PreUpgrade string `sh:"preupgrade"`
PostUpgrade string `sh:"postupgrade"`
PreTrans string `sh:"pretrans"`
PostTrans string `sh:"posttrans"`
}
func buildCmd(c *cli.Context) error {
@ -221,6 +233,8 @@ func buildPackage(ctx context.Context, script string, mgr manager.Manager) ([]st
},
}
setScripts(&vars, pkgInfo, filepath.Dir(script))
if pkgInfo.Arch == "arm" {
pkgInfo.Arch = checkARMVariant()
}
@ -393,6 +407,42 @@ func checkARMVariant() string {
}
}
func setScripts(vars *BuildVars, info *nfpm.Info, scriptDir string) {
if vars.Scripts.PreInstall != "" {
info.Scripts.PreInstall = filepath.Join(scriptDir, vars.Scripts.PreInstall)
}
if vars.Scripts.PostInstall != "" {
info.Scripts.PostInstall = filepath.Join(scriptDir, vars.Scripts.PostInstall)
}
if vars.Scripts.PreRemove != "" {
info.Scripts.PreRemove = filepath.Join(scriptDir, vars.Scripts.PreRemove)
}
if vars.Scripts.PostRemove != "" {
info.Scripts.PostRemove = filepath.Join(scriptDir, vars.Scripts.PostRemove)
}
if vars.Scripts.PreUpgrade != "" {
info.ArchLinux.Scripts.PreUpgrade = filepath.Join(scriptDir, vars.Scripts.PreUpgrade)
info.APK.Scripts.PreUpgrade = filepath.Join(scriptDir, vars.Scripts.PreUpgrade)
}
if vars.Scripts.PostUpgrade != "" {
info.ArchLinux.Scripts.PostUpgrade = filepath.Join(scriptDir, vars.Scripts.PostUpgrade)
info.APK.Scripts.PostUpgrade = filepath.Join(scriptDir, vars.Scripts.PostUpgrade)
}
if vars.Scripts.PreTrans != "" {
info.RPM.Scripts.PreTrans = filepath.Join(scriptDir, vars.Scripts.PreTrans)
}
if vars.Scripts.PostTrans != "" {
info.RPM.Scripts.PostTrans = filepath.Join(scriptDir, vars.Scripts.PostTrans)
}
}
func uniq(ss ...*[]string) {
for _, s := range ss {
slices.Sort(*s)

93
info.go Normal file
View File

@ -0,0 +1,93 @@
/*
* LURE - Linux User REpository
* Copyright (C) 2022 Arsen Musayelyan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"os"
"github.com/urfave/cli/v2"
"go.arsenm.dev/lure/distro"
"go.arsenm.dev/lure/internal/shutils/decoder"
"gopkg.in/yaml.v3"
"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
)
func infoCmd(c *cli.Context) error {
args := c.Args()
if args.Len() < 1 {
log.Fatalf("Command info expected at least 1 argument, got %d", args.Len()).Send()
}
info, err := distro.ParseOSRelease(c.Context)
if err != nil {
log.Fatal("Error parsing os-release").Err(err).Send()
}
found, err := findPkg(args.First())
if err != nil {
log.Fatal("Error finding package").Err(err).Send()
}
// if multiple are matched, only use the first one
script := found[0]
fl, err := os.Open(script)
if err != nil {
log.Fatal("Error opening script").Err(err).Send()
}
file, err := syntax.NewParser().Parse(fl, "lure.sh")
if err != nil {
log.Fatal("Error parsing script").Err(err).Send()
}
fl.Close()
env := genBuildEnv(info)
runner, err := interp.New(
interp.Env(expand.ListEnviron(env...)),
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
)
if err != nil {
log.Fatal("Error creating runner").Err(err).Send()
}
err = runner.Run(c.Context, file)
if err != nil {
log.Fatal("Error running script").Err(err).Send()
}
dec := decoder.New(info, runner)
var vars BuildVars
err = dec.DecodeVars(&vars)
if err != nil {
log.Fatal("Error decoding script variables").Err(err).Send()
}
err = yaml.NewEncoder(os.Stdout).Encode(vars)
if err != nil {
log.Fatal("Error encoding script variables").Err(err).Send()
}
return nil
}

View File

@ -22,7 +22,6 @@ import (
"context"
"errors"
"fmt"
"io"
"reflect"
"runtime"
"strings"
@ -37,13 +36,12 @@ import (
var ErrInvalidType = errors.New("val must be a pointer to a struct")
type NotFoundError struct {
stype string
name string
type VarNotFoundError struct {
name string
}
func (nfe NotFoundError) Error() string {
return "required " + nfe.stype + " '" + nfe.name + "' could not be found"
func (nfe VarNotFoundError) Error() string {
return "required variable '" + nfe.name + "' could not be found"
}
// Decoder provides methods for decoding variable values
@ -63,7 +61,7 @@ func New(info *distro.OSRelease, runner *interp.Runner) *Decoder {
func (d *Decoder) DecodeVar(name string, val any) error {
variable := d.getVar(name)
if variable == nil {
return NotFoundError{"variable", name}
return VarNotFoundError{name}
}
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
@ -128,7 +126,7 @@ func (d *Decoder) DecodeVars(val any) error {
newVal := reflect.New(field.Type())
err := d.DecodeVar(name, newVal.Interface())
if _, ok := err.(NotFoundError); ok && !required {
if _, ok := err.(VarNotFoundError); ok && !required {
continue
} else if err != nil {
return err
@ -158,31 +156,6 @@ func (d *Decoder) GetFunc(name string) (ScriptFunc, bool) {
}, true
}
// WriteFunc writes the contents of a bash function to w.
func (d *Decoder) WriteFunc(name string, w io.Writer) error {
fn := d.getFunc(name)
if fn == nil {
return NotFoundError{"function", name}
}
printer := syntax.NewPrinter()
// Print individual statements instead of the entire block
block := fn.Cmd.(*syntax.Block)
for _, stmt := range block.Stmts {
err := printer.Print(w, stmt)
if err != nil {
return err
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}
return nil
}
func (d *Decoder) getFunc(name string) *syntax.Stmt {
names := d.genPossibleNames(name)
for _, fnName := range names {

View File

@ -64,6 +64,12 @@ func main() {
Aliases: []string{"up"},
Action: upgradeCmd,
},
{
Name: "info",
Usage: "Print information about a package",
Aliases: []string{"up"},
Action: infoCmd,
},
{
Flags: []cli.Flag{
&cli.StringFlag{