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 (
|
||||
"context"
|
||||
"decl/internal/config"
|
||||
"decl/internal/resource"
|
||||
"decl/internal/source"
|
||||
"decl/internal/target"
|
||||
"decl/internal/data"
|
||||
_ "decl/internal/config"
|
||||
"decl/internal/folio"
|
||||
_ "decl/internal/resource"
|
||||
_ "decl/internal/fan"
|
||||
"decl/internal/builtin"
|
||||
_ "errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
_ "gopkg.in/yaml.v3"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"os"
|
||||
"decl/internal/client"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -29,6 +31,8 @@ var (
|
||||
date string
|
||||
)
|
||||
|
||||
var Client *client.App = client.NewClient()
|
||||
|
||||
var GlobalOformat *string
|
||||
var GlobalOutput string
|
||||
var GlobalQuiet *bool
|
||||
@ -40,7 +44,7 @@ var ApplyDelete *bool
|
||||
|
||||
var ConfigPath string
|
||||
|
||||
var ConfigDoc *config.Document = config.NewDocument()
|
||||
var ConfigDoc data.Document = folio.DocumentRegistry.NewDocument("")
|
||||
|
||||
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) {
|
||||
e := cmd.Parse(os.Args[2:])
|
||||
if e != nil { // returns ErrHelp
|
||||
return e
|
||||
if err = cmd.Parse(os.Args[2:]); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("ConfigSubCommand", "configdoc", ConfigDoc)
|
||||
for _, configSource := range cmd.Args() {
|
||||
for _, argConfigDoc := range LoadConfigURI(configSource) {
|
||||
ConfigDoc.Append(argConfigDoc)
|
||||
if err = Client.SetOutput(GlobalOutput); err == nil {
|
||||
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
||||
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
||||
}
|
||||
err = Client.ConfigCmd(cmd.Args(), true)
|
||||
}
|
||||
|
||||
outputTarget, err := config.ConfigTargetTypes.New(GlobalOutput)
|
||||
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
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
return e
|
||||
}
|
||||
|
||||
if ConfigPath != "" {
|
||||
for _, argConfigDoc := range LoadConfigURI(ConfigPath) {
|
||||
ConfigDoc.Append(argConfigDoc)
|
||||
if err = Client.SetOutput(GlobalOutput); err == nil {
|
||||
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
||||
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
||||
}
|
||||
}
|
||||
|
||||
merged := resource.NewDocument()
|
||||
documents := make([]*resource.Document, 0, 100)
|
||||
for _, source := range cmd.Args() {
|
||||
loaded := LoadSourceURI(source)
|
||||
if loaded != nil {
|
||||
documents = append(documents, loaded...)
|
||||
}
|
||||
err = Client.ImportCmd(ctx, cmd.Args(), *ImportResource, *GlobalQuiet, *ImportMerge)
|
||||
}
|
||||
|
||||
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
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if ConfigPath != "" {
|
||||
for _, argConfigDoc := range LoadConfigURI(ConfigPath) {
|
||||
ConfigDoc.Append(argConfigDoc)
|
||||
if err = Client.SetOutput(GlobalOutput); err == nil {
|
||||
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
||||
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
||||
}
|
||||
|
||||
err = Client.ApplyCmd(ctx, cmd.Args(), *GlobalQuiet, *ApplyDelete)
|
||||
}
|
||||
|
||||
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
|
||||
return
|
||||
}
|
||||
|
||||
func DiffSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
||||
if e := cmd.Parse(os.Args[2:]); e != nil {
|
||||
return e
|
||||
}
|
||||
leftSource := cmd.Arg(0)
|
||||
rightSource := cmd.Arg(1)
|
||||
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
|
||||
|
||||
return Client.DiffCmd(cmd.Args())
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
func main() {
|
||||
LoggerConfig()
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
@ -348,20 +157,18 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
DefaultConfigurations, configErr := config.Configurations()
|
||||
DefaultConfigurations, configErr := builtin.BuiltInDocuments()
|
||||
if configErr != nil {
|
||||
slog.Error("Failed loading default configuration", "error", configErr)
|
||||
}
|
||||
|
||||
for _, argConfigDoc := range DefaultConfigurations {
|
||||
ConfigDoc.Append(argConfigDoc)
|
||||
}
|
||||
ConfigDoc.AppendConfigurations(DefaultConfigurations)
|
||||
|
||||
for _, subCmd := range jxSubCommands {
|
||||
cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError)
|
||||
|
||||
cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx/config.yaml", "Config file path")
|
||||
cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx/config.yaml", "Config file path")
|
||||
cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx", "Config file path")
|
||||
cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx", "Config file path")
|
||||
|
||||
GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format")
|
||||
cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)")
|
||||
@ -398,6 +205,7 @@ func main() {
|
||||
if os.Args[1] == subCmd.Name {
|
||||
if e := subCmd.Run(cmdFlagSet, os.Stdout); e != nil {
|
||||
slog.Error("Failed running command", "command", os.Args[1], "error", e)
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user