Initial commit

This commit is contained in:
2022-03-22 09:21:55 +01:00
commit ada7f18e36
49 changed files with 2635 additions and 0 deletions

156
internal/command/common.go Normal file
View File

@ -0,0 +1,156 @@
package command
import (
"encoding/json"
"io"
"os"
"strings"
"forge.cadoles.com/wpetit/formidable/internal/def"
"github.com/pkg/errors"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/urfave/cli/v2"
)
const (
filePathPrefix = "@"
)
func commonFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "defaults",
Aliases: []string{"d"},
Usage: "Default values as JSON or file path prefixed by '@'",
Value: "{}",
},
&cli.StringFlag{
Name: "values",
Aliases: []string{"v"},
Usage: "Current values as JSON or file path prefixed by '@'",
Value: "{}",
},
&cli.StringFlag{
Name: "schema",
Aliases: []string{"s"},
Usage: "Use `schema_file` as schema",
TakesFile: true,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o", "out"},
Value: "-",
Usage: "Output modified values to `output_file` (or '-' for stdout, the default)",
},
}
}
func loadJSONFlag(ctx *cli.Context, flagName string) (interface{}, error) {
flagValue := ctx.String(flagName)
if flagValue == "" {
return nil, nil
}
if !strings.HasPrefix(flagValue, filePathPrefix) {
var value interface{}
if err := json.Unmarshal([]byte(flagValue), &value); err != nil {
return nil, errors.WithStack(err)
}
return value, nil
}
flagValue = strings.TrimPrefix(flagValue, filePathPrefix)
file, err := os.Open(flagValue)
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
if err := file.Close(); err != nil {
panic(errors.WithStack(err))
}
}()
reader := json.NewDecoder(file)
var values interface{}
if err := reader.Decode(&values); err != nil {
return nil, errors.WithStack(err)
}
return values, nil
}
func loadValues(ctx *cli.Context) (interface{}, error) {
values, err := loadJSONFlag(ctx, "values")
if err != nil {
return nil, errors.WithStack(err)
}
return values, nil
}
func loadDefaults(ctx *cli.Context) (interface{}, error) {
values, err := loadJSONFlag(ctx, "defaults")
if err != nil {
return nil, errors.WithStack(err)
}
return values, nil
}
func loadSchema(ctx *cli.Context) (*jsonschema.Schema, error) {
schemaFlag := ctx.String("schema")
compiler := jsonschema.NewCompiler()
compiler.ExtractAnnotations = true
compiler.AssertFormat = true
compiler.AssertContent = true
var (
schema *jsonschema.Schema
err error
)
if schemaFlag == "" {
schema = def.Schema
} else {
schema, err = compiler.Compile(schemaFlag)
}
if err != nil {
return nil, errors.WithStack(err)
}
return schema, nil
}
const OutputStdout = "-"
type noopWriteCloser struct {
io.Writer
}
func (c *noopWriteCloser) Close() error {
return nil
}
func outputWriter(ctx *cli.Context) (io.WriteCloser, error) {
output := ctx.String("output")
if output == OutputStdout {
return &noopWriteCloser{ctx.App.Writer}, nil
}
file, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return nil, errors.WithStack(err)
}
return file, nil
}

View File

@ -0,0 +1,68 @@
package command
import (
"encoding/json"
"fmt"
"os"
"forge.cadoles.com/wpetit/formidable/internal/jsonpointer"
"github.com/pkg/errors"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/urfave/cli/v2"
)
func Delete() *cli.Command {
return &cli.Command{
Name: "delete",
Usage: "Delete value at specific path",
Flags: commonFlags(),
Action: func(ctx *cli.Context) error {
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")
}
rawPointer := ctx.Args().Get(0)
pointer := jsonpointer.New(rawPointer)
var updatedValues interface{}
updatedValues, err = pointer.Delete(values)
if err != nil {
return errors.Wrapf(err, "could not delete pointer '%v'", rawPointer)
}
if err := schema.Validate(updatedValues); err != nil {
if _, ok := err.(*jsonschema.ValidationError); ok {
fmt.Printf("%#v\n", err)
os.Exit(1)
}
return errors.Wrap(err, "could not validate resulting json")
}
output, err := outputWriter(ctx)
if err != nil {
return errors.Wrap(err, "could not create output writer")
}
encoder := json.NewEncoder(output)
encoder.SetIndent("", " ")
if err := encoder.Encode(updatedValues); err != nil {
return errors.Wrap(err, "could not write to output")
}
return nil
},
}
}

98
internal/command/edit.go Normal file
View File

@ -0,0 +1,98 @@
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()
flags = append(flags, &cli.StringFlag{
Name: "browser",
EnvVars: []string{"FORMIDABLE_BROWSER"},
Value: "w3m",
})
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")
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(
server.WithSchema(schema),
server.WithValues(values),
server.WithDefaults(defaults),
)
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)
cmdErrs := make(chan error)
cmdCtx, cmdCancel := context.WithCancel(ctx.Context)
defer cmdCancel()
go func() {
defer func() {
close(cmdErrs)
}()
cmd := exec.CommandContext(cmdCtx, 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)
}
}()
select {
case err := <-cmdErrs:
srvCancel()
return errors.WithStack(err)
case err := <-srvErrs:
cmdCancel()
return errors.WithStack(err)
}
},
}
}

69
internal/command/get.go Normal file
View File

@ -0,0 +1,69 @@
package command
import (
"encoding/json"
"fmt"
"os"
"forge.cadoles.com/wpetit/formidable/internal/jsonpointer"
"github.com/pkg/errors"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/urfave/cli/v2"
)
func Get() *cli.Command {
flags := []cli.Flag{}
flags = append(flags, commonFlags()...)
return &cli.Command{
Name: "get",
Usage: "Get value at specific path",
Flags: flags,
Action: func(ctx *cli.Context) error {
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")
}
if err := schema.Validate(values); err != nil {
if _, ok := err.(*jsonschema.ValidationError); ok {
fmt.Printf("%#v\n", err)
os.Exit(1)
}
return errors.Wrap(err, "could not validate resulting json")
}
rawPointer := ctx.Args().Get(0)
pointer := jsonpointer.New(rawPointer)
value, err := pointer.Get(values)
if err != nil {
return errors.Wrapf(err, "could not get value from pointer '%v'", rawPointer)
}
output, err := outputWriter(ctx)
if err != nil {
return errors.Wrap(err, "could not create output writer")
}
encoder := json.NewEncoder(output)
encoder.SetIndent("", " ")
if err := encoder.Encode(value); err != nil {
return errors.Wrap(err, "could not write to output")
}
return nil
},
}
}

12
internal/command/root.go Normal file
View File

@ -0,0 +1,12 @@
package command
import "github.com/urfave/cli/v2"
func Root() []*cli.Command {
return []*cli.Command{
Edit(),
Set(),
Get(),
Delete(),
}
}

95
internal/command/set.go Normal file
View File

@ -0,0 +1,95 @@
package command
import (
"encoding/json"
"fmt"
"os"
"forge.cadoles.com/wpetit/formidable/internal/jsonpointer"
"github.com/pkg/errors"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/urfave/cli/v2"
)
func Set() *cli.Command {
flags := []cli.Flag{
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force data tree creation",
Value: false,
},
}
flags = append(flags, commonFlags()...)
return &cli.Command{
Name: "set",
Usage: "Set value at specific path",
Flags: flags,
Action: func(ctx *cli.Context) error {
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")
}
rawPointer := ctx.Args().Get(0)
rawValue := ctx.Args().Get(1)
pointer := jsonpointer.New(rawPointer)
var value interface{}
if err := json.Unmarshal([]byte(rawValue), &value); err != nil {
return errors.Wrapf(err, "could not parse json '%s'", rawValue)
}
var updatedValues interface{}
force := ctx.Bool("force")
if force {
updatedValues, err = pointer.Force(values, value)
if err != nil {
return errors.Wrapf(err, "could not force value '%v' to pointer '%v'", rawValue, rawPointer)
}
} else {
updatedValues, err = pointer.Set(values, value)
if err != nil {
return errors.Wrapf(err, "could not set value '%v' to pointer '%v'", rawValue, rawPointer)
}
}
if err := schema.Validate(updatedValues); err != nil {
if _, ok := err.(*jsonschema.ValidationError); ok {
fmt.Printf("%#v\n", err)
os.Exit(1)
}
return errors.Wrap(err, "could not validate resulting json")
}
output, err := outputWriter(ctx)
if err != nil {
return errors.Wrap(err, "could not create output writer")
}
encoder := json.NewEncoder(output)
encoder.SetIndent("", " ")
if err := encoder.Encode(updatedValues); err != nil {
return errors.Wrap(err, "could not write to output")
}
return nil
},
}
}