1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00
neonmodem/cmd/root.go

140 lines
2.9 KiB
Go
Raw Normal View History

2022-12-29 14:44:32 -05:00
package cmd
import (
2023-01-06 19:41:28 -05:00
"embed"
2022-12-31 13:00:32 -05:00
"net/url"
"os"
"runtime"
tea "github.com/charmbracelet/bubbletea"
2023-01-06 19:46:41 -05:00
"github.com/mrusme/neonmodem/config"
"github.com/mrusme/neonmodem/system"
"github.com/mrusme/neonmodem/ui"
"github.com/mrusme/neonmodem/ui/ctx"
2022-12-29 14:44:32 -05:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
2023-01-06 19:41:28 -05:00
var EMBEDFS *embed.FS
var LOG *zap.SugaredLogger
var CFG config.Config
2022-12-29 14:44:32 -05:00
func init() {
cobra.OnInitialize(load)
rootCmd.
PersistentFlags().
Bool(
"debug",
false,
"Debug output",
)
viper.BindPFlag(
"debug",
rootCmd.PersistentFlags().Lookup("debug"),
)
}
2022-12-31 13:00:32 -05:00
func loadLogger(filename string, debug bool) (*zap.Logger, error) {
if runtime.GOOS == "windows" {
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})
}
var cfg zap.Config
if debug {
cfg = zap.NewDevelopmentConfig()
} else {
cfg = zap.NewProductionConfig()
}
if runtime.GOOS == "windows" {
cfg.OutputPaths = []string{
"stdout",
"winfile:///" + filename,
}
} else {
cfg.OutputPaths = []string{
filename,
}
}
return cfg.Build()
}
2022-12-29 14:44:32 -05:00
func load() {
var err error
2022-12-29 14:44:32 -05:00
var logger *zap.Logger
CFG, err = config.Load()
2022-12-29 14:44:32 -05:00
if err != nil {
panic(err)
}
2022-12-31 13:00:32 -05:00
logger, err = loadLogger(CFG.Log, CFG.Debug)
if err != nil {
panic(err)
2022-12-29 14:44:32 -05:00
}
defer logger.Sync()
LOG = logger.Sugar()
2023-01-10 18:56:02 -05:00
if CFG.Proxy != "" {
LOG.Debugf("set proxy: %s", CFG.Proxy)
os.Setenv("HTTP_PROXY", CFG.Proxy)
}
2022-12-29 14:44:32 -05:00
}
func loadSystems(c *ctx.Ctx) []error {
var errs []error
2023-06-15 11:25:50 -04:00
for i := 0; i < len(c.Config.Systems); i++ {
sysCfg := c.Config.Systems[i]
c.Logger.Debugf("loading system of type %s ...", sysCfg.Type)
2023-01-10 18:56:02 -05:00
sysCfg.Config["proxy"] = CFG.Proxy
sys, err := system.New(sysCfg.Type, &sysCfg.Config, LOG)
2022-12-29 14:44:32 -05:00
if err != nil {
c.Logger.Errorf("error loading system %s: %s", sysCfg.Type, err)
c.Logger.Infof("system %s won't be available due to errors", sysCfg.Type)
2022-12-29 14:44:32 -05:00
errs = append(errs, err)
} else {
c.Logger.Debugf("loaded %s system", sysCfg.Type)
c.AddSystem(&sys)
c.Logger.Debugf("setting system ID to %d", c.NumSystems()-1)
sys.SetID(c.NumSystems() - 1)
2022-12-29 14:44:32 -05:00
}
}
return errs
}
var rootCmd = &cobra.Command{
2023-01-06 19:46:41 -05:00
Use: "neonmodem",
2022-12-29 14:44:32 -05:00
SuggestFor: []string{"bbs", "discourse", "lemmy"},
2023-01-06 19:46:41 -05:00
Short: "neonmodem, the bulletin board system TUI",
Long: "neonmodem is a bulletin board system (BBS) text user interface written " +
2022-12-29 14:44:32 -05:00
"in Go, supporting Discourse and Lemmy.\n" +
2023-01-06 19:46:41 -05:00
"More info available on https://xn--gckvb8fzb.com/projects/neonmodem",
Run: func(cmd *cobra.Command, args []string) {
2022-12-30 03:30:03 -05:00
var err error
2023-01-06 19:41:28 -05:00
c := ctx.New(EMBEDFS, &CFG, LOG)
_ = loadSystems(&c)
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())
err = tui.Start()
if err != nil {
panic(err)
}
},
2022-12-29 14:44:32 -05:00
}
2023-01-06 19:41:28 -05:00
func Execute(efs *embed.FS) {
EMBEDFS = efs
2022-12-29 14:44:32 -05:00
if err := rootCmd.Execute(); err != nil {
// LOG.Errorln(err)
2022-12-29 14:44:32 -05:00
}
}