1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-11-03 04:27:16 -05:00

Refactored main, added Cobra

This commit is contained in:
マリウス 2022-12-29 14:44:32 -05:00
parent 63cea35624
commit f1efaab908
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
4 changed files with 107 additions and 55 deletions

86
cmd/root.go Normal file
View File

@ -0,0 +1,86 @@
package cmd
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/config"
"github.com/mrusme/gobbs/system"
"github.com/mrusme/gobbs/ui"
"github.com/mrusme/gobbs/ui/ctx"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
)
var sugar *zap.SugaredLogger
func init() {
cobra.OnInitialize(load)
rootCmd.
PersistentFlags().
Bool(
"debug",
false,
"Debug output",
)
viper.BindPFlag(
"debug",
rootCmd.PersistentFlags().Lookup("debug"),
)
}
func load() {
var logger *zap.Logger
cfg, err := config.Load()
if err != nil {
panic(err)
}
if cfg.Debug == "true" {
logger, _ = zap.NewDevelopment()
} else {
logger, _ = zap.NewProduction()
}
defer logger.Sync()
sugar = logger.Sugar()
c := ctx.New(&cfg, sugar)
_ = loadSystems(&c) // TODO: Handle errs
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())
err = tui.Start()
if err != nil {
panic(err)
}
}
func loadSystems(c *ctx.Ctx) []error {
var errs []error
for _, sysCfg := range c.Config.Systems {
sys, err := system.New(sysCfg.Type, &sysCfg.Config)
if err != nil {
c.Logger.Errorf("error loading system: %s", err)
errs = append(errs, err)
}
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",
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
sugar.Errorln(err)
}
}

19
go.mod
View File

@ -2,16 +2,23 @@ module github.com/mrusme/gobbs
go 1.19
require (
github.com/charmbracelet/bubbles v0.14.0
github.com/charmbracelet/bubbletea v0.23.1
github.com/charmbracelet/lipgloss v0.6.0
github.com/google/uuid v1.3.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/spf13/viper v1.14.0
go.uber.org/zap v1.24.0
)
require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/charmbracelet/bubbles v0.14.0 // indirect
github.com/charmbracelet/bubbletea v0.23.1 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
@ -24,18 +31,16 @@ require (
github.com/muesli/termenv v0.13.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/sahilm/fuzzy v0.1.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.14.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect

7
go.sum
View File

@ -63,6 +63,7 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -138,6 +139,9 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@ -192,12 +196,15 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=

View File

@ -1,53 +1,7 @@
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/config"
"github.com/mrusme/gobbs/system"
"github.com/mrusme/gobbs/ui"
"github.com/mrusme/gobbs/ui/ctx"
"go.uber.org/zap"
)
import "github.com/mrusme/gobbs/cmd"
func main() {
var logger *zap.Logger
var sugar *zap.SugaredLogger
cfg, err := config.Load()
if err != nil {
panic(err)
}
if cfg.Debug == "true" {
logger, _ = zap.NewDevelopment()
} else {
logger, _ = zap.NewProduction()
}
defer logger.Sync()
sugar = logger.Sugar()
c := ctx.New(&cfg, sugar)
_ = loadSystems(&c) // TODO: Handle errs
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())
err = tui.Start()
if err != nil {
panic(err)
}
}
func loadSystems(c *ctx.Ctx) []error {
var errs []error
for _, sysCfg := range c.Config.Systems {
sys, err := system.New(sysCfg.Type, &sysCfg.Config)
if err != nil {
c.Logger.Errorf("error loading system: %s", err)
errs = append(errs, err)
}
c.AddSystem(&sys)
}
return errs
cmd.Execute()
}