moonmath/moonmath.go

85 lines
1.7 KiB
Go
Raw Permalink Normal View History

2024-03-17 09:10:05 +00:00
package main
import (
"fmt"
2024-03-23 05:54:32 +00:00
"io"
"log/slog"
2024-03-17 09:10:05 +00:00
"os"
2024-03-23 05:54:32 +00:00
"path/filepath"
2024-03-22 00:39:10 +00:00
"strings"
2024-03-17 09:10:05 +00:00
2024-03-22 00:39:10 +00:00
"code.humancabbage.net/sam/moonmath/coindesk"
2024-03-20 04:44:11 +00:00
"code.humancabbage.net/sam/moonmath/config"
2024-03-20 01:38:46 +00:00
"code.humancabbage.net/sam/moonmath/tui"
2024-03-20 04:44:11 +00:00
"github.com/alecthomas/kong"
tea "github.com/charmbracelet/bubbletea"
2024-03-17 09:10:05 +00:00
)
2024-03-20 04:44:11 +00:00
var CLI struct {
Asset []string `short:"a" default:"BTC" help:"Asset(s) to project."`
ConfigFile string `short:"c" help:"Path to YAML configuration file."`
Perf bool `help:"Display internal performance stats."`
2024-03-20 04:44:11 +00:00
}
2024-03-17 09:10:05 +00:00
func main() {
2024-03-23 05:54:32 +00:00
logFile := setupLogging()
defer func() {
_ = logFile.Close()
}()
2024-03-20 04:44:11 +00:00
ctx := kong.Parse(&CLI)
if ctx.Error != nil {
fail(ctx.Error)
}
2024-03-22 00:39:10 +00:00
allCfg, err := config.Load(CLI.ConfigFile)
if err != nil {
fail(err)
}
var assets []coindesk.Asset
for i := range CLI.Asset {
asset := coindesk.Asset(strings.ToUpper(CLI.Asset[i]))
assets = append(assets, asset)
2024-03-20 04:44:11 +00:00
}
m := tui.New(assets, allCfg, CLI.Perf)
p := tea.NewProgram(m,
tea.WithAltScreen(),
tea.WithFPS(30),
)
2024-03-17 09:10:05 +00:00
if _, err := p.Run(); err != nil {
2024-03-20 04:44:11 +00:00
fail(err)
2024-03-17 09:10:05 +00:00
}
}
2024-03-20 04:44:11 +00:00
2024-03-23 05:54:32 +00:00
func setupLogging() io.Closer {
homePath, err := os.UserHomeDir()
if err != nil {
panic(err)
}
programConfigPath := filepath.Join(homePath, ".moonmath")
err = os.MkdirAll(programConfigPath, 0755)
if err != nil {
panic(err)
}
errLogPath := filepath.Join(programConfigPath, "errors.log")
errLogFile, err := os.OpenFile(
errLogPath,
os.O_CREATE|os.O_APPEND|os.O_WRONLY,
0600,
)
if err != nil {
panic(err)
}
slog.SetDefault(slog.New(
slog.NewTextHandler(errLogFile, &slog.HandlerOptions{
Level: slog.LevelError,
})))
return errLogFile
}
2024-03-20 04:44:11 +00:00
func fail(err error) {
fmt.Printf("program error: %v\n", err)
os.Exit(1)
}