2022-12-29 14:44:32 -05:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-12-31 13:00:32 -05:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2022-12-29 22:04:05 -05:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-12-29 14:44:32 -05:00
|
|
|
"github.com/mrusme/gobbs/config"
|
|
|
|
"github.com/mrusme/gobbs/system"
|
2022-12-29 22:04:05 -05:00
|
|
|
"github.com/mrusme/gobbs/ui"
|
2022-12-29 14:44:32 -05:00
|
|
|
"github.com/mrusme/gobbs/ui/ctx"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-12-29 17:47:10 -05:00
|
|
|
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() {
|
2022-12-29 22:04:05 -05:00
|
|
|
var err error
|
2022-12-29 14:44:32 -05:00
|
|
|
var logger *zap.Logger
|
|
|
|
|
2022-12-29 22:04:05 -05:00
|
|
|
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()
|
2022-12-29 17:47:10 -05:00
|
|
|
LOG = logger.Sugar()
|
2022-12-29 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadSystems(c *ctx.Ctx) []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
for _, sysCfg := range c.Config.Systems {
|
2022-12-31 13:42:30 -05:00
|
|
|
c.Logger.Debugf("loading system of type %s ...", sysCfg.Type)
|
2022-12-29 22:04:05 -05:00
|
|
|
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", err)
|
|
|
|
errs = append(errs, err)
|
2022-12-29 22:04:05 -05:00
|
|
|
} else {
|
2022-12-31 13:42:30 -05:00
|
|
|
c.Logger.Debugf("loaded %s system", sysCfg.Type)
|
2022-12-29 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
c.AddSystem(&sys)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "gobbs",
|
|
|
|
SuggestFor: []string{"bbs", "discourse", "lemmy"},
|
|
|
|
Short: "Gobbs, the bulletin board system TUI",
|
|
|
|
Long: "Gobbs is a bulletin board system (BBS) text user interface written " +
|
|
|
|
"in Go, supporting Discourse and Lemmy.\n" +
|
|
|
|
"More info available on https://xn--gckvb8fzb.com/projects/gobbs",
|
2022-12-29 22:04:05 -05:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-12-30 03:30:03 -05:00
|
|
|
var err error
|
|
|
|
|
2022-12-29 22:04:05 -05:00
|
|
|
c := ctx.New(&CFG, LOG)
|
|
|
|
_ = loadSystems(&c) // TODO: Handle errs
|
|
|
|
|
|
|
|
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())
|
|
|
|
err = tui.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
},
|
2022-12-29 14:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2022-12-29 17:47:10 -05:00
|
|
|
// LOG.Errorln(err)
|
2022-12-29 14:44:32 -05:00
|
|
|
}
|
|
|
|
}
|