Initial commit, pt. 70

This commit is contained in:
Dmitrii Okunev
2024-08-04 18:46:38 +01:00
parent 5cc9aed3a7
commit 2750bd7c06
6 changed files with 32 additions and 36 deletions

View File

@@ -5,4 +5,4 @@ Website = "https://github.com/xaionaro/streamctl"
Name = "streampanel"
ID = "center.dx.streampanel"
Version = "0.1.0"
Build = 85
Build = 87

View File

@@ -29,7 +29,11 @@ func getContext(
l := xlogrus.New(ll).WithLevel(logger.Level(flags.LoggerLevel))
if flags.LogFile != "" {
logPath, err := xpath.Expand(flags.LogFile)
logPathUnexpanded := flags.LogFile
if flags.Subprocess != "" {
logPathUnexpanded += "-" + flags.Subprocess
}
logPath, err := xpath.Expand(logPathUnexpanded)
if err != nil {
l.Errorf("unable to expand path '%s': %w", flags.LogFile, err)
} else {

View File

@@ -95,6 +95,14 @@ func initRuntime(
runtime.GOMAXPROCS(16)
}
observability.Go(ctx, func() {
t := time.NewTicker(time.Second)
for {
<-t.C
belt.Flush(ctx)
}
})
ctx, cancelFn := context.WithCancel(ctx)
return ctx, func() {
defer belt.Flush(ctx)

View File

@@ -3,6 +3,7 @@ package observability
import (
"context"
"fmt"
"time"
"github.com/facebookincubator/go-belt"
"github.com/facebookincubator/go-belt/tool/experimental/errmon"
@@ -14,5 +15,6 @@ func PanicIfNotNil(ctx context.Context, r any) {
}
errmon.ObserveRecoverCtx(ctx, r)
belt.Flush(ctx)
time.Sleep(time.Second)
panic(fmt.Sprintf("%#+v", r))
}

View File

@@ -2,12 +2,13 @@ package config
import (
"bytes"
"encoding/json"
"fmt"
"io"
goyaml "github.com/go-yaml/yaml"
"github.com/goccy/go-yaml"
"github.com/xaionaro-go/datacounter"
goyaml "gopkg.in/yaml.v3"
)
var _ io.Writer = (*Config)(nil)
@@ -33,22 +34,31 @@ func (cfg Config) WriteTo(
}
func (cfg Config) MarshalYAML() ([]byte, error) {
b, err := yaml.Marshal((config)(cfg))
var buf bytes.Buffer
// There is bug in github.com/goccy/go-yaml that makes wrong intention
// in cfg.BuiltinStreamD.GitRepo.PrivateKey makes the whole value unparsable
//
// Working this around...
opt := yaml.CustomMarshaler(func(v string) ([]byte, error) {
fmt.Println(v)
return json.Marshal(v)
})
encoder := yaml.NewEncoder(&buf, opt)
err := encoder.Encode((config)(cfg))
if err != nil {
return nil, fmt.Errorf("unable to serialize data %#+v: %w", cfg, err)
}
// have to use another YAML encoder to avoid the random-indent bug,
// but also have to use the initial encoder to correctly map
// out structures to YAML; so using both sequentially :(
m := map[string]any{}
err = goyaml.Unmarshal(b, &m)
err = goyaml.Unmarshal(buf.Bytes(), &m)
if err != nil {
return nil, fmt.Errorf("unable to unserialize data %s: %w", b, err)
return nil, fmt.Errorf("unable to unserialize data %s: %w", buf.Bytes(), err)
}
b, err = goyaml.Marshal(m)
b, err := goyaml.Marshal(m)
if err != nil {
return nil, fmt.Errorf("unable to re-serialize data %#+v: %w", m, err)
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"io"
goyaml "github.com/go-yaml/yaml"
"github.com/goccy/go-yaml"
"github.com/xaionaro-go/datacounter"
)
@@ -21,38 +20,11 @@ func (cfg Config) Write(b []byte) (int, error) {
func (cfg Config) WriteTo(
w io.Writer,
) (int64, error) {
// There is bug in github.com/goccy/go-yaml that makes wrong intention
// in cfg.BuiltinStreamD.GitRepo.PrivateKey makes the whole value unparsable
//
// Working this around...
key := cfg.BuiltinStreamD.GitRepo.PrivateKey
cfg.BuiltinStreamD.GitRepo.PrivateKey = ""
b, err := yaml.Marshal(cfg)
if err != nil {
return 0, fmt.Errorf("unable to serialize data %#+v: %w", cfg, err)
}
m := map[any]any{}
err = goyaml.Unmarshal(b, &m)
if err != nil {
return 0, fmt.Errorf("unable to unserialize data %s: %w", b, err)
}
if v, ok := m["streamd_builtin"]; ok {
if m2, ok := v.(map[any]any); ok {
if v, ok := m2["gitrepo"]; ok {
if m3, ok := v.(map[any]any); ok {
m3["private_key"] = key
}
}
}
}
b, err = goyaml.Marshal(m)
if err != nil {
return 0, fmt.Errorf("unable to re-serialize data %#+v: %w", m, err)
}
counter := datacounter.NewWriterCounter(w)
io.Copy(counter, bytes.NewReader(b))
return int64(counter.Count()), nil