Files
kontor/kontor-gin/internal/cmd/add-links.go
T
Thomas Peetz 094dffb1c7
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
add loading links form file
2026-10-07 18:50:09 +02:00

54 lines
1.1 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"kontor-api-gin/internal/helpers"
"log/slog"
"os"
"github.com/go-stomp/stomp/v3"
"github.com/spf13/cobra"
)
var addLinksCmd = &cobra.Command{
Use: "add-links [file]",
Short: "Add Links",
Long: "Add Links",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
conn, err := stomp.Dial("tcp", "localhost:61616",
stomp.ConnOpt.Login("artemis", "artemis"), // Provide username and password
)
if err != nil {
slog.Info("failed to connect to Artemis: %s", err)
os.Exit(1)
}
defer conn.Disconnect()
fileName := args[0]
slog.Info(fmt.Sprintf("add Links from %s", fileName))
lines, err := helpers.LinesInFile(fileName)
if err != nil {
slog.Warn("add links failed", "error", err)
os.Exit(1)
}
for _, line := range lines {
b, jsonErr := json.Marshal(line)
if jsonErr != nil {
slog.Info("Json encoding failed", "error", jsonErr.Error())
}
err = conn.Send("media.link", "application/json", b)
if err != nil {
slog.Info("send failed", "error", err.Error())
}
}
},
}
func init() {
rootCmd.AddCommand(addLinksCmd)
}