Handle actions
This commit is contained in:
parent
299f3ba0cf
commit
b125a3cd00
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
actions
|
||||
gongs
|
||||
reveil
|
||||
tracks
|
||||
|
@ -1,37 +1,93 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.nemunai.re/nemunaire/reveil/config"
|
||||
"git.nemunai.re/nemunaire/reveil/model"
|
||||
)
|
||||
|
||||
func declareActionsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
||||
router.GET("/actions", func(c *gin.Context) {
|
||||
actions, err := reveil.LoadActions(cfg)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, actions)
|
||||
})
|
||||
router.POST("/actions", func(c *gin.Context) {
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusNotImplemented, gin.H{"errmsg": "TODO"})
|
||||
})
|
||||
|
||||
actionsRoutes := router.Group("/actions/:gid")
|
||||
actionsRoutes.Use(actionHandler)
|
||||
actionsRoutes := router.Group("/actions/:tid")
|
||||
actionsRoutes.Use(func(c *gin.Context) {
|
||||
actions, err := reveil.LoadActions(cfg)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
for _, t := range actions {
|
||||
if t.Id.ToString() == c.Param("tid") {
|
||||
c.Set("action", t)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Action not found"})
|
||||
})
|
||||
|
||||
actionsRoutes.GET("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("action"))
|
||||
})
|
||||
actionsRoutes.PUT("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("action"))
|
||||
oldaction := c.MustGet("action").(*reveil.Action)
|
||||
|
||||
var action reveil.Action
|
||||
if err := c.ShouldBindJSON(&action); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if action.Name != oldaction.Name {
|
||||
err := oldaction.Rename(action.Name)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to rename the action: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if action.Enabled != oldaction.Enabled {
|
||||
var err error
|
||||
if action.Enabled {
|
||||
err = oldaction.Enable()
|
||||
} else {
|
||||
err = oldaction.Disable()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to enable/disable the action: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, oldaction)
|
||||
})
|
||||
actionsRoutes.DELETE("", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, c.MustGet("action"))
|
||||
action := c.MustGet("action").(*reveil.Action)
|
||||
|
||||
err := action.Remove()
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to remove the action: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, nil)
|
||||
})
|
||||
}
|
||||
|
||||
func actionHandler(c *gin.Context) {
|
||||
c.Set("action", nil)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
|
123
model/action.go
Normal file
123
model/action.go
Normal file
@ -0,0 +1,123 @@
|
||||
package reveil
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha512"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.nemunai.re/nemunaire/reveil/config"
|
||||
)
|
||||
|
||||
type Action struct {
|
||||
Id Identifier `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Path string `json:"path"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
func LoadAction(path string) (string, string, error) {
|
||||
fd, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
fileScanner := bufio.NewScanner(fd)
|
||||
fileScanner.Split(bufio.ScanLines)
|
||||
|
||||
var (
|
||||
shebang = false
|
||||
name = ""
|
||||
description = ""
|
||||
)
|
||||
for fileScanner.Scan() {
|
||||
line := strings.TrimSpace(fileScanner.Text())
|
||||
if !shebang {
|
||||
if len(line) < 2 || line[0] != '#' || line[1] != '!' {
|
||||
return name, description, errors.New("Not a valid action file (shebang not found).")
|
||||
}
|
||||
shebang = true
|
||||
continue
|
||||
}
|
||||
|
||||
if len(line) < 2 || line[0] != '#' {
|
||||
if len(description) > 0 {
|
||||
return name, strings.TrimSpace(description), nil
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if len(name) == 0 {
|
||||
name = strings.TrimSpace(line[1:])
|
||||
} else {
|
||||
description += strings.TrimSpace(line[1:]) + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
return name, description, nil
|
||||
}
|
||||
|
||||
func LoadActions(cfg *config.Config) (actions []*Action, err error) {
|
||||
err = filepath.Walk(cfg.ActionsDir, func(path string, d fs.FileInfo, err error) error {
|
||||
if d.Mode().IsRegular() {
|
||||
hash := sha512.Sum512([]byte(path))
|
||||
|
||||
// Parse content
|
||||
name, description, err := LoadAction(path)
|
||||
if err != nil {
|
||||
log.Printf("Invalid action file (trying to parse %s): %s", path, err.Error())
|
||||
// Ignore invalid files
|
||||
return nil
|
||||
}
|
||||
|
||||
if description == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
actions = append(actions, &Action{
|
||||
Id: hash[:],
|
||||
Name: name,
|
||||
Description: description,
|
||||
Path: path,
|
||||
Enabled: d.Mode().Perm()&0111 != 0,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Action) Rename(name string) error {
|
||||
return errors.New("Not implemeted")
|
||||
}
|
||||
|
||||
func (a *Action) Enable() error {
|
||||
fi, err := os.Stat(a.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Chmod(a.Path, fi.Mode().Perm()|0111)
|
||||
}
|
||||
|
||||
func (a *Action) Disable() error {
|
||||
fi, err := os.Stat(a.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Chmod(a.Path, fi.Mode().Perm()&0666)
|
||||
}
|
||||
|
||||
func (a *Action) Remove() error {
|
||||
return os.Remove(a.Path)
|
||||
}
|
77
ui/src/components/ActionList.svelte
Normal file
77
ui/src/components/ActionList.svelte
Normal file
@ -0,0 +1,77 @@
|
||||
<script>
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/stores';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Icon,
|
||||
Spinner,
|
||||
} from 'sveltestrap';
|
||||
|
||||
import { actions } from '../stores/actions';
|
||||
|
||||
export let flush = false;
|
||||
|
||||
export { className as class };
|
||||
let className = '';
|
||||
|
||||
let refreshInProgress = false;
|
||||
function refresh_actions() {
|
||||
refreshInProgress = true;
|
||||
actions.refresh().then(() => {
|
||||
refreshInProgress = false;
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center" class:px-2={flush}>
|
||||
<h2>
|
||||
Actions
|
||||
</h2>
|
||||
<div>
|
||||
{#if !flush}
|
||||
<Button
|
||||
href="routines/actions"
|
||||
color="outline-info"
|
||||
size="sm"
|
||||
>
|
||||
<Icon name="pencil" />
|
||||
</Button>
|
||||
{/if}
|
||||
<Button
|
||||
color="outline-dark"
|
||||
size="sm"
|
||||
title="Rafraîchir la liste des actions"
|
||||
on:click={refresh_actions}
|
||||
disabled={refreshInProgress}
|
||||
>
|
||||
{#if !refreshInProgress}
|
||||
<Icon name="arrow-clockwise" />
|
||||
{:else}
|
||||
<Spinner color="dark" size="sm" />
|
||||
{/if}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="list-group {className}" class:list-group-flush={flush}>
|
||||
{#if $actions.list}
|
||||
{#each $actions.list as action (action.id)}
|
||||
<a
|
||||
href="routines/actions/{action.id}"
|
||||
class="list-group-item list-group-item-action"
|
||||
class:active={$page.url.pathname.indexOf('/actions/') !== -1 && $page.params.aid == action.id}
|
||||
aria-current="true"
|
||||
>
|
||||
<span class:fw-bold={action.enabled}>{action.name}</span>
|
||||
</a>
|
||||
{/each}
|
||||
{:else}
|
||||
{#await actions.refresh()}
|
||||
<div class="d-flex justify-content-center align-items-center gap-2">
|
||||
<Spinner color="primary" /> Chargement en cours…
|
||||
</div>
|
||||
{:then}
|
||||
test
|
||||
{/await}
|
||||
{/if}
|
||||
</div>
|
66
ui/src/lib/action.js
Normal file
66
ui/src/lib/action.js
Normal file
@ -0,0 +1,66 @@
|
||||
export class Action {
|
||||
constructor(res) {
|
||||
if (res) {
|
||||
this.update(res);
|
||||
}
|
||||
}
|
||||
|
||||
update({ id, name, description, path, enabled }) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.path = path;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
async delete() {
|
||||
const res = await fetch(`api/actions/${this.id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {'Accept': 'application/json'}
|
||||
});
|
||||
if (res.status == 200) {
|
||||
return true;
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
toggleEnable() {
|
||||
this.enabled = !this.enabled;
|
||||
this.save();
|
||||
return this;
|
||||
}
|
||||
|
||||
async save() {
|
||||
const res = await fetch(this.id?`api/actions/${this.id}`:'api/actions', {
|
||||
method: this.id?'PUT':'POST',
|
||||
headers: {'Accept': 'application/json'},
|
||||
body: JSON.stringify(this),
|
||||
});
|
||||
if (res.status == 200) {
|
||||
const data = await res.json();
|
||||
this.update(data);
|
||||
return data;
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function getActions() {
|
||||
const res = await fetch(`api/actions`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return (await res.json()).map((t) => new Action(t));
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAction(aid) {
|
||||
const res = await fetch(`api/actions/${aid}`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return new Action(await res.json());
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
@ -8,20 +8,28 @@
|
||||
} from 'sveltestrap';
|
||||
|
||||
import CardRoutine from '../../components/CardRoutine.svelte';
|
||||
import ActionList from '../../components/ActionList.svelte';
|
||||
</script>
|
||||
|
||||
<Container fluid class="flex-fill d-flex flex-column py-2">
|
||||
<Row cols={{xs: 1, md: 2, lg: 3}}>
|
||||
<Col class="mb-4">
|
||||
<CardRoutine />
|
||||
<Row>
|
||||
<Col md="8">
|
||||
<Row cols={{xs: 1, lg: 2, xl: 3}}>
|
||||
<Col class="mb-4">
|
||||
<CardRoutine />
|
||||
</Col>
|
||||
<Col class="mb-4">
|
||||
<Card
|
||||
class="h-100 d-flex justify-content-center align-items-center fst-italic"
|
||||
style="cursor: pointer; border-style: dashed; min-height: 5em;"
|
||||
>
|
||||
Ajouter une routine …
|
||||
</Card>
|
||||
</Col>
|
||||
</Row>
|
||||
</Col>
|
||||
<Col class="mb-4">
|
||||
<Card
|
||||
class="h-100 d-flex justify-content-center align-items-center fst-italic"
|
||||
style="cursor: pointer; border-style: dashed;"
|
||||
>
|
||||
Ajouter une routine …
|
||||
</Card>
|
||||
<Col md="4">
|
||||
<ActionList class="mb-5" />
|
||||
</Col>
|
||||
</Row>
|
||||
</Container>
|
||||
|
36
ui/src/stores/actions.js
Normal file
36
ui/src/stores/actions.js
Normal file
@ -0,0 +1,36 @@
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
import { getActions } from '../lib/action'
|
||||
|
||||
function createActionsStore() {
|
||||
const { subscribe, set, update } = writable({list: null});
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
|
||||
set: (v) => {
|
||||
update((m) => Object.assign(m, v));
|
||||
},
|
||||
|
||||
refresh: async () => {
|
||||
const list = await getActions();
|
||||
update((m) => Object.assign(m, {list}));
|
||||
return list;
|
||||
},
|
||||
|
||||
update: (res_actions, cb=null) => {
|
||||
if (res_actions.status === 200) {
|
||||
res_actions.json().then((list) => {
|
||||
update((m) => (Object.assign(m, {list})));
|
||||
|
||||
if (cb) {
|
||||
cb(list);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
export const actions = createActionsStore();
|
Loading…
x
Reference in New Issue
Block a user