Add gen command
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2023-10-10 13:38:39 -07:00
parent 564f3c9b05
commit 602a558ab1
5 changed files with 147 additions and 1 deletions

46
gen.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"os"
"github.com/urfave/cli/v2"
"lure.sh/lure/pkg/gen"
)
var genCmd = &cli.Command{
Name: "generate",
Usage: "Generate a LURE script from a template",
Aliases: []string{"gen"},
Subcommands: []*cli.Command{
genPipCmd,
},
}
var genPipCmd = &cli.Command{
Name: "pip",
Usage: "Generate a LURE script for a pip module",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Required: true,
},
&cli.StringFlag{
Name: "version",
Aliases: []string{"v"},
Required: true,
},
&cli.StringFlag{
Name: "description",
Aliases: []string{"d"},
Value: "A Python Pip module",
},
},
Action: func(c *cli.Context) error {
return gen.Pip(os.Stdout, gen.PipOptions{
Name: c.String("name"),
Version: c.String("version"),
Description: c.String("description"),
})
},
}

View File

@ -62,8 +62,9 @@ var app = &cli.App{
removerepoCmd,
refreshCmd,
fixCmd,
versionCmd,
genCmd,
helperCmd,
versionCmd,
},
Before: func(c *cli.Context) error {
ctx := c.Context

9
pkg/gen/funcs.go Normal file
View File

@ -0,0 +1,9 @@
package gen
import "text/template"
var funcs = template.FuncMap{
"firstchar": func(s string) string {
return s[:1]
},
}

57
pkg/gen/pip.go Normal file
View File

@ -0,0 +1,57 @@
package gen
import (
_ "embed"
"fmt"
"io"
"net/http"
"path"
"text/template"
)
//go:embed tmpls/pip.tmpl.sh
var pipTmpl string
type PipOptions struct {
Name string
Version string
Description string
}
func Pip(w io.Writer, opts PipOptions) error {
tmpl, err := template.New("pip").
Funcs(funcs).
Parse(pipTmpl)
if err != nil {
return err
}
params := map[string]any{
"name": opts.Name,
"version": opts.Version,
"description": opts.Description,
}
url := fmt.Sprintf(
"https://files.pythonhosted.org/packages/source/%s/%s/%s-%s.tar.gz",
opts.Name[:1],
opts.Name,
opts.Name,
opts.Version,
)
res, err := http.Head(url)
if err != nil {
return err
}
dir := path.Dir(res.Request.URL.Path)
checksum := path.Base(dir)
dir = path.Dir(dir)
checksum = path.Base(dir) + checksum
dir = path.Dir(dir)
checksum = path.Base(dir) + checksum
params["checksum"] = "blake2b-256:" + checksum
return tmpl.Execute(w, params)
}

33
pkg/gen/tmpls/pip.tmpl.sh Normal file
View File

@ -0,0 +1,33 @@
name='{{.name}}'
version='{{.version}}'
release='1'
desc='{{.description}}'
homepage='https://example.com'
maintainer='Example <user@example.com>'
architectures=('all')
license=('custom:Unknown')
provides=('{{.name}}')
conflicts=('{{.name}}')
deps=("python3")
deps_arch=("python")
deps_alpine=("python3")
build_deps=("python3" "python3-setuptools")
build_deps_arch=("python" "python-setuptools")
build_deps_alpine=("python3" "py3-setuptools")
sources=("https://files.pythonhosted.org/packages/source/{{.name | firstchar}}/{{.name}}/{{.name}}-${version}.tar.gz")
checksums=('{{.checksum}}')
build() {
cd "$srcdir/{{.name}}-${version}"
python3 setup.py build
}
package() {
cd "$srcdir/{{.name}}-${version}"
python3 setup.py install --root="${pkgdir}/" --optimize=1 || return 1
}