mirror of
https://github.com/bitmagnet-io/bitmagnet.git
synced 2026-05-06 04:16:44 -04:00
c16f76130c
The classifier has been re-implemented and now uses a DSL allowing for full customisation. Several bugs have also been fixed. - Closes https://github.com/bitmagnet-io/bitmagnet/issues/182 - Closes https://github.com/bitmagnet-io/bitmagnet/issues/70 - Closes https://github.com/bitmagnet-io/bitmagnet/issues/68 - Hopefully fixes https://github.com/bitmagnet-io/bitmagnet/issues/126
82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package classifiercmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/bitmagnet-io/bitmagnet/internal/boilerplate/lazy"
|
|
"github.com/bitmagnet-io/bitmagnet/internal/classifier"
|
|
"github.com/urfave/cli/v2"
|
|
"go.uber.org/fx"
|
|
"gopkg.in/yaml.v3"
|
|
"io"
|
|
)
|
|
|
|
type Params struct {
|
|
fx.In
|
|
WorkflowSource lazy.Lazy[classifier.Source]
|
|
}
|
|
|
|
type Result struct {
|
|
fx.Out
|
|
Command *cli.Command `group:"commands"`
|
|
}
|
|
|
|
var formatFlag = cli.StringFlag{
|
|
Name: "format",
|
|
Usage: "Output format (json or yaml)",
|
|
Value: "yaml",
|
|
}
|
|
|
|
func New(p Params) (Result, error) {
|
|
return Result{Command: &cli.Command{
|
|
Name: "classifier",
|
|
Subcommands: []*cli.Command{
|
|
{
|
|
Name: "show",
|
|
Usage: "Show the classifier workflow source",
|
|
Flags: []cli.Flag{
|
|
&formatFlag,
|
|
},
|
|
Action: func(ctx *cli.Context) error {
|
|
src, srcErr := p.WorkflowSource.Get()
|
|
if srcErr != nil {
|
|
return srcErr
|
|
}
|
|
return write(ctx.App.Writer, src, ctx.String("format"))
|
|
},
|
|
},
|
|
{
|
|
Name: "schema",
|
|
Usage: "Show the classifier JSON schema",
|
|
Flags: []cli.Flag{
|
|
&formatFlag,
|
|
},
|
|
Action: func(ctx *cli.Context) error {
|
|
return write(ctx.App.Writer, classifier.DefaultJsonSchema(), ctx.String("format"))
|
|
},
|
|
},
|
|
},
|
|
}}, nil
|
|
}
|
|
|
|
func write(writer io.Writer, src any, format string) error {
|
|
var (
|
|
output []byte
|
|
outputErr error
|
|
)
|
|
switch format {
|
|
case "json":
|
|
output, outputErr = json.MarshalIndent(src, "", " ")
|
|
output = append(output, '\n')
|
|
case "yaml":
|
|
output, outputErr = yaml.Marshal(src)
|
|
default:
|
|
outputErr = fmt.Errorf("unsupported format: %s", format)
|
|
}
|
|
if outputErr != nil {
|
|
return outputErr
|
|
}
|
|
_, writeErr := writer.Write(output)
|
|
return writeErr
|
|
}
|