Skip to content

Commit

Permalink
feat: context
Browse files Browse the repository at this point in the history
  • Loading branch information
Adis Durakovic committed Jan 13, 2024
1 parent e5f6522 commit d36545b
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 91 deletions.
9 changes: 0 additions & 9 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ type App struct {
settings *Settings
}

type BackupJob struct {
JobId uuid.UUID `json:"job_id"`
Schedule Schedule `json:"schedule"`
}

// NewApp creates a new App application struct
func NewApp(restic *Restic, scheduler *Scheduler, settings *Settings) *App {
return &App{restic: restic, scheduler: scheduler, settings: settings}
Expand Down Expand Up @@ -95,10 +90,6 @@ func (a *App) startup(ctx context.Context) {

}

func (a *App) GetBackupJobs() []BackupJob {
return a.scheduler.RunningJobs
}

func (a *App) StopBackup(id uuid.UUID) {
// a.scheduler.RemoveJob(id)
// a.RescheduleBackups()
Expand Down
9 changes: 1 addition & 8 deletions frontend/components/HeaderNavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,24 @@
<div class="">
<ul class="flex">
<li>
<!-- <NuxtLink to="/"><FaIcon icon="fire" />Dashboard</NuxtLink> -->
<UButton to="/" variant="ghost" :color="useRoute().path === '/' ? 'orange' : 'gray'" icon="i-heroicons-fire">Dashboard</UButton>
</li>
<li>
<UButton to="/repositories" variant="ghost" :color="useRoute().path.includes('repositories') ? 'purple' : 'gray'" icon="i-heroicons-server-stack">Repositories</UButton>
<!-- <NuxtLink to="/repositories" :class="useRoute().path.includes('repositories') ? 'active' : ''"><FaIcon icon="fa-server" />Repositories</NuxtLink> -->
</li>
<li>
<UButton to="/backups" variant="ghost" :color="useRoute().path.includes('backups') ? 'sky' : 'gray'" icon="i-heroicons-arrow-up-tray">Backups</UButton>

<!-- <NuxtLink to="/backups" :class="useRoute().path.includes('backups') ? 'active' : ''"><FaIcon icon="fa-upload" />Backups</NuxtLink> -->
</li>
<li>
<UButton to="/schedules" variant="ghost" :color="useRoute().path.includes('schedules') ? 'yellow' : 'gray'" icon="i-heroicons-clock"> Schedules </UButton>

<!-- <NuxtLink to="/schedules" :class="useRoute().path.includes('schedules') ? 'active' : ''"><FaIcon icon="clock" />Schedules</NuxtLink> -->
</li>
<li>
<UButton to="/logs" variant="ghost" :color="useRoute().path.includes('logs') ? 'blue' : 'gray'" icon="i-heroicons-clock">Logs</UButton>

<!-- <NuxtLink to="/schedules" :class="useRoute().path.includes('schedules') ? 'active' : ''"><FaIcon icon="clock" />Schedules</NuxtLink> -->
</li>
</ul>
</div>
</div>
<div class="bg-purple-800"></div>
</div>
</template>

Expand Down
26 changes: 18 additions & 8 deletions frontend/components/Schedule/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,24 @@
const items = (row: any) => [
[
{
label: 'Run now',
icon: 'i-heroicons-arrow-uturn-right',
click: async () => {
const t = await useApi().runSchedule(row.id)
console.log(t)
},
},
!useJobs().scheduleIsRunning(row.id)
? {
label: 'Run now',
icon: 'i-heroicons-arrow-uturn-right',
click: async () => {
const t = await useApi().runSchedule(row.id)
console.log(t)
},
}
: {
label: 'Stop',
icon: 'i-heroicons-arrow-uturn-right',
click: async () => {
const t = await useApi().stopSchedule(row.id)
console.log(t)
},
},
{
label: 'Delete',
icon: 'i-heroicons-trash',
Expand Down
2 changes: 2 additions & 0 deletions frontend/composables/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const useApi = defineStore('useApi', () => {

const statRepository = async (repoId: string) => (await useHttp.get(`/repositories/${repoId}/stats`)) ?? {}
const runSchedule = async (scheduleId: string) => (await useHttp.get(`/schedules/${scheduleId}/run`)) ?? {}
const stopSchedule = async (scheduleId: string) => (await useHttp.get(`/schedules/${scheduleId}/stop`)) ?? {}
const getConfig = async () => (await useHttp.get(`/config`)) ?? {}
const saveConfig = async (config: any) => (await useHttp.post(`/config`, config, { title: 'Settings', text: 'Settings saved successfully' })) ?? {}
const checkRepository = async (repo: any) => (await useHttp.post(`/check`, repo, { title: 'Check Repository', text: 'Repository can be used' })) ?? {}
Expand All @@ -29,6 +30,7 @@ export const useApi = defineStore('useApi', () => {
restoreFromSnapshot,
getSnapshots,
runSchedule,
stopSchedule,
mount,
unmount,
getConfig,
Expand Down
3 changes: 0 additions & 3 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';
import {uuid} from '../models';

export function GetBackupJobs():Promise<Array<main.BackupJob>>;

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

export function StopBackup(arg1:uuid.UUID):Promise<void>;
4 changes: 0 additions & 4 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT

export function GetBackupJobs() {
return window['go']['main']['App']['GetBackupJobs']();
}

export function SelectDirectory(arg1) {
return window['go']['main']['App']['SelectDirectory'](arg1);
}
Expand Down
18 changes: 0 additions & 18 deletions frontend/wailsjs/go/models.ts

This file was deleted.

27 changes: 20 additions & 7 deletions restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ type FileDescriptor struct {
Mtime string `json:"mtime"`
}

func (r *Restic) core(repository Repository, cmd []string, envs []string) (string, error) {
func (r *Restic) core(
repository Repository,
cmd []string,
envs []string,
sctx *ScheduleContext,
) (string, error) {

cmds := []string{"-r", repository.Path, "--json"}
cmds = append(cmds, cmd...)
var sout bytes.Buffer
var serr bytes.Buffer
c := exec.Command("/usr/bin/restic", cmds...)
var c *exec.Cmd
if sctx != nil {
c = exec.CommandContext(sctx.Ctx, "/usr/bin/restic", cmds...)
defer sctx.Cancel()
} else {
c = exec.Command("/usr/bin/restic", cmds...)
}
c.Stderr = &serr
c.Stdout = &sout
c.Env = append(
Expand All @@ -102,7 +113,7 @@ func (r *Restic) core(repository Repository, cmd []string, envs []string) (strin
}

func (r *Restic) Exec(repository Repository, cmds []string, envs []string) (string, error) {
if data, err := r.core(repository, cmds, envs); err != nil {
if data, err := r.core(repository, cmds, envs, nil); err != nil {
return "", err
} else {
return data, nil
Expand All @@ -114,7 +125,7 @@ func (r *Restic) BrowseSnapshot(
snapshotId string,
path string,
) ([]FileDescriptor, error) {
if res, err := r.core(repository, []string{"ls", "-l", "--human-readable", snapshotId, path}, []string{}); err == nil {
if res, err := r.core(repository, []string{"ls", "-l", "--human-readable", snapshotId, path}, []string{}, nil); err == nil {
res = strings.ReplaceAll(res, "}", "},")
res = strings.ReplaceAll(res, "\n", "")
res = "[" + res + "]"
Expand All @@ -134,11 +145,13 @@ func (r *Restic) BrowseSnapshot(
}

func (r *Restic) RunSchedule(
sctx *ScheduleContext,
action string,
backup *Backup,
toRepository *Repository,
fromRepository *Repository,
) {

switch action {
case "backup":
if backup == nil || toRepository == nil {
Expand All @@ -152,7 +165,7 @@ func (r *Restic) RunSchedule(

fmt.Println(cmds)

_, err := r.core(*toRepository, cmds, []string{})
_, err := r.core(*toRepository, cmds, []string{}, sctx)
if err != nil {
fmt.Println(err)
}
Expand All @@ -177,9 +190,9 @@ func (r *Restic) RunSchedule(
for _, p := range toRepository.PruneParams {
cmds = append(cmds, p...)
}
_, err := r.core(*toRepository, []string{"unlock"}, []string{})
_, err := r.core(*toRepository, []string{"unlock"}, []string{}, sctx)
if err == nil {
_, err := r.core(*toRepository, cmds, []string{})
_, err := r.core(*toRepository, cmds, []string{}, sctx)
if err != nil {
fmt.Println(err)
}
Expand Down
Loading

0 comments on commit d36545b

Please sign in to comment.