use client package in jx cli
This commit is contained in:
parent
32c9d13e71
commit
52c58ea436
264
cmd/cli/main.go
264
cmd/cli/main.go
@ -4,18 +4,20 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"decl/internal/config"
|
"decl/internal/data"
|
||||||
"decl/internal/resource"
|
_ "decl/internal/config"
|
||||||
"decl/internal/source"
|
"decl/internal/folio"
|
||||||
"decl/internal/target"
|
_ "decl/internal/resource"
|
||||||
|
_ "decl/internal/fan"
|
||||||
|
"decl/internal/builtin"
|
||||||
_ "errors"
|
_ "errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "gopkg.in/yaml.v3"
|
_ "gopkg.in/yaml.v3"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
|
"decl/internal/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -29,6 +31,8 @@ var (
|
|||||||
date string
|
date string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Client *client.App = client.NewClient()
|
||||||
|
|
||||||
var GlobalOformat *string
|
var GlobalOformat *string
|
||||||
var GlobalOutput string
|
var GlobalOutput string
|
||||||
var GlobalQuiet *bool
|
var GlobalQuiet *bool
|
||||||
@ -40,7 +44,7 @@ var ApplyDelete *bool
|
|||||||
|
|
||||||
var ConfigPath string
|
var ConfigPath string
|
||||||
|
|
||||||
var ConfigDoc *config.Document = config.NewDocument()
|
var ConfigDoc data.Document = folio.DocumentRegistry.NewDocument("")
|
||||||
|
|
||||||
var ctx context.Context = context.Background()
|
var ctx context.Context = context.Background()
|
||||||
|
|
||||||
@ -88,62 +92,19 @@ func LoggerConfig() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfigURI(uri string) []*config.Document {
|
|
||||||
slog.Info("LoadConfigURI()", "uri", uri)
|
|
||||||
if uri != "" {
|
|
||||||
cs, err := config.ConfigSourceTypes.New(uri)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("Failed loading config document from source", "error", err)
|
|
||||||
}
|
|
||||||
extractConfigs, extractErr := cs.Extract(nil)
|
|
||||||
if extractErr != nil {
|
|
||||||
slog.Error("Failed loading configs from source", "error", extractErr)
|
|
||||||
}
|
|
||||||
return extractConfigs
|
|
||||||
}
|
|
||||||
return []*config.Document{config.NewDocument()}
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadSourceURI(uri string) []*resource.Document {
|
|
||||||
slog.Info("loading ", "uri", uri)
|
|
||||||
if uri != "" {
|
|
||||||
ds, err := source.SourceTypes.New(uri)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("Failed loading document from source", "error", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
extractDocuments, extractErr := ds.ExtractResources(nil)
|
|
||||||
if extractErr != nil {
|
|
||||||
slog.Error("Failed loading resources from source", "error", extractErr)
|
|
||||||
}
|
|
||||||
return extractDocuments
|
|
||||||
}
|
|
||||||
return []*resource.Document{resource.NewDocument()}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ConfigSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
func ConfigSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
||||||
e := cmd.Parse(os.Args[2:])
|
if err = cmd.Parse(os.Args[2:]); err != nil {
|
||||||
if e != nil { // returns ErrHelp
|
return
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("ConfigSubCommand", "configdoc", ConfigDoc)
|
if err = Client.SetOutput(GlobalOutput); err == nil {
|
||||||
for _, configSource := range cmd.Args() {
|
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
||||||
for _, argConfigDoc := range LoadConfigURI(configSource) {
|
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
||||||
ConfigDoc.Append(argConfigDoc)
|
|
||||||
}
|
}
|
||||||
|
err = Client.ConfigCmd(cmd.Args(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
outputTarget, err := config.ConfigTargetTypes.New(GlobalOutput)
|
return
|
||||||
if err != nil {
|
|
||||||
slog.Error("Failed opening target", "error", err)
|
|
||||||
}
|
|
||||||
defer outputTarget.Close()
|
|
||||||
|
|
||||||
if outputErr := outputTarget.EmitResources([]*config.Document{ConfigDoc}, nil); outputErr != nil {
|
|
||||||
return outputErr
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
||||||
@ -153,74 +114,14 @@ func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
|||||||
if e != nil { // returns ErrHelp
|
if e != nil { // returns ErrHelp
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
if err = Client.SetOutput(GlobalOutput); err == nil {
|
||||||
if ConfigPath != "" {
|
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
||||||
for _, argConfigDoc := range LoadConfigURI(ConfigPath) {
|
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
||||||
ConfigDoc.Append(argConfigDoc)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
merged := resource.NewDocument()
|
err = Client.ImportCmd(ctx, cmd.Args(), *ImportResource, *GlobalQuiet, *ImportMerge)
|
||||||
documents := make([]*resource.Document, 0, 100)
|
|
||||||
for _, source := range cmd.Args() {
|
|
||||||
loaded := LoadSourceURI(source)
|
|
||||||
if loaded != nil {
|
|
||||||
documents = append(documents, loaded...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
slog.Info("main.ImportResource", "args", os.Args, "output", GlobalOutput)
|
|
||||||
outputTarget, err := target.TargetTypes.New(GlobalOutput)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("Failed opening target", "error", err)
|
|
||||||
}
|
|
||||||
defer outputTarget.Close()
|
|
||||||
|
|
||||||
if len(documents) == 0 {
|
|
||||||
documents = append(documents, resource.NewDocument())
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, d := range documents {
|
|
||||||
if d != nil {
|
|
||||||
|
|
||||||
if *ImportResource != "" {
|
|
||||||
slog.Info("ImportResource", "resource", ImportResource)
|
|
||||||
resourceUri, uriErr := url.Parse(*ImportResource)
|
|
||||||
if uriErr != nil {
|
|
||||||
slog.Error("Failed adding resource", "error", uriErr)
|
|
||||||
}
|
|
||||||
if resourceUri.Scheme == "" {
|
|
||||||
resourceUri.Scheme = "file"
|
|
||||||
}
|
|
||||||
if addResourceErr := d.AddResource(resourceUri.String()); addResourceErr != nil {
|
|
||||||
slog.Error("Failed adding resource", "error", addResourceErr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if *GlobalQuiet {
|
|
||||||
for _, dr := range d.Resources() {
|
|
||||||
if _, e := output.Write([]byte(fmt.Sprintf("%s\n", dr.Resource().URI()))); e != nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if *ImportMerge {
|
|
||||||
merged.ResourceDecls = append(merged.ResourceDecls, d.ResourceDecls...)
|
|
||||||
} else {
|
|
||||||
slog.Info("main.ImportResource", "outputTarget", outputTarget, "type", outputTarget.Type())
|
|
||||||
if outputErr := outputTarget.EmitResources([]*resource.Document{d}, nil); outputErr != nil {
|
|
||||||
return outputErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if *ImportMerge {
|
|
||||||
if outputErr := outputTarget.EmitResources([]*resource.Document{merged}, nil); outputErr != nil {
|
|
||||||
return outputErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
||||||
@ -229,118 +130,26 @@ func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
if ConfigPath != "" {
|
if err = Client.SetOutput(GlobalOutput); err == nil {
|
||||||
for _, argConfigDoc := range LoadConfigURI(ConfigPath) {
|
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
||||||
ConfigDoc.Append(argConfigDoc)
|
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = Client.ApplyCmd(ctx, cmd.Args(), *GlobalQuiet, *ApplyDelete)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
documents := make([]*resource.Document, 0, 100)
|
|
||||||
for _, source := range cmd.Args() {
|
|
||||||
loaded := LoadSourceURI(source)
|
|
||||||
if loaded != nil {
|
|
||||||
documents = append(documents, loaded...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("main.Apply()", "documents", documents, "configdoc", ConfigDoc)
|
|
||||||
for _, d := range documents {
|
|
||||||
|
|
||||||
d.SetConfig(ConfigDoc)
|
|
||||||
|
|
||||||
slog.Info("main.Apply()", "doc", d)
|
|
||||||
var overrideState string = ""
|
|
||||||
if *ApplyDelete {
|
|
||||||
overrideState = "delete"
|
|
||||||
}
|
|
||||||
d.ResolveIds(ctx)
|
|
||||||
if e := d.Apply(overrideState); e != nil {
|
|
||||||
slog.Info("main.Apply() error", "error", e)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
outputTarget, err := target.TargetTypes.New(GlobalOutput)
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("Failed opening target", "error", err)
|
|
||||||
}
|
|
||||||
defer outputTarget.Close()
|
|
||||||
|
|
||||||
if *GlobalQuiet {
|
|
||||||
for _, dr := range d.Resources() {
|
|
||||||
if _, e := output.Write([]byte(dr.Resource().URI())); e != nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
slog.Info("main.Apply", "outputTarget", outputTarget, "type", outputTarget.Type())
|
|
||||||
if outputErr := outputTarget.EmitResources([]*resource.Document{d}, nil); outputErr != nil {
|
|
||||||
return outputErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DiffSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
func DiffSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
||||||
if e := cmd.Parse(os.Args[2:]); e != nil {
|
if e := cmd.Parse(os.Args[2:]); e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
leftSource := cmd.Arg(0)
|
|
||||||
rightSource := cmd.Arg(1)
|
return Client.DiffCmd(cmd.Args())
|
||||||
leftDocuments := make([]*resource.Document, 0, 100)
|
|
||||||
rightDocuments := make([]*resource.Document, 0, 100)
|
|
||||||
|
|
||||||
slog.Info("jx diff subcommand", "left", leftSource, "right", rightSource, "flagset", cmd)
|
|
||||||
|
|
||||||
if rightSource == "" {
|
|
||||||
rightDocuments = append(rightDocuments, LoadSourceURI(leftSource)...)
|
|
||||||
slog.Info("jx diff clone", "docs", rightDocuments)
|
|
||||||
for i, doc := range rightDocuments {
|
|
||||||
if doc != nil {
|
|
||||||
leftDocuments = append(leftDocuments, doc.Clone())
|
|
||||||
for _, resourceDeclaration := range leftDocuments[i].Resources() {
|
|
||||||
if _, e := resourceDeclaration.Resource().Read(ctx); e != nil {
|
|
||||||
slog.Info("jx diff ", "err", e)
|
|
||||||
//return e
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
leftDocuments = append(leftDocuments, LoadSourceURI(leftSource)...)
|
|
||||||
rightDocuments = append(rightDocuments, LoadSourceURI(rightSource)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("jx diff ", "right", rightDocuments, "left", leftDocuments)
|
|
||||||
index := 0
|
|
||||||
for {
|
|
||||||
if index >= len(rightDocuments) && index >= len(leftDocuments) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if index >= len(rightDocuments) {
|
|
||||||
if _, e := leftDocuments[index].Diff(resource.NewDocument(), output); e != nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
index++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if index >= len(leftDocuments) {
|
|
||||||
if _, e := resource.NewDocument().Diff(rightDocuments[index], output); e != nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
index++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if _, e := leftDocuments[index].Diff(rightDocuments[index], output); e != nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
index++
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
LoggerConfig()
|
LoggerConfig()
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
@ -348,20 +157,18 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultConfigurations, configErr := config.Configurations()
|
DefaultConfigurations, configErr := builtin.BuiltInDocuments()
|
||||||
if configErr != nil {
|
if configErr != nil {
|
||||||
slog.Error("Failed loading default configuration", "error", configErr)
|
slog.Error("Failed loading default configuration", "error", configErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, argConfigDoc := range DefaultConfigurations {
|
ConfigDoc.AppendConfigurations(DefaultConfigurations)
|
||||||
ConfigDoc.Append(argConfigDoc)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, subCmd := range jxSubCommands {
|
for _, subCmd := range jxSubCommands {
|
||||||
cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError)
|
cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError)
|
||||||
|
|
||||||
cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx/config.yaml", "Config file path")
|
cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx", "Config file path")
|
||||||
cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx/config.yaml", "Config file path")
|
cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx", "Config file path")
|
||||||
|
|
||||||
GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format")
|
GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format")
|
||||||
cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)")
|
cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)")
|
||||||
@ -398,6 +205,7 @@ func main() {
|
|||||||
if os.Args[1] == subCmd.Name {
|
if os.Args[1] == subCmd.Name {
|
||||||
if e := subCmd.Run(cmdFlagSet, os.Stdout); e != nil {
|
if e := subCmd.Run(cmdFlagSet, os.Stdout); e != nil {
|
||||||
slog.Error("Failed running command", "command", os.Args[1], "error", e)
|
slog.Error("Failed running command", "command", os.Args[1], "error", e)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user