2024-03-20 19:23:31 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
2024-04-12 16:08:08 +00:00
|
|
|
|
2024-03-20 19:23:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-19 07:52:10 +00:00
|
|
|
"context"
|
2024-09-25 05:03:50 +00:00
|
|
|
"decl/internal/data"
|
|
|
|
_ "decl/internal/config"
|
|
|
|
"decl/internal/folio"
|
|
|
|
_ "decl/internal/resource"
|
|
|
|
_ "decl/internal/fan"
|
|
|
|
"decl/internal/builtin"
|
2024-10-16 17:26:42 +00:00
|
|
|
_ "errors"
|
2024-07-01 21:54:18 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-10-16 17:26:42 +00:00
|
|
|
_ "gopkg.in/yaml.v3"
|
2024-07-01 21:54:18 +00:00
|
|
|
"io"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
2024-09-25 05:03:50 +00:00
|
|
|
"decl/internal/client"
|
2024-03-20 19:23:31 +00:00
|
|
|
)
|
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
const (
|
|
|
|
FormatYaml = "yaml"
|
|
|
|
FormatJson = "json"
|
|
|
|
)
|
2024-03-20 19:23:31 +00:00
|
|
|
|
2024-04-23 22:35:08 +00:00
|
|
|
var (
|
|
|
|
version string
|
2024-07-01 21:54:18 +00:00
|
|
|
commit string
|
|
|
|
date string
|
2024-04-23 22:35:08 +00:00
|
|
|
)
|
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
var Client *client.App = client.NewClient()
|
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
var GlobalOformat *string
|
2024-04-25 07:45:05 +00:00
|
|
|
var GlobalOutput string
|
2024-04-19 07:52:10 +00:00
|
|
|
var GlobalQuiet *bool
|
2024-04-12 16:08:08 +00:00
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
var ImportMerge *bool
|
2024-04-21 06:13:17 +00:00
|
|
|
var ImportResource *string
|
2024-04-03 18:47:55 +00:00
|
|
|
|
2024-05-24 05:11:51 +00:00
|
|
|
var ApplyDelete *bool
|
|
|
|
|
2024-07-01 21:54:18 +00:00
|
|
|
var ConfigPath string
|
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
var ConfigDoc data.Document = folio.DocumentRegistry.NewDocument("")
|
2024-04-12 16:08:08 +00:00
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
var ctx context.Context = context.Background()
|
|
|
|
|
|
|
|
type RunCommand func(cmd *flag.FlagSet, output io.Writer) error
|
|
|
|
|
|
|
|
type SubCommand struct {
|
|
|
|
Name string
|
2024-07-01 21:54:18 +00:00
|
|
|
Run RunCommand
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 21:54:18 +00:00
|
|
|
var jxSubCommands = []SubCommand{
|
2024-04-19 07:52:10 +00:00
|
|
|
{
|
|
|
|
Name: "diff",
|
2024-07-01 21:54:18 +00:00
|
|
|
Run: DiffSubCommand,
|
2024-04-19 07:52:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "apply",
|
2024-07-01 21:54:18 +00:00
|
|
|
Run: ApplySubCommand,
|
2024-04-19 07:52:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "import",
|
2024-07-01 21:54:18 +00:00
|
|
|
Run: ImportSubCommand,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "config",
|
|
|
|
Run: ConfigSubCommand,
|
2024-04-19 07:52:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:35:08 +00:00
|
|
|
func VersionUsage() {
|
|
|
|
fmt.Println("jx")
|
|
|
|
fmt.Printf("version: %s\n", version)
|
|
|
|
fmt.Printf("commit: %s\n", commit)
|
|
|
|
fmt.Printf("date: %s\n", date)
|
|
|
|
}
|
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
func LoggerConfig() {
|
|
|
|
var programLevel = new(slog.LevelVar)
|
|
|
|
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel}))
|
|
|
|
slog.SetDefault(logger)
|
2024-07-01 21:54:18 +00:00
|
|
|
if debugLogging, ok := os.LookupEnv("JX_DEBUG"); ok && debugLogging != "" {
|
2024-04-19 07:52:10 +00:00
|
|
|
programLevel.Set(slog.LevelDebug)
|
|
|
|
} else {
|
|
|
|
programLevel.Set(slog.LevelError)
|
|
|
|
}
|
|
|
|
}
|
2024-04-12 16:08:08 +00:00
|
|
|
|
2024-07-01 21:54:18 +00:00
|
|
|
func ConfigSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
2024-09-25 05:03:50 +00:00
|
|
|
if err = cmd.Parse(os.Args[2:]); err != nil {
|
|
|
|
return
|
2024-07-01 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
if err = Client.SetOutput(GlobalOutput); err == nil {
|
|
|
|
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
|
|
|
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
2024-07-01 21:54:18 +00:00
|
|
|
}
|
2024-09-25 05:03:50 +00:00
|
|
|
err = Client.ConfigCmd(cmd.Args(), true)
|
2024-07-01 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
return
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ImportSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
2024-04-21 06:13:17 +00:00
|
|
|
ImportResource = cmd.String("resource", "", "(uri) Add a resource to the document.")
|
2024-04-19 07:52:10 +00:00
|
|
|
ImportMerge = cmd.Bool("merge", false, "Merge resources into a single document.")
|
2024-04-22 06:11:17 +00:00
|
|
|
e := cmd.Parse(os.Args[2:])
|
|
|
|
if e != nil { // returns ErrHelp
|
|
|
|
return e
|
|
|
|
}
|
2024-09-25 05:03:50 +00:00
|
|
|
if err = Client.SetOutput(GlobalOutput); err == nil {
|
|
|
|
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
|
|
|
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
2024-04-21 06:13:17 +00:00
|
|
|
}
|
2024-04-23 22:35:08 +00:00
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
err = Client.ImportCmd(ctx, cmd.Args(), *ImportResource, *GlobalQuiet, *ImportMerge)
|
2024-04-21 06:13:17 +00:00
|
|
|
}
|
2024-09-25 05:03:50 +00:00
|
|
|
return
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ApplySubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
2024-05-24 05:11:51 +00:00
|
|
|
ApplyDelete = cmd.Bool("delete", false, "Delete resources defined in the available documents.")
|
2024-04-22 06:11:17 +00:00
|
|
|
if e := cmd.Parse(os.Args[2:]); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2024-07-01 21:54:18 +00:00
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
if err = Client.SetOutput(GlobalOutput); err == nil {
|
|
|
|
if configErr := Client.SystemConfiguration(ConfigPath); configErr != nil {
|
|
|
|
slog.Info("Main.Import - SystemConfiguration", "config", ConfigPath, "error", configErr)
|
2024-04-21 06:13:17 +00:00
|
|
|
}
|
2024-07-01 21:54:18 +00:00
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
err = Client.ApplyCmd(ctx, cmd.Args(), *GlobalQuiet, *ApplyDelete)
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
2024-09-25 05:03:50 +00:00
|
|
|
return
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DiffSubCommand(cmd *flag.FlagSet, output io.Writer) (err error) {
|
2024-04-22 06:11:17 +00:00
|
|
|
if e := cmd.Parse(os.Args[2:]); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2024-09-25 05:03:50 +00:00
|
|
|
|
|
|
|
return Client.DiffCmd(cmd.Args())
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
2024-03-20 19:23:31 +00:00
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
func main() {
|
2024-04-19 07:52:10 +00:00
|
|
|
LoggerConfig()
|
|
|
|
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
fmt.Println("expected subcommands: diff, apply, import")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
DefaultConfigurations, configErr := builtin.BuiltInDocuments()
|
2024-07-01 21:54:18 +00:00
|
|
|
if configErr != nil {
|
2024-10-16 17:26:42 +00:00
|
|
|
slog.Warn("Failed loading default configuration", "error", configErr)
|
2024-07-01 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 05:03:50 +00:00
|
|
|
ConfigDoc.AppendConfigurations(DefaultConfigurations)
|
2024-07-01 21:54:18 +00:00
|
|
|
|
|
|
|
for _, subCmd := range jxSubCommands {
|
2024-04-19 07:52:10 +00:00
|
|
|
cmdFlagSet := flag.NewFlagSet(subCmd.Name, flag.ExitOnError)
|
2024-07-01 21:54:18 +00:00
|
|
|
|
2024-10-09 22:31:39 +00:00
|
|
|
cmdFlagSet.StringVar(&ConfigPath, "config", "/etc/jx/conf.d", "Config file path")
|
|
|
|
cmdFlagSet.StringVar(&ConfigPath, "c", "/etc/jx/conf.d", "Config file path")
|
2024-07-01 21:54:18 +00:00
|
|
|
|
2024-04-19 07:52:10 +00:00
|
|
|
GlobalOformat = cmdFlagSet.String("oformat", "yaml", "Output serialization format")
|
2024-04-25 07:45:05 +00:00
|
|
|
cmdFlagSet.StringVar(&GlobalOutput, "output", "-", "Output target (default stdout)")
|
|
|
|
cmdFlagSet.StringVar(&GlobalOutput, "o", "-", "Output target (default stdout)")
|
2024-04-19 07:52:10 +00:00
|
|
|
GlobalQuiet = cmdFlagSet.Bool("quiet", false, "Generate terse output.")
|
|
|
|
|
|
|
|
switch subCmd.Name {
|
|
|
|
case "diff":
|
|
|
|
cmdFlagSet.Usage = func() {
|
|
|
|
fmt.Println("jx diff source [source2]")
|
|
|
|
cmdFlagSet.PrintDefaults()
|
2024-04-23 22:35:08 +00:00
|
|
|
VersionUsage()
|
2024-04-12 16:08:08 +00:00
|
|
|
}
|
2024-04-19 07:52:10 +00:00
|
|
|
case "apply":
|
|
|
|
cmdFlagSet.Usage = func() {
|
|
|
|
fmt.Println("jx diff source [source2]")
|
|
|
|
cmdFlagSet.PrintDefaults()
|
2024-04-23 22:35:08 +00:00
|
|
|
VersionUsage()
|
2024-04-12 16:08:08 +00:00
|
|
|
}
|
2024-04-19 07:52:10 +00:00
|
|
|
case "import":
|
|
|
|
cmdFlagSet.Usage = func() {
|
2024-04-25 07:45:05 +00:00
|
|
|
fmt.Println("jx import source...")
|
2024-04-19 07:52:10 +00:00
|
|
|
cmdFlagSet.PrintDefaults()
|
2024-04-23 22:35:08 +00:00
|
|
|
VersionUsage()
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
2024-07-01 21:54:18 +00:00
|
|
|
case "config":
|
|
|
|
cmdFlagSet.Usage = func() {
|
|
|
|
fmt.Println("jx config source...")
|
|
|
|
cmdFlagSet.PrintDefaults()
|
|
|
|
VersionUsage()
|
|
|
|
}
|
2024-04-19 07:52:10 +00:00
|
|
|
}
|
2024-07-01 21:54:18 +00:00
|
|
|
slog.Info("CLI", "cmd", subCmd.Name)
|
2024-04-19 07:52:10 +00:00
|
|
|
if os.Args[1] == subCmd.Name {
|
2024-04-22 06:43:58 +00:00
|
|
|
if e := subCmd.Run(cmdFlagSet, os.Stdout); e != nil {
|
2024-05-24 05:11:51 +00:00
|
|
|
slog.Error("Failed running command", "command", os.Args[1], "error", e)
|
2024-09-25 05:03:50 +00:00
|
|
|
os.Exit(1)
|
2024-04-22 06:43:58 +00:00
|
|
|
}
|
2024-04-19 07:52:10 +00:00
|
|
|
return
|
2024-04-12 16:08:08 +00:00
|
|
|
}
|
2024-04-03 19:27:16 +00:00
|
|
|
}
|
2024-04-25 07:45:05 +00:00
|
|
|
flag.PrintDefaults()
|
|
|
|
VersionUsage()
|
|
|
|
os.Exit(1)
|
2024-03-20 19:23:31 +00:00
|
|
|
}
|