Skip to content

Commit

Permalink
chore: refactor new folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Adis Durakovic committed Mar 8, 2024
1 parent 339c121 commit 676f7ed
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 198 deletions.
129 changes: 16 additions & 113 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"embed"
"os"
"sync"
"time"

"github.com/ad-on-is/resticity/internal"

"github.com/charmbracelet/log"
"github.com/energye/systray"
"github.com/go-co-op/gocron/v2"
Expand All @@ -18,117 +19,19 @@ import (
// App struct
type App struct {
ctx context.Context
scheduler *Scheduler
restic *Restic
settings *Settings
scheduler *internal.Scheduler
restic *internal.Restic
settings *internal.Settings
assets *embed.FS
}

type Settings struct {
file string
Config Config `json:"config"`
mux sync.Mutex
}

type S3Options struct {
S3Key string `json:"s3_key"`
S3Secret string `json:"s3_secret"`
}

type AzureOptions struct {
AzureAccountName string `json:"azure_account_name"`
AzureAccountKey string `json:"azure_account_key"`
AzureAccountSas string `json:"azure_account_sas"`
}

type GcsOptions struct {
GoogleProjectId string `json:"google_project_id"`
GoogleApplicationCredentials string `json:"google_application_credentials"`
}

type Options struct {
S3Options
AzureOptions
GcsOptions
}

type GroupKey struct {
Hostname string `json:"hostname"`
Paths []string `json:"paths"`
Tags []string `json:"tags"`
}

type SnapshotGroup struct {
GroupKey GroupKey `json:"group_key"`
Snapshots []Snapshot `json:"snapshots"`
}

type Snapshot struct {
Id string `json:"id"`
Time time.Time `json:"time"`
Paths []string `json:"paths"`
Hostname string `json:"hostname"`
Username string `json:"username"`
UID uint32 `json:"uid"`
GID uint32 `json:"gid"`
ShortId string `json:"short_id"`
Tags []string `json:"tags"`
ProgramVersion string `json:"program_version"`
}

type FileDescriptor struct {
Name string `json:"name"`
Type string `json:"type"`
Path string `json:"path"`
Size uint32 `json:"size"`
Mtime string `json:"mtime"`
}

type Mount struct {
RepositoryId string `json:"repository_id"`
Path string `json:"path"`
}

type Repository struct {
Id string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
PruneParams [][]string `json:"prune_params"`
Path string `json:"path"`
Password string `json:"password"`
Options Options `json:"options"`
}

type Backup struct {
Id string `json:"id"`
Path string `json:"path"`
Name string `json:"name"`
Cron string `json:"cron"`
BackupParams [][]string `json:"backup_params"`
Targets []string `json:"targets"`
}

type Schedule struct {
Id string `json:"id"`
Action string `json:"action"`
BackupId string `json:"backup_id"`
ToRepositoryId string `json:"to_repository_id"`
FromRepositoryId string `json:"from_repository_id"`
Cron string `json:"cron"`
Active bool `json:"active"`
LastRun string `json:"last_run"`
LastError string `json:"last_error"`
}

type Config struct {
Mounts []Mount `json:"mounts"`
Repositories []Repository `json:"repositories"`
Backups []Backup `json:"backups"`
Schedules []Schedule `json:"schedules"`
}

// NewApp creates a new App application struct
func NewApp(restic *Restic, scheduler *Scheduler, settings *Settings, assets *embed.FS) *App {
func NewApp(
restic *internal.Restic,
scheduler *internal.Scheduler,
settings *internal.Settings,
assets *embed.FS,
) *App {
return &App{restic: restic, scheduler: scheduler, settings: settings}
}

Expand All @@ -140,13 +43,13 @@ func (a *App) toggleSysTrayIcon() {
"/home/adonis/Development/Go/resticity/frontend/public/appicon_active.png",
)

_, err := a.scheduler.gocron.NewJob(
_, err := a.scheduler.Gocron.NewJob(
gocron.DurationJob(500*time.Millisecond),
gocron.NewTask(func() {
running := funk.Filter(
a.scheduler.Jobs,
func(j Job) bool { return j.Running == true },
).([]Job)
func(j internal.Job) bool { return j.Running == true },
).([]internal.Job)
if len(running) > 0 {
systray.SetIcon(active_icon)
} else {
Expand Down Expand Up @@ -223,6 +126,6 @@ func (a *App) SelectFile(title string) string {
return ""
}

func (a *App) FakeCreateForModels() (SnapshotGroup, Repository, Backup, Config, Schedule, FileDescriptor) {
return SnapshotGroup{}, Repository{}, Backup{}, Config{}, Schedule{}, FileDescriptor{}
func (a *App) FakeCreateForModels() (internal.SnapshotGroup, internal.Repository, internal.Backup, internal.Config, internal.Schedule, internal.FileDescriptor) {
return internal.SnapshotGroup{}, internal.Repository{}, internal.Backup{}, internal.Config{}, internal.Schedule{}, internal.FileDescriptor{}
}
32 changes: 32 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"bytes"
"flag"

"github.com/ad-on-is/resticity/internal"

"github.com/charmbracelet/log"
)

func main() {
log.SetLevel(log.DebugLevel)
flagConfigFile := ""
flag.StringVar(&flagConfigFile, "config", "", "Specify a config file")
flag.StringVar(&flagConfigFile, "c", "", "Specify a config file")
flag.Parse()
log.Info("settings file", flagConfigFile)
errb := bytes.NewBuffer([]byte{})
outb := bytes.NewBuffer([]byte{})
outputChan := make(chan internal.ChanMsg)
settings := internal.NewSettings(flagConfigFile)
restic := internal.NewRestic(errb, outb, settings)
if scheduler, err := internal.NewScheduler(settings, restic, &outputChan); err == nil {
(scheduler).RescheduleBackups()
internal.RunServer(scheduler, restic, settings, errb, outb, &outputChan)

} else {
log.Error("Init scheduler", "error", err)
}

}
13 changes: 0 additions & 13 deletions com.adisdurakovic.Resticity.yml

This file was deleted.

4 changes: 2 additions & 2 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';
import {internal} from '../models';
import {uuid} from '../models';

export function FakeCreateForModels():Promise<main.SnapshotGroup>;
export function FakeCreateForModels():Promise<internal.SnapshotGroup>;

export function SelectDirectory(arg1:string):Promise<string>;

Expand Down
2 changes: 1 addition & 1 deletion frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export namespace main {
export namespace internal {

export interface Backup {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module resticity
module github.com/ad-on-is/resticity

go 1.21

Expand Down
14 changes: 0 additions & 14 deletions helpers.go

This file was deleted.

2 changes: 1 addition & 1 deletion restic.go → internal/restic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"bufio"
Expand Down
10 changes: 5 additions & 5 deletions scheduler.go → internal/scheduler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"context"
Expand All @@ -23,7 +23,7 @@ type Job struct {
}

type Scheduler struct {
gocron gocron.Scheduler
Gocron gocron.Scheduler
restic *Restic
Jobs []Job
jmu sync.Mutex
Expand All @@ -38,8 +38,8 @@ func NewScheduler(settings *Settings, restic *Restic, ch *chan ChanMsg) (*Schedu
s.restic = restic
s.outputCh = ch
if gc, err := gocron.NewScheduler(); err == nil {
s.gocron = gc
s.gocron.Start()
s.Gocron = gc
s.Gocron.Start()
return s, nil
} else {
return nil, err
Expand Down Expand Up @@ -142,7 +142,7 @@ func (s *Scheduler) RescheduleBackups() {
jobDef = gocron.CronJob(schedule.Cron, false)
}

j, err := s.gocron.NewJob(
j, err := s.Gocron.NewJob(
jobDef,
gocron.NewTask(func() {

Expand Down
28 changes: 1 addition & 27 deletions server.go → internal/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"bytes"
Expand All @@ -15,32 +15,6 @@ import (
"github.com/thoas/go-funk"
)

type BrowseData struct {
Path string `json:"path"`
}

type MountData struct {
Path string `json:"path"`
}

type RestoreData struct {
RootPath string `json:"root_path"`
FromPath string `json:"from_path"`
ToPath string `json:"to_path"`
}

type Output struct {
Id string `json:"id"`
Out any `json:"out"`
}

type MsgJob struct {
Id string `json:"id"`
Schedule Schedule `json:"schedule"`
Running bool `json:"running"`
Force bool `json:"force"`
}

func RunServer(
scheduler *Scheduler,
restic *Restic,
Expand Down
2 changes: 1 addition & 1 deletion settings.go → internal/settings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package internal

import (
"encoding/json"
Expand Down
Loading

0 comments on commit 676f7ed

Please sign in to comment.