This commit is contained in:
@@ -25,13 +25,13 @@ var addLinksCmd = &cobra.Command{
|
||||
slog.Warn("add links failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var login LoginResponse
|
||||
client := resty.New()
|
||||
token := helpers.GetToken(client)
|
||||
for _, line := range lines {
|
||||
lofiAdd := models.MediaLofiAdd{Url: line}
|
||||
client := resty.New()
|
||||
PostJSON(client, fmt.Sprintf("%s/%s/%s", server, "api", "login"), &login)
|
||||
response, err := client.R().
|
||||
SetBody(lofiAdd).
|
||||
SetAuthToken(token).
|
||||
SetHeader("Accept", "application/json").
|
||||
Post("http://localhost:8550/api/v1/media/lofi")
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"kontor-api-gin/internal/api"
|
||||
"kontor-api-gin/internal/config"
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"kontor-api-gin/internal/models"
|
||||
"log/slog"
|
||||
|
||||
@@ -24,39 +27,16 @@ var checkCmd = &cobra.Command{
|
||||
// Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
slog.Info("check kontor")
|
||||
slog.Info("Verbosity level", "level", verbose)
|
||||
var login LoginResponse
|
||||
slog.Info("Verbosity level", "level", config.Verbose)
|
||||
client := resty.New()
|
||||
_, err := client.R().
|
||||
SetBody(Login{
|
||||
Email: "thomas.peetz@thpeetz.de",
|
||||
Password: "admin",
|
||||
}).
|
||||
SetHeader("Accept", "application/json").
|
||||
SetResult(&login).
|
||||
Post("http://127.0.0.1:8550/api/login")
|
||||
if err != nil {
|
||||
slog.Info("Health", "error", err.Error())
|
||||
}
|
||||
slog.Info("health", "token", login.Token)
|
||||
|
||||
type ExportData struct {
|
||||
Sport []models.SportDTO `json:"sport"`
|
||||
Comic []models.ComicDTO `json:"comic"`
|
||||
}
|
||||
|
||||
var comics []models.ComicDTO
|
||||
_, err = client.R().
|
||||
SetAuthToken(login.Token).
|
||||
SetHeader("Accept", "application/json").
|
||||
SetResult(&comics).
|
||||
Get("http://127.0.0.1:8550/api/v1/comics/comics")
|
||||
if err != nil {
|
||||
slog.Info("comics", "error", err.Error())
|
||||
}
|
||||
// slog.Info("comics", "res", comics)
|
||||
|
||||
defer client.Close()
|
||||
token := helpers.GetToken(client)
|
||||
var comics []models.ComicDTO
|
||||
localServer := config.Config.Server["local"]
|
||||
|
||||
api.GetJSON(client, token, localServer, "comics/comics", &comics)
|
||||
|
||||
slog.Info("comics", "count", len(comics))
|
||||
|
||||
slog.Info("kontor.check_kontor.check_duplicate_links")
|
||||
// check_duplicate_links(first_server, dirs)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"kontor-api-gin/internal/config"
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
TraverseChildren: true,
|
||||
Short: "show current config",
|
||||
Long: "show current config",
|
||||
// Args: cobra.ExactArgs(2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
slog.Info("show config")
|
||||
var config config.CliConfig
|
||||
if err := viper.Unmarshal(&config); err != nil {
|
||||
slog.Info("unable to decode config", "error", err.Error())
|
||||
}
|
||||
for name, server := range config.Server {
|
||||
slog.Info("show config", "server", name, "URL", server.Url)
|
||||
}
|
||||
slog.Info("show config", "config", config)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"kontor-api-gin/internal/api"
|
||||
"kontor-api-gin/internal/config"
|
||||
"kontor-api-gin/internal/helpers"
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -11,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
server string
|
||||
servername string
|
||||
filename string
|
||||
apiVersion string
|
||||
)
|
||||
@@ -23,17 +25,19 @@ var exportCmd = &cobra.Command{
|
||||
// Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
slog.Info("export kontor")
|
||||
slog.Info("Verbosity level", "level", verbose)
|
||||
slog.Info(fmt.Sprintf("Using server: %s", server))
|
||||
var login LoginResponse
|
||||
slog.Info("Verbosity level", "level", config.Verbose)
|
||||
slog.Info(fmt.Sprintf("Using server: %s", servername))
|
||||
|
||||
client := resty.New()
|
||||
PostJSON(client, fmt.Sprintf("%s/%s/%s", server, "api", "login"), &login)
|
||||
defer client.Close()
|
||||
localServer := config.Config.Server[servername]
|
||||
|
||||
token := helpers.GetToken(client)
|
||||
|
||||
for path, value := range api.Registry {
|
||||
GetJSON(client, login.Token, fmt.Sprintf("%s/%s/%s", server, apiVersion, path), value)
|
||||
api.GetJSON(client, token, localServer, path, value)
|
||||
}
|
||||
export, _ := api.GetData(server, apiVersion)
|
||||
export, _ := api.GetData(localServer, apiVersion)
|
||||
err := api.WriteJson(export, filename)
|
||||
if err != nil {
|
||||
slog.Info("WriteJon failed", "Error:", err)
|
||||
@@ -42,36 +46,10 @@ var exportCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
func GetJSON(client *resty.Client, token string, url string, v any) {
|
||||
_, err := client.R().
|
||||
SetAuthToken(token).
|
||||
SetHeader("Accept", "application/json").
|
||||
SetResult(v).
|
||||
Get(url)
|
||||
if err != nil {
|
||||
slog.Info(url, "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func PostJSON(client *resty.Client, url string, v any) {
|
||||
_, err := client.R().
|
||||
SetBody(Login{
|
||||
Email: "thomas.peetz@thpeetz.de",
|
||||
Password: "admin",
|
||||
}).
|
||||
SetHeader("Accept", "application/json").
|
||||
SetResult(v).
|
||||
Post(url)
|
||||
if err != nil {
|
||||
slog.Info("Health", "error", err.Error())
|
||||
}
|
||||
slog.Info(url, "token", v)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(exportCmd)
|
||||
|
||||
exportCmd.PersistentFlags().StringVarP(&server, "server", "s", "http://127.0.0.1:8550", "Define server to get Kontor items")
|
||||
exportCmd.PersistentFlags().StringVarP(&servername, "servername", "s", "local", "Define server to get Kontor items")
|
||||
viper.BindPFlag("server", exportCmd.PersistentFlags().Lookup("server"))
|
||||
exportCmd.PersistentFlags().StringVarP(&filename, "file", "f", "/home/tpeetz/.sync/media/data-gin.json", "Filename to store data")
|
||||
viper.BindPFlag("file", exportCmd.PersistentFlags().Lookup("file"))
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"kontor-api-gin/internal/config"
|
||||
"log/slog"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var configInitcmd = &cobra.Command{
|
||||
Use: "init",
|
||||
Short: "create config",
|
||||
Long: "create config file with defaults",
|
||||
// Args: cobra.ExactArgs(2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
slog.Info("create config")
|
||||
config := config.CliConfig{
|
||||
Server: map[string]config.Server{
|
||||
"main": {
|
||||
Url: "https://kontor-gin.thpeetz.de",
|
||||
Username: "user",
|
||||
Password: "password",
|
||||
},
|
||||
"local": {
|
||||
Url: "http://localhost:8550",
|
||||
Username: "user",
|
||||
Password: "password",
|
||||
},
|
||||
},
|
||||
}
|
||||
viper.Set("server", config.Server)
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
slog.Info("write config failed", "error", err.Error())
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
configInitcmd.Flags().BoolVarP(&config.Overwrite, "overwrite", "y", false, "overwrite existing config")
|
||||
configCmd.AddCommand(configInitcmd)
|
||||
}
|
||||
@@ -2,30 +2,25 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kontor-api-gin/internal/config"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
var verbose int
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "kontor-cli",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Use: "kontor-cli",
|
||||
TraverseChildren: true,
|
||||
Short: "A brief description of your application",
|
||||
Long: ``,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Help()
|
||||
slog.Info("Verbosity level", "level", verbose)
|
||||
slog.Info("Verbosity level", "level", config.Verbose)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -37,6 +32,8 @@ func Execute() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "c", "config file (default is $HOME/.config/kli.yaml)")
|
||||
rootCmd.PersistentFlags().CountVarP(&verbose, "verbose", "v", "increase verbosity (-v, -vv, -vvv)")
|
||||
rootCmd.PersistentFlags().StringVarP(&config.CfgFile, "config", "c", "/home/tpeetz/.config/kli.yaml", "config file")
|
||||
rootCmd.PersistentFlags().CountVarP(&config.Verbose, "verbose", "v", "increase verbosity (-v, -vv, -vvv)")
|
||||
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
||||
cobra.OnInitialize(config.InitCliConfig)
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package cmd
|
||||
|
||||
import (
|
||||
"kontor-api-gin/internal/api"
|
||||
"kontor-api-gin/internal/config"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var syncCmd = &cobra.Command{
|
||||
@@ -15,8 +17,12 @@ var syncCmd = &cobra.Command{
|
||||
// Args: cobra.ExactArgs(2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
slog.Info("sync kontor")
|
||||
sourceData, _ := api.GetData(server, apiVersion)
|
||||
targetData, _ := api.GetData("https://kontor-gin.thpeetz.de", apiVersion)
|
||||
apiVersion := viper.GetString("api")
|
||||
mainServer := config.Config.Server["main"]
|
||||
slog.Info("syncCmd#Run", "mainServer", mainServer)
|
||||
localServer := config.Config.Server["local"]
|
||||
sourceData, _ := api.GetData(mainServer, apiVersion)
|
||||
targetData, _ := api.GetData(localServer, apiVersion)
|
||||
slog.Info("Size of targetData", "size", reflect.TypeOf(targetData).Size())
|
||||
// find missing items in target and collect items not available in source
|
||||
for table, item_list := range sourceData.GetMap() {
|
||||
|
||||
Reference in New Issue
Block a user