2022-03-22 09:21:55 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"forge.cadoles.com/wpetit/formidable/internal/server"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Edit() *cli.Command {
|
|
|
|
flags := commonFlags()
|
|
|
|
|
2022-06-15 17:10:17 +02:00
|
|
|
flags = append(flags,
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "browser",
|
|
|
|
EnvVars: []string{"FORMIDABLE_BROWSER"},
|
|
|
|
Value: "w3m",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "http-host",
|
|
|
|
EnvVars: []string{"FORMIDABLE_HTTP_HOST"},
|
|
|
|
Value: "127.0.0.1",
|
|
|
|
},
|
|
|
|
&cli.UintFlag{
|
|
|
|
Name: "http-port",
|
|
|
|
EnvVars: []string{"FORMIDABLE_HTTP_PORT"},
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "no-browser",
|
|
|
|
EnvVars: []string{"FORMIDABLE_NO_BROWSER"},
|
|
|
|
Value: false,
|
|
|
|
},
|
|
|
|
)
|
2022-03-22 09:21:55 +01:00
|
|
|
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "edit",
|
|
|
|
Usage: "Display a form for given schema and values",
|
|
|
|
Flags: flags,
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
browser := ctx.String("browser")
|
2022-06-15 17:10:17 +02:00
|
|
|
noBrowser := ctx.Bool("no-browser")
|
|
|
|
httpPort := ctx.Uint("http-port")
|
|
|
|
httpHost := ctx.String("http-host")
|
2022-03-22 09:21:55 +01:00
|
|
|
|
|
|
|
schema, err := loadSchema(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not load schema")
|
|
|
|
}
|
|
|
|
|
|
|
|
values, err := loadValues(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not load values")
|
|
|
|
}
|
|
|
|
|
|
|
|
defaults, err := loadDefaults(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not load defaults")
|
|
|
|
}
|
|
|
|
|
|
|
|
srvCtx, srvCancel := context.WithCancel(ctx.Context)
|
|
|
|
defer srvCancel()
|
|
|
|
|
|
|
|
srv := server.New(
|
2022-06-15 17:10:17 +02:00
|
|
|
server.WithAddress(httpHost, httpPort),
|
2022-03-22 09:21:55 +01:00
|
|
|
server.WithSchema(schema),
|
|
|
|
server.WithValues(values),
|
|
|
|
server.WithDefaults(defaults),
|
2022-05-10 22:31:17 +02:00
|
|
|
server.WithOnUpdate(func(values interface{}) error {
|
|
|
|
if err := outputValues(ctx, values); err != nil {
|
|
|
|
return errors.Wrap(err, "could not output updated values")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}),
|
2022-03-22 09:21:55 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
addrs, srvErrs := srv.Start(srvCtx)
|
|
|
|
|
|
|
|
url := fmt.Sprintf("http://%s", (<-addrs).String())
|
|
|
|
url = strings.Replace(url, "0.0.0.0", "127.0.0.1", 1)
|
|
|
|
|
|
|
|
log.Printf("listening on %s", url)
|
|
|
|
|
2022-06-15 17:10:17 +02:00
|
|
|
browserErrs := make(chan error)
|
|
|
|
browserCtx, browserCancel := context.WithCancel(ctx.Context)
|
|
|
|
defer browserCancel()
|
2022-03-22 09:21:55 +01:00
|
|
|
|
2022-06-15 17:10:17 +02:00
|
|
|
if !noBrowser {
|
|
|
|
browserErrs = startBrowser(browserCtx, browser, url)
|
|
|
|
}
|
2022-03-22 09:21:55 +01:00
|
|
|
|
|
|
|
select {
|
2022-06-15 17:10:17 +02:00
|
|
|
case err := <-browserErrs:
|
2022-03-22 09:21:55 +01:00
|
|
|
srvCancel()
|
|
|
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
|
|
|
|
case err := <-srvErrs:
|
2022-06-15 17:10:17 +02:00
|
|
|
browserCancel()
|
2022-03-22 09:21:55 +01:00
|
|
|
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 17:10:17 +02:00
|
|
|
|
|
|
|
func startBrowser(ctx context.Context, browser, url string) chan error {
|
|
|
|
cmdErrs := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
close(cmdErrs)
|
|
|
|
}()
|
|
|
|
|
|
|
|
cmd := exec.CommandContext(ctx, browser, url)
|
|
|
|
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
cmdErrs <- errors.WithStack(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return cmdErrs
|
|
|
|
}
|