Skip to content

Commit

Permalink
Add features (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdouchement authored Jan 26, 2024
1 parent 601c0e0 commit 9ead18c
Show file tree
Hide file tree
Showing 17 changed files with 722 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
version: '3'

vars:
VERSION: 0.3.0
VERSION: 0.4.0
REVISION: { sh: git rev-parse HEAD }

env:
Expand Down
16 changes: 8 additions & 8 deletions cmd/shigoto/daemon/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"bytes"
"fmt"
"log/slog"
"os"
"os/signal"
"path/filepath"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/mdouchement/shigoto/internal/cron"
"github.com/mdouchement/shigoto/internal/socket"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -47,16 +47,17 @@ var (
return err
}

log := logrus.New()
log.SetFormatter(&logger.LogrusTextFormatter{
l := slog.New(logger.NewSlogTextHandler(os.Stdout, &logger.SlogTextOption{
Level: slog.LevelInfo,
DisableColors: !konf.Bool("log.force_color"),
ForceColors: konf.Bool("log.force_color"),
ForceFormatting: konf.Bool("log.force_formating"),
PrefixRE: regexp.MustCompile(`^(\[.*?\])\s`),
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
pool := cron.New(logger.WrapLogrus(log))
}))
log := logger.WrapSlog(l)
pool := cron.New(log)

//
//
Expand All @@ -69,7 +70,7 @@ var (
if bytes.Equal(event, socket.SignalReload) {
log.Info("Reloading daemon")

err := cron.Load(filepath.Join(konf.String("directory")), pool, logger.WrapLogrus(log))
err := cron.Load(filepath.Join(konf.String("directory")), pool, log)
if err != nil {
log.WithError(err).Error("Fail to reloading")
return []byte(err.Error())
Expand All @@ -80,7 +81,6 @@ var (
}
return []byte("Unsupported signal")
})

if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -90,7 +90,7 @@ var (
//
//

err := cron.Load(filepath.Join(konf.String("directory")), pool, logger.WrapLogrus(log))
err := cron.Load(filepath.Join(konf.String("directory")), pool, log)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/shigoto/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package run

import (
"fmt"
"log/slog"
"os"
"regexp"
"slices"

"github.com/mdouchement/logger"
"github.com/mdouchement/shigoto/pkg/runner"
"github.com/mdouchement/shigoto/pkg/shigoto"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -18,15 +19,15 @@ var Command = &cobra.Command{
Short: "Run (oneshot) the given shigoto file",
Args: cobra.MinimumNArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
logrus := logrus.New()
logrus.SetFormatter(&logger.LogrusTextFormatter{
l := slog.New(logger.NewSlogTextHandler(os.Stdout, &logger.SlogTextOption{
Level: slog.LevelInfo,
ForceColors: true,
ForceFormatting: true,
PrefixRE: regexp.MustCompile(`^(\[.*?\])\s`),
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
log := logger.WrapLogrus(logrus)
}))
log := logger.WrapSlog(l)

//

Expand Down
68 changes: 67 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- [1.2.2. HTTP](#122-http)
- [1.2.3. Shell](#123-shell)
- [1.2.4. Yaegi](#124-yaegi)
- [1.2.5. Defer](#125-defer)
- [1.2.6. Tengo](#126-tengo)

<!-- /TOC -->

Expand Down Expand Up @@ -166,7 +168,7 @@ shigoto:
baito_yaegi:
schedule: "@every 5s"
commands:
# Run a golang file compatible with Yaegi.
# Run a Golang file compatible with Yaegi.
- yaegi: $HOME/main.go
# IgnoreError allows errors and continue to the next command.
# (default: false)
Expand All @@ -186,4 +188,68 @@ shigoto:
# (default: false)
ignore_error: true
```

### 1.2.5. Defer

With the `defer` keyword, it's possible to schedule cleanup to be run once the non deferred commands are completed.
The difference with just putting it as the last command is that this command will run even when the task fails.

```yml
shigoto:
baito_defer:
schedule: "@every 5s"
commands:
- defer: echo "defer 0"
- defer: { exec: echo "defer 1" }
- defer:
sh: echo "defer 2"
- echo "shigoto"
```

> Due to the nature of how the Go's own defer work, the deferred commands are executed in the reverse order if you schedule multiple of them.

### 1.2.6. Tengo

[Tengo](https://github.com/d5/tengo) allows to write Tengo scripts. It also uses extra libs/overwrites from [LDT](https://github.com/mdouchement/ldt).
Special import is `logger` that use the Shigoto [logger](https://godoc.org/github.com/mdouchement/logger#Logger).

- Supports global/local templating variables as source.
- Supports host/global/local envrironment variables as source.

```yml
variables:
TENGO_LIST: |
[
"string 0",
"string 1",
"string 2"
]
shigoto:
baito_tengo:
schedule: "@every 5s"
commands:
# Run a Tengo file (*.tengo or *.tgo).
- tengo: ~/shigoto.tengo
# IgnoreError allows errors and continue to the next command.
# (default: false)
ignore_error: true
# Declare Tengo source code directly.
- tengo: |
fmt := import("fmt")
list := {{.TENGO_LIST}}
for v in list {
fmt.println(v)
}
log := shigoto.logger
log = log.with_prefix("[trololo]")
log.info("done")
# IgnoreError allows errors and continue to the next command.
# (default: false)
ignore_error: true
```
26 changes: 18 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ replace github.com/imdario/mergo => github.com/darccio/mergo v0.3.11

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/d5/tengo/v2 v2.16.1
github.com/gobs/args v0.0.0-20210311043657-b8c0b223be93
github.com/knadh/koanf v1.5.0
github.com/mdouchement/logger v0.0.0-20231201121128-2bebbcf0fdea
github.com/mdouchement/ldt v0.9.1
github.com/mdouchement/logger v0.0.0-20240111140701-71e4ee17e98e
github.com/mdouchement/upathex v0.1.0
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.3
github.com/slok/goresilience v0.2.0
github.com/spf13/cobra v1.8.0
github.com/traefik/yaegi v0.15.1
Expand All @@ -22,32 +24,40 @@ require (
require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/direnv/direnv/v2 v2.33.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
github.com/vbauerster/mpb/v8 v8.7.1 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 9ead18c

Please sign in to comment.