Skip to content

Commit

Permalink
feat: ini runtime config
Browse files Browse the repository at this point in the history
Migrate away from json for runtime configuration
  • Loading branch information
williamsjokvist committed Jan 27, 2025
1 parent 4bcb80f commit 1e15327
Show file tree
Hide file tree
Showing 22 changed files with 162 additions and 195 deletions.
43 changes: 29 additions & 14 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/williamsjokvist/cfn-tracker/pkg/i18n"
"github.com/williamsjokvist/cfn-tracker/pkg/model"
"github.com/williamsjokvist/cfn-tracker/pkg/server"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/nosql"
cfgDb "github.com/williamsjokvist/cfn-tracker/pkg/storage/config"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/sql"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/txt"
"github.com/williamsjokvist/cfn-tracker/pkg/update/github"
Expand All @@ -24,16 +24,16 @@ import (
type CommandHandler struct {
githubClient github.GithubClient

sqlDb *sql.Storage
nosqlDb *nosql.Storage
sqlDb *sql.Storage
cfgDb *cfgDb.Storage

cfg *config.Config
cfg *config.BuildConfig
}

func NewCommandHandler(githubClient github.GithubClient, sqlDb *sql.Storage, nosqlDb *nosql.Storage, txtDb *txt.Storage, cfg *config.Config) *CommandHandler {
func NewCommandHandler(githubClient github.GithubClient, sqlDb *sql.Storage, cfgDb *cfgDb.Storage, txtDb *txt.Storage, cfg *config.BuildConfig) *CommandHandler {
return &CommandHandler{
sqlDb: sqlDb,
nosqlDb: nosqlDb,
cfgDb: cfgDb,
githubClient: githubClient,
cfg: cfg,
}
Expand Down Expand Up @@ -146,29 +146,44 @@ func (ch *CommandHandler) GetSupportedLanguages() ([]string, error) {
}

func (ch *CommandHandler) SaveLocale(locale string) error {
if err := ch.nosqlDb.SaveLocale(locale); err != nil {
runtimeCfg, err := ch.cfgDb.GetRuntimeConfig()
if err != nil {
return model.WrapError(model.ErrGetGUIConfig, err)
}
runtimeCfg.GUI.Locale = locale
if err := ch.cfgDb.SaveRuntimeConfig(runtimeCfg); err != nil {
return model.WrapError(model.ErrSaveLocale, err)
}
return nil
}

func (ch *CommandHandler) GetGuiConfig() (*model.GuiConfig, error) {
guiCfg, err := ch.nosqlDb.GetGuiConfig()
func (ch *CommandHandler) GetGuiConfig() (*model.GUIConfig, error) {
runtimeCfg, err := ch.cfgDb.GetRuntimeConfig()
if err != nil {
return nil, model.WrapError(model.ErrGetGUIConfig, err)
}
return guiCfg, nil
return &runtimeCfg.GUI, nil
}

func (ch *CommandHandler) SaveSidebarMinimized(sidebarMinified bool) error {
if err := ch.nosqlDb.SaveSidebarMinimized(sidebarMinified); err != nil {
return model.WrapError(model.ErrSaveSidebarMinimized, err)
func (ch *CommandHandler) SaveSidebarMinimized(sidebar bool) error {
runtimeCfg, err := ch.cfgDb.GetRuntimeConfig()
if err != nil {
return model.WrapError(model.ErrGetGUIConfig, err)
}
runtimeCfg.GUI.SideBar = sidebar
if err := ch.cfgDb.SaveRuntimeConfig(runtimeCfg); err != nil {
return model.WrapError(model.ErrSaveSidebar, err)
}
return nil
}

func (ch *CommandHandler) SaveTheme(theme model.ThemeName) error {
if err := ch.nosqlDb.SaveTheme(theme); err != nil {
runtimeCfg, err := ch.cfgDb.GetRuntimeConfig()
if err != nil {
return model.WrapError(model.ErrGetGUIConfig, err)
}
runtimeCfg.GUI.Theme = theme
if err := ch.cfgDb.SaveRuntimeConfig(runtimeCfg); err != nil {
return model.WrapError(model.ErrSaveTheme, err)
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/williamsjokvist/cfn-tracker/cmd"
"github.com/williamsjokvist/cfn-tracker/pkg/config"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/nosql"
cfgDb "github.com/williamsjokvist/cfn-tracker/pkg/storage/config"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/sql"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/txt"
"github.com/williamsjokvist/cfn-tracker/pkg/tracker/sf6/cfn"
Expand All @@ -24,7 +24,7 @@ func TestMain(m *testing.M) {
if err != nil {
log.Fatalf("init sql storage: %v", err)
}
nosqlDb, err := nosql.NewStorage()
cfgDb, err := cfgDb.NewStorage()
if err != nil {
log.Fatalf("init nosql storage: %v", err)
}
Expand All @@ -33,7 +33,7 @@ func TestMain(m *testing.M) {
log.Fatalf("init txt storage: %v", err)
}

cfg := config.Config{
cfg := config.BuildConfig{
AppVersion: "4.0.0",
Headless: true,
CapIDEmail: "test",
Expand All @@ -46,7 +46,7 @@ func TestMain(m *testing.M) {
wavu.NewClient(),
cfn.NewClient(nil),
sqlDb,
nosqlDb,
cfgDb,
txtDb,
&cfg,
nil,
Expand Down
10 changes: 5 additions & 5 deletions cmd/tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/williamsjokvist/cfn-tracker/pkg/config"
"github.com/williamsjokvist/cfn-tracker/pkg/model"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/nosql"
cfgDb "github.com/williamsjokvist/cfn-tracker/pkg/storage/config"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/sql"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/txt"
"github.com/williamsjokvist/cfn-tracker/pkg/tracker"
Expand All @@ -23,13 +23,13 @@ type EventEmitFn func(eventName string, optionalData ...interface{})

type TrackingHandler struct {
sqlDb *sql.Storage
nosqlDb *nosql.Storage
nosqlDb *cfgDb.Storage
txtDb *txt.Storage

wavuClient wavu.WavuClient
cfnClient cfn.CFNClient

cfg *config.Config
cfg *config.BuildConfig
matchChans []chan model.Match

cancelPolling context.CancelFunc
Expand All @@ -42,9 +42,9 @@ func NewTrackingHandler(
wavuClient wavu.WavuClient,
cfnClient cfn.CFNClient,
sqlDb *sql.Storage,
nosqlDb *nosql.Storage,
nosqlDb *cfgDb.Storage,
txtDb *txt.Storage,
cfg *config.Config,
cfg *config.BuildConfig,
matchChans ...chan model.Match,
) *TrackingHandler {
return &TrackingHandler{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/wailsapp/wails/v2 v2.9.2
gopkg.in/ini.v1 v1.67.0
modernc.org/sqlite v1.34.4
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
Empty file removed gui/dist/.gitkeep
Empty file.
8 changes: 4 additions & 4 deletions gui/src/main/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export function AppSidebar() {
React.useEffect(() => {
animate(
'span',
{ opacity: +!cfg.sidebarMinified, display: cfg.sidebarMinified ? 'none' : 'block' },
{ opacity: +!cfg.sidebar, display: cfg.sidebar ? 'none' : 'block' },
{ duration: 0.175, ease: 'circIn' }
)
}, [cfg.sidebarMinified])
}, [cfg.sidebar])

return (
<div
Expand All @@ -35,7 +35,7 @@ export function AppSidebar() {
'transition-[width_250ms_ease-out]'
)}
style={{
width: cfg.sidebarMinified ? '76px' : '175px'
width: cfg.sidebar ? '76px' : '175px'
}}
>
<AppTitleBar />
Expand Down Expand Up @@ -94,7 +94,7 @@ function Nav() {
/>
<span>{t(href)}</span>
</div>
{!cfg.sidebarMinified && (
{!cfg.sidebar && (
<Icon
icon='fa6-solid:chevron-left'
className='h-3 w-3 rotate-180 opacity-0 transition-opacity group-hover:opacity-100'
Expand Down
6 changes: 3 additions & 3 deletions gui/src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { useTranslation } from 'react-i18next'
import { model } from '@model'
import { GetGuiConfig } from '@cmd/CommandHandler'

const initialConfig: model.GuiConfig = {
const initialConfig: model.GUIConfig = {
locale: 'en-GB',
theme: model.ThemeName.DEFAULT,
sidebarMinified: false
sidebar: false
}

export const ConfigContext = React.createContext<
[cfg: model.GuiConfig, setCfg: React.Dispatch<model.GuiConfig>]
[cfg: model.GUIConfig, setCfg: React.Dispatch<model.GUIConfig>]
>([
initialConfig,
() => {
Expand Down
14 changes: 2 additions & 12 deletions gui/src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ export function SettingsPage() {
text={t('follow')}
url='https://x.com/greensoap_'
/>
<Link
icon={
<Icon
icon='fa6-brands:twitter'
className='mr-2 h-6 w-6 text-[#49b3f5] transition-colors group-hover:text-white'
/>
}
text='enth'
url='https://x.com/enthcreations'
/>
</div>
</div>
</motion.section>
Expand Down Expand Up @@ -116,10 +106,10 @@ function SideBarToggle() {
<div className='flex w-full justify-between'>
<h3 className='font-bold'>{t('minimize')}</h3>
<Switch
checked={cfg.sidebarMinified}
checked={cfg.sidebar}
onCheckedChange={checked => {
SaveSidebarMinimized(checked)
.then(() => setCfg({ ...cfg, sidebarMinified: checked }))
.then(() => setCfg({ ...cfg, sidebar: checked }))
.catch(setError)
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion gui/src/pages/tracking/tracking-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function TrackingForm() {
)}
<footer className='flex w-full items-center'>
{users.some(old => old.code === playerIdInput) && (
<div className='group flex items-center'>
<div className='group flex items-center gap-4'>
<Checkbox ref={restoreRef} id='restore-session' />
<label
htmlFor='restore-session'
Expand Down
1 change: 0 additions & 1 deletion gui/src/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export function Button(props: JSX.IntrinsicElements['button']) {
const { disabled, style, className, children, onClick, ...restProps } = props
return (
<button
ref={props.ref}
onClick={disabled ? undefined : onClick}
{...(disabled
? {
Expand Down
2 changes: 1 addition & 1 deletion gui/wailsjs/go/cmd/CommandHandler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function GetAppVersion():Promise<string>;

export function GetFGCTrackerErrorModelUnused():Promise<model.FGCTrackerError>;

export function GetGuiConfig():Promise<model.GuiConfig>;
export function GetGuiConfig():Promise<model.GUIConfig>;

export function GetMatches(arg1:number,arg2:string,arg3:number,arg4:number):Promise<Array<model.Match>>;

Expand Down
10 changes: 5 additions & 5 deletions gui/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export namespace model {
errGetGuiConfig = "errGetGuiConfig",
errSaveTheme = "errSaveTheme",
errSaveUser = "errSaveUser",
errSaveSidebarMinimized = "errSaveSidebarMinimized",
errSaveSidebar = "errSaveSidebar",
errGetSessions = "errGetSessions",
errGetTranslations = "errGetTranslations",
errGetSessionStatistics = "errGetSessionStatistics",
Expand All @@ -43,20 +43,20 @@ export namespace model {
this.message = source["message"];
}
}
export class GuiConfig {
export class GUIConfig {
locale: string;
theme: ThemeName;
sidebarMinified: boolean;
sidebar: boolean;

static createFrom(source: any = {}) {
return new GuiConfig(source);
return new GUIConfig(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.locale = source["locale"];
this.theme = source["theme"];
this.sidebarMinified = source["sidebarMinified"];
this.sidebar = source["sidebar"];
}
}
export class Localization {
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/williamsjokvist/cfn-tracker/pkg/config"
"github.com/williamsjokvist/cfn-tracker/pkg/model"
"github.com/williamsjokvist/cfn-tracker/pkg/server"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/nosql"
cfgDb "github.com/williamsjokvist/cfn-tracker/pkg/storage/config"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/sql"
"github.com/williamsjokvist/cfn-tracker/pkg/storage/txt"
"github.com/williamsjokvist/cfn-tracker/pkg/tracker/sf6/cfn"
Expand All @@ -47,7 +47,7 @@ var assets embed.FS
//go:embed gui/error/error.html
var errorTmpl []byte

var cfg config.Config
var cfg config.BuildConfig
var logFile *os.File

func logToFile() {
Expand All @@ -69,7 +69,7 @@ func init() {
err := godotenv.Load(`.env`)
if err != nil {
log.Println(fmt.Errorf(`missing .env file: %w`, err))
cfg = config.Config{
cfg = config.BuildConfig{
AppVersion: appVersion,
Headless: isProduction == `true`,
CapIDEmail: capIDEmail,
Expand Down Expand Up @@ -138,7 +138,7 @@ func main() {
closeWithError(fmt.Errorf("failed to initalize database: %w", err))
return
}
noSqlDb, err := nosql.NewStorage()
noSqlDb, err := cfgDb.NewStorage()
if err != nil {
closeWithError(fmt.Errorf("failed to initalize app config: %w", err))
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package config

type Config struct {
type BuildConfig struct {
AppVersion string `envconfig:"APP_VERSION" default:"0.0.0"`
Headless bool `envconfig:"HEADLESS" default:"true"`
BrowserSourcePort int `envconfig:"BROWSER_SOURCE_PORT" default:"4242"`
Expand Down
11 changes: 11 additions & 0 deletions pkg/model/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

type RuntimeConfig struct {
GUI GUIConfig `ini:"gui"`
}

type GUIConfig struct {
Locale string `ini:"locale" json:"locale"`
Theme ThemeName `ini:"theme" json:"theme"`
SideBar bool `ini:"sidebar" json:"sidebar"`
}
6 changes: 3 additions & 3 deletions pkg/model/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
tKeyErrGetGuiConfig ErrorLocalizationKey = "errGetGuiConfig"
tKeyErrSaveTheme ErrorLocalizationKey = "errSaveTheme"
tKeyErrSaveUser ErrorLocalizationKey = "errSaveUser"
tKeyErrSaveSidebarMinimized ErrorLocalizationKey = "errSaveSidebarMinimized"
tKeyErrSaveSidebar ErrorLocalizationKey = "errSaveSidebar"
tKeyErrGetSessions ErrorLocalizationKey = "errGetSessions"
tKeyErrGetTranslations ErrorLocalizationKey = "errGetTranslations"
tKeyErrGetSessionStatistics ErrorLocalizationKey = "errGetSessionStatistics"
Expand All @@ -43,7 +43,7 @@ var AllErrorKeys = []struct {
{tKeyErrGetGuiConfig, string(tKeyErrGetGuiConfig)},
{tKeyErrSaveTheme, string(tKeyErrSaveTheme)},
{tKeyErrSaveUser, string(tKeyErrSaveUser)},
{tKeyErrSaveSidebarMinimized, string(tKeyErrSaveSidebarMinimized)},
{tKeyErrSaveSidebar, string(tKeyErrSaveSidebar)},
{tKeyErrGetSessions, string(tKeyErrGetSessions)},
{tKeyErrGetTranslations, string(tKeyErrGetTranslations)},
{tKeyErrGetSessionStatistics, string(tKeyErrGetSessionStatistics)},
Expand All @@ -64,7 +64,7 @@ var (
ErrGetGUIConfig = newError(tKeyErrGetGuiConfig, errors.New("get gui config"))
ErrSaveTheme = newError(tKeyErrSaveTheme, errors.New("save theme"))
ErrSaveUser = newError(tKeyErrSaveUser, errors.New("save user"))
ErrSaveSidebarMinimized = newError(tKeyErrSaveSidebarMinimized, errors.New("save sidebar"))
ErrSaveSidebar = newError(tKeyErrSaveSidebar, errors.New("save sidebar"))
ErrGetSessions = newError(tKeyErrGetSessions, errors.New("get sessions"))
ErrGetTranslations = newError(tKeyErrGetTranslations, errors.New("get translations"))
ErrGetSessionStatistics = newError(tKeyErrGetSessionStatistics, errors.New("get session statistics"))
Expand Down
Loading

0 comments on commit 1e15327

Please sign in to comment.