2022-12-29 17:47:10 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-01-05 14:57:01 -05:00
|
|
|
"fmt"
|
2023-01-06 23:48:21 -05:00
|
|
|
"net/url"
|
2022-12-29 17:47:10 -05:00
|
|
|
"os"
|
2023-01-06 23:48:21 -05:00
|
|
|
"strings"
|
2022-12-29 17:47:10 -05:00
|
|
|
|
2023-01-06 19:46:41 -05:00
|
|
|
"github.com/mrusme/neonmodem/config"
|
|
|
|
"github.com/mrusme/neonmodem/system"
|
2022-12-29 17:47:10 -05:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd := connectBase()
|
|
|
|
rootCmd.AddCommand(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectBase() *cobra.Command {
|
|
|
|
var sysType string = ""
|
|
|
|
var sysURL string = ""
|
|
|
|
var sysConfig map[string]interface{}
|
|
|
|
|
|
|
|
var cmd = &cobra.Command{
|
|
|
|
Use: "connect",
|
|
|
|
Short: "Connect to BBS",
|
|
|
|
Long: "Add a new connection to a BBS.",
|
2022-12-31 12:30:26 -05:00
|
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
sysType, _ := cmd.Flags().GetString("type")
|
2023-01-06 23:48:21 -05:00
|
|
|
sysType = strings.ToLower(sysType)
|
2022-12-31 12:30:26 -05:00
|
|
|
if sysType != "hackernews" {
|
|
|
|
cmd.MarkFlagRequired("url")
|
|
|
|
}
|
|
|
|
},
|
2022-12-29 17:47:10 -05:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
sysConfig = make(map[string]interface{})
|
2022-12-29 22:04:05 -05:00
|
|
|
sys, err := system.New(sysType, &sysConfig, LOG)
|
2022-12-29 17:47:10 -05:00
|
|
|
if err != nil {
|
|
|
|
LOG.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2023-01-06 23:48:21 -05:00
|
|
|
sysURLparsed, err := url.Parse(sysURL)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if caps := sys.GetCapabilities(); !caps.IsCapableOf("connect:multiple") {
|
|
|
|
for _, existingSys := range CFG.Systems {
|
|
|
|
if existingSys.Type == sysType {
|
|
|
|
existingSysURL, ok := existingSys.Config["url"]
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("Cannot add multiple instances of this system!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
existingSysURLparsed, err := url.Parse(existingSysURL.(string))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
//&& existingSysURLparsed.RequestURI() == sysURLparsed.RequestURI()
|
|
|
|
if existingSysURLparsed.Host == sysURLparsed.Host {
|
|
|
|
fmt.Println("Cannot add multiple instances of this system!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 17:47:10 -05:00
|
|
|
if err := sys.Connect(sysURL); err != nil {
|
|
|
|
LOG.Panicln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
CFG.Systems = append(CFG.Systems, config.SystemConfig{
|
|
|
|
Type: sysType,
|
|
|
|
Config: sys.GetConfig(),
|
|
|
|
})
|
|
|
|
if err := CFG.Save(); err != nil {
|
|
|
|
LOG.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2023-01-05 14:57:01 -05:00
|
|
|
fmt.Println("Successfully added new connection!")
|
2022-12-29 17:47:10 -05:00
|
|
|
os.Exit(0)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.
|
|
|
|
Flags().
|
|
|
|
StringVar(
|
|
|
|
&sysType,
|
|
|
|
"type",
|
|
|
|
"",
|
2023-06-13 08:46:55 -04:00
|
|
|
"Type of system to connect to (discourse, lemmy, lobsters, hackernews)",
|
2022-12-29 17:47:10 -05:00
|
|
|
)
|
|
|
|
cmd.MarkFlagRequired("type")
|
|
|
|
|
|
|
|
cmd.
|
|
|
|
Flags().
|
|
|
|
StringVar(
|
|
|
|
&sysURL,
|
|
|
|
"url",
|
|
|
|
"",
|
|
|
|
"URL of system (e.g. https://www.keebtalk.com)",
|
|
|
|
)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|