feat: eliminate fmt, string concat — add core.Println, use Concat/Path everywhere
New primitive: core.Println() wraps fmt.Println.
Replaced across all test + example files:
- fmt.Println → Println (17 example files)
- fmt.Sprintf → Concat + Sprint
- dir + "/file" → Path(dir, "file") (path security)
- "str" + var → Concat("str", var) (AX consistency)
"fmt" import is now zero across all test files.
String concat with + is zero across all test files.
Remaining 9 stdlib imports (all Go infrastructure):
testing, context, time, sync, embed, io/fs, bytes, gzip, base64
558 tests, 84.5% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
921b4f2b21
commit
5be20af4b0
23 changed files with 140 additions and 150 deletions
|
|
@ -2,7 +2,6 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -16,16 +15,16 @@ func ExampleAction_Run() {
|
||||||
r := c.Action("double").Run(context.Background(), NewOptions(
|
r := c.Action("double").Run(context.Background(), NewOptions(
|
||||||
Option{Key: "n", Value: 21},
|
Option{Key: "n", Value: 21},
|
||||||
))
|
))
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output: 42
|
// Output: 42
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleAction_Exists() {
|
func ExampleAction_Exists() {
|
||||||
c := New()
|
c := New()
|
||||||
fmt.Println(c.Action("missing").Exists())
|
Println(c.Action("missing").Exists())
|
||||||
|
|
||||||
c.Action("present", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
c.Action("present", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
||||||
fmt.Println(c.Action("present").Exists())
|
Println(c.Action("present").Exists())
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
|
|
@ -38,7 +37,7 @@ func ExampleAction_Run_panicRecovery() {
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Action("boom").Run(context.Background(), NewOptions())
|
r := c.Action("boom").Run(context.Background(), NewOptions())
|
||||||
fmt.Println(r.OK)
|
Println(r.OK)
|
||||||
// Output: false
|
// Output: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,6 +54,6 @@ func ExampleAction_Run_entitlementDenied() {
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Action("premium").Run(context.Background(), NewOptions())
|
r := c.Action("premium").Run(context.Background(), NewOptions())
|
||||||
fmt.Println(r.OK)
|
Println(r.OK)
|
||||||
// Output: false
|
// Output: false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ func TestAction_NamedAction_Good_Invoke(t *testing.T) {
|
||||||
c := New()
|
c := New()
|
||||||
c.Action("git.log", func(_ context.Context, opts Options) Result {
|
c.Action("git.log", func(_ context.Context, opts Options) Result {
|
||||||
dir := opts.String("dir")
|
dir := opts.String("dir")
|
||||||
return Result{Value: "log from " + dir, OK: true}
|
return Result{Value: Concat("log from ", dir), OK: true}
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Action("git.log").Run(context.Background(), NewOptions(
|
r := c.Action("git.log").Run(context.Background(), NewOptions(
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -12,14 +11,14 @@ func ExampleAPI_RegisterProtocol() {
|
||||||
c.API().RegisterProtocol("http", func(h *DriveHandle) (Stream, error) {
|
c.API().RegisterProtocol("http", func(h *DriveHandle) (Stream, error) {
|
||||||
return &mockStream{response: []byte("pong")}, nil
|
return &mockStream{response: []byte("pong")}, nil
|
||||||
})
|
})
|
||||||
fmt.Println(c.API().Protocols())
|
Println(c.API().Protocols())
|
||||||
// Output: [http]
|
// Output: [http]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleAPI_Stream() {
|
func ExampleAPI_Stream() {
|
||||||
c := New()
|
c := New()
|
||||||
c.API().RegisterProtocol("http", func(h *DriveHandle) (Stream, error) {
|
c.API().RegisterProtocol("http", func(h *DriveHandle) (Stream, error) {
|
||||||
return &mockStream{response: []byte("connected to " + h.Name)}, nil
|
return &mockStream{response: []byte(Concat("connected to ", h.Name))}, nil
|
||||||
})
|
})
|
||||||
c.Drive().New(NewOptions(
|
c.Drive().New(NewOptions(
|
||||||
Option{Key: "name", Value: "charon"},
|
Option{Key: "name", Value: "charon"},
|
||||||
|
|
@ -30,7 +29,7 @@ func ExampleAPI_Stream() {
|
||||||
if r.OK {
|
if r.OK {
|
||||||
stream := r.Value.(Stream)
|
stream := r.Value.(Stream)
|
||||||
resp, _ := stream.Receive()
|
resp, _ := stream.Receive()
|
||||||
fmt.Println(string(resp))
|
Println(string(resp))
|
||||||
stream.Close()
|
stream.Close()
|
||||||
}
|
}
|
||||||
// Output: connected to charon
|
// Output: connected to charon
|
||||||
|
|
@ -45,6 +44,6 @@ func ExampleCore_RemoteAction() {
|
||||||
|
|
||||||
// No colon — resolves locally
|
// No colon — resolves locally
|
||||||
r := c.RemoteAction("status", context.Background(), NewOptions())
|
r := c.RemoteAction("status", context.Background(), NewOptions())
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output: running
|
// Output: running
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -12,8 +11,8 @@ func ExampleNewArray() {
|
||||||
a.Add("bravo")
|
a.Add("bravo")
|
||||||
a.Add("charlie")
|
a.Add("charlie")
|
||||||
|
|
||||||
fmt.Println(a.Len())
|
Println(a.Len())
|
||||||
fmt.Println(a.Contains("bravo"))
|
Println(a.Contains("bravo"))
|
||||||
// Output:
|
// Output:
|
||||||
// 3
|
// 3
|
||||||
// true
|
// true
|
||||||
|
|
@ -25,7 +24,7 @@ func ExampleArray_AddUnique() {
|
||||||
a.AddUnique("alpha") // no duplicate
|
a.AddUnique("alpha") // no duplicate
|
||||||
a.AddUnique("bravo")
|
a.AddUnique("bravo")
|
||||||
|
|
||||||
fmt.Println(a.Len())
|
Println(a.Len())
|
||||||
// Output: 2
|
// Output: 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -37,6 +36,6 @@ func ExampleArray_Filter() {
|
||||||
a.Add(4)
|
a.Add(4)
|
||||||
|
|
||||||
r := a.Filter(func(n int) bool { return n%2 == 0 })
|
r := a.Filter(func(n int) bool { return n%2 == 0 })
|
||||||
fmt.Println(r.OK)
|
Println(r.OK)
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -15,7 +14,7 @@ func ExampleCore_Command_register() {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(c.Command("deploy/to/homelab").OK)
|
Println(c.Command("deploy/to/homelab").OK)
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -27,7 +26,7 @@ func ExampleCore_Command_managed() {
|
||||||
})
|
})
|
||||||
|
|
||||||
cmd := c.Command("serve").Value.(*Command)
|
cmd := c.Command("serve").Value.(*Command)
|
||||||
fmt.Println(cmd.IsManaged())
|
Println(cmd.IsManaged())
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,6 +35,6 @@ func ExampleCore_Commands() {
|
||||||
c.Command("deploy", Command{Action: func(_ Options) Result { return Result{OK: true} }})
|
c.Command("deploy", Command{Action: func(_ Options) Result { return Result{OK: true} }})
|
||||||
c.Command("test", Command{Action: func(_ Options) Result { return Result{OK: true} }})
|
c.Command("test", Command{Action: func(_ Options) Result { return Result{OK: true} }})
|
||||||
|
|
||||||
fmt.Println(c.Commands())
|
Println(c.Commands())
|
||||||
// Output: [deploy test]
|
// Output: [deploy test]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -11,8 +10,8 @@ func ExampleConfig_Set() {
|
||||||
c.Config().Set("database.host", "localhost")
|
c.Config().Set("database.host", "localhost")
|
||||||
c.Config().Set("database.port", 5432)
|
c.Config().Set("database.port", 5432)
|
||||||
|
|
||||||
fmt.Println(c.Config().String("database.host"))
|
Println(c.Config().String("database.host"))
|
||||||
fmt.Println(c.Config().Int("database.port"))
|
Println(c.Config().Int("database.port"))
|
||||||
// Output:
|
// Output:
|
||||||
// localhost
|
// localhost
|
||||||
// 5432
|
// 5432
|
||||||
|
|
@ -23,8 +22,8 @@ func ExampleConfig_Enable() {
|
||||||
c.Config().Enable("dark-mode")
|
c.Config().Enable("dark-mode")
|
||||||
c.Config().Enable("beta-features")
|
c.Config().Enable("beta-features")
|
||||||
|
|
||||||
fmt.Println(c.Config().Enabled("dark-mode"))
|
Println(c.Config().Enabled("dark-mode"))
|
||||||
fmt.Println(c.Config().EnabledFeatures())
|
Println(c.Config().EnabledFeatures())
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// [dark-mode beta-features]
|
// [dark-mode beta-features]
|
||||||
|
|
@ -32,10 +31,10 @@ func ExampleConfig_Enable() {
|
||||||
|
|
||||||
func ExampleConfigVar() {
|
func ExampleConfigVar() {
|
||||||
v := NewConfigVar(42)
|
v := NewConfigVar(42)
|
||||||
fmt.Println(v.Get(), v.IsSet())
|
Println(v.Get(), v.IsSet())
|
||||||
|
|
||||||
v.Unset()
|
v.Unset()
|
||||||
fmt.Println(v.Get(), v.IsSet())
|
Println(v.Get(), v.IsSet())
|
||||||
// Output:
|
// Output:
|
||||||
// 42 true
|
// 42 true
|
||||||
// 0 false
|
// 0 false
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -13,8 +12,8 @@ func ExampleDrive_New() {
|
||||||
Option{Key: "transport", Value: "https://forge.lthn.ai"},
|
Option{Key: "transport", Value: "https://forge.lthn.ai"},
|
||||||
))
|
))
|
||||||
|
|
||||||
fmt.Println(c.Drive().Has("forge"))
|
Println(c.Drive().Has("forge"))
|
||||||
fmt.Println(c.Drive().Names())
|
Println(c.Drive().Names())
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// [forge]
|
// [forge]
|
||||||
|
|
@ -30,7 +29,7 @@ func ExampleDrive_Get() {
|
||||||
r := c.Drive().Get("charon")
|
r := c.Drive().Get("charon")
|
||||||
if r.OK {
|
if r.OK {
|
||||||
h := r.Value.(*DriveHandle)
|
h := r.Value.(*DriveHandle)
|
||||||
fmt.Println(h.Transport)
|
Println(h.Transport)
|
||||||
}
|
}
|
||||||
// Output: http://10.69.69.165:9101
|
// Output: http://10.69.69.165:9101
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,7 @@ func TestEmbed_Extract_Good(t *testing.T) {
|
||||||
r := Extract(testFS, dir, nil)
|
r := Extract(testFS, dir, nil)
|
||||||
assert.True(t, r.OK)
|
assert.True(t, r.OK)
|
||||||
|
|
||||||
cr := (&Fs{}).New("/").Read(dir + "/testdata/test.txt")
|
cr := (&Fs{}).New("/").Read(Path(dir, "testdata/test.txt"))
|
||||||
assert.True(t, cr.OK)
|
assert.True(t, cr.OK)
|
||||||
assert.Equal(t, "hello from testdata\n", cr.Value)
|
assert.Equal(t, "hello from testdata\n", cr.Value)
|
||||||
}
|
}
|
||||||
|
|
@ -147,12 +147,12 @@ func TestEmbed_GeneratePack_Empty_Good(t *testing.T) {
|
||||||
|
|
||||||
func TestEmbed_GeneratePack_WithFiles_Good(t *testing.T) {
|
func TestEmbed_GeneratePack_WithFiles_Good(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
assetDir := dir + "/mygroup"
|
assetDir := Path(dir, "mygroup")
|
||||||
(&Fs{}).New("/").EnsureDir(assetDir)
|
(&Fs{}).New("/").EnsureDir(assetDir)
|
||||||
(&Fs{}).New("/").Write(assetDir+"/hello.txt", "hello world")
|
(&Fs{}).New("/").Write(Path(assetDir, "hello.txt"), "hello world")
|
||||||
|
|
||||||
source := "package test\nimport \"dappco.re/go/core\"\nfunc example() {\n\t_, _ = core.GetAsset(\"mygroup\", \"hello.txt\")\n}\n"
|
source := "package test\nimport \"dappco.re/go/core\"\nfunc example() {\n\t_, _ = core.GetAsset(\"mygroup\", \"hello.txt\")\n}\n"
|
||||||
goFile := dir + "/test.go"
|
goFile := Path(dir, "test.go")
|
||||||
(&Fs{}).New("/").Write(goFile, source)
|
(&Fs{}).New("/").Write(goFile, source)
|
||||||
|
|
||||||
sr := ScanAssets([]string{goFile})
|
sr := ScanAssets([]string{goFile})
|
||||||
|
|
@ -174,10 +174,10 @@ func TestEmbed_Extract_WithTemplate_Good(t *testing.T) {
|
||||||
|
|
||||||
// Use a real temp dir with files
|
// Use a real temp dir with files
|
||||||
srcDir := t.TempDir()
|
srcDir := t.TempDir()
|
||||||
(&Fs{}).New("/").Write(srcDir+"/plain.txt", "static content")
|
(&Fs{}).New("/").Write(Path(srcDir, "plain.txt"), "static content")
|
||||||
(&Fs{}).New("/").Write(srcDir+"/greeting.tmpl", "Hello {{.Name}}!")
|
(&Fs{}).New("/").Write(Path(srcDir, "greeting.tmpl"), "Hello {{.Name}}!")
|
||||||
(&Fs{}).New("/").EnsureDir(srcDir+"/sub")
|
(&Fs{}).New("/").EnsureDir(Path(srcDir, "sub"))
|
||||||
(&Fs{}).New("/").Write(srcDir+"/sub/nested.txt", "nested")
|
(&Fs{}).New("/").Write(Path(srcDir, "sub/nested.txt"), "nested")
|
||||||
|
|
||||||
_ = tmplDir
|
_ = tmplDir
|
||||||
fsys := DirFS(srcDir)
|
fsys := DirFS(srcDir)
|
||||||
|
|
@ -189,24 +189,24 @@ func TestEmbed_Extract_WithTemplate_Good(t *testing.T) {
|
||||||
f := (&Fs{}).New("/")
|
f := (&Fs{}).New("/")
|
||||||
|
|
||||||
// Plain file copied
|
// Plain file copied
|
||||||
cr := f.Read(dir + "/plain.txt")
|
cr := f.Read(Path(dir, "plain.txt"))
|
||||||
assert.True(t, cr.OK)
|
assert.True(t, cr.OK)
|
||||||
assert.Equal(t, "static content", cr.Value)
|
assert.Equal(t, "static content", cr.Value)
|
||||||
|
|
||||||
// Template processed and .tmpl stripped
|
// Template processed and .tmpl stripped
|
||||||
gr := f.Read(dir + "/greeting")
|
gr := f.Read(Path(dir, "greeting"))
|
||||||
assert.True(t, gr.OK)
|
assert.True(t, gr.OK)
|
||||||
assert.Equal(t, "Hello World!", gr.Value)
|
assert.Equal(t, "Hello World!", gr.Value)
|
||||||
|
|
||||||
// Nested directory preserved
|
// Nested directory preserved
|
||||||
nr := f.Read(dir + "/sub/nested.txt")
|
nr := f.Read(Path(dir, "sub/nested.txt"))
|
||||||
assert.True(t, nr.OK)
|
assert.True(t, nr.OK)
|
||||||
assert.Equal(t, "nested", nr.Value)
|
assert.Equal(t, "nested", nr.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmbed_Extract_BadTargetDir_Ugly(t *testing.T) {
|
func TestEmbed_Extract_BadTargetDir_Ugly(t *testing.T) {
|
||||||
srcDir := t.TempDir()
|
srcDir := t.TempDir()
|
||||||
(&Fs{}).New("/").Write(srcDir+"/f.txt", "x")
|
(&Fs{}).New("/").Write(Path(srcDir, "f.txt"), "x")
|
||||||
r := Extract(DirFS(srcDir), "/nonexistent/deeply/nested/impossible", nil)
|
r := Extract(DirFS(srcDir), "/nonexistent/deeply/nested/impossible", nil)
|
||||||
// Should fail gracefully, not panic
|
// Should fail gracefully, not panic
|
||||||
_ = r
|
_ = r
|
||||||
|
|
@ -248,7 +248,7 @@ func TestEmbed_EmbedFS_Original_Good(t *testing.T) {
|
||||||
func TestEmbed_Extract_NilData_Good(t *testing.T) {
|
func TestEmbed_Extract_NilData_Good(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
srcDir := t.TempDir()
|
srcDir := t.TempDir()
|
||||||
(&Fs{}).New("/").Write(srcDir+"/file.txt", "no template")
|
(&Fs{}).New("/").Write(Path(srcDir, "file.txt"), "no template")
|
||||||
|
|
||||||
r := Extract(DirFS(srcDir), dir, nil)
|
r := Extract(DirFS(srcDir), dir, nil)
|
||||||
assert.True(t, r.OK)
|
assert.True(t, r.OK)
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,13 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleEntitlement_UsagePercent() {
|
func ExampleEntitlement_UsagePercent() {
|
||||||
e := Entitlement{Limit: 100, Used: 75}
|
e := Entitlement{Limit: 100, Used: 75}
|
||||||
fmt.Println(e.UsagePercent())
|
Println(e.UsagePercent())
|
||||||
// Output: 75
|
// Output: 75
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,9 +30,9 @@ func ExampleCore_SetEntitlementChecker() {
|
||||||
return Entitlement{Allowed: true, Limit: limit, Used: used, Remaining: remaining}
|
return Entitlement{Allowed: true, Limit: limit, Used: used, Remaining: remaining}
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(c.Entitled("social.accounts", 2).Allowed)
|
Println(c.Entitled("social.accounts", 2).Allowed)
|
||||||
fmt.Println(c.Entitled("social.accounts", 5).Allowed)
|
Println(c.Entitled("social.accounts", 5).Allowed)
|
||||||
fmt.Println(c.Entitled("ai.credits").NearLimit(0.9))
|
Println(c.Entitled("ai.credits").NearLimit(0.9))
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|
@ -44,10 +43,10 @@ func ExampleCore_RecordUsage() {
|
||||||
c := New()
|
c := New()
|
||||||
var recorded string
|
var recorded string
|
||||||
c.SetUsageRecorder(func(action string, qty int, _ context.Context) {
|
c.SetUsageRecorder(func(action string, qty int, _ context.Context) {
|
||||||
recorded = fmt.Sprintf("%s:%d", action, qty)
|
recorded = Concat(action, ":", Sprint(qty))
|
||||||
})
|
})
|
||||||
|
|
||||||
c.RecordUsage("ai.credits", 10)
|
c.RecordUsage("ai.credits", 10)
|
||||||
fmt.Println(recorded)
|
Println(recorded)
|
||||||
// Output: ai.credits:10
|
// Output: ai.credits:10
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,14 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleE() {
|
func ExampleE() {
|
||||||
err := E("cache.Get", "key not found", nil)
|
err := E("cache.Get", "key not found", nil)
|
||||||
fmt.Println(Operation(err))
|
Println(Operation(err))
|
||||||
fmt.Println(ErrorMessage(err))
|
Println(ErrorMessage(err))
|
||||||
// Output:
|
// Output:
|
||||||
// cache.Get
|
// cache.Get
|
||||||
// key not found
|
// key not found
|
||||||
|
|
@ -18,8 +17,8 @@ func ExampleE() {
|
||||||
func ExampleWrap() {
|
func ExampleWrap() {
|
||||||
cause := NewError("connection refused")
|
cause := NewError("connection refused")
|
||||||
err := Wrap(cause, "database.Connect", "failed to reach host")
|
err := Wrap(cause, "database.Connect", "failed to reach host")
|
||||||
fmt.Println(Operation(err))
|
Println(Operation(err))
|
||||||
fmt.Println(Is(err, cause))
|
Println(Is(err, cause))
|
||||||
// Output:
|
// Output:
|
||||||
// database.Connect
|
// database.Connect
|
||||||
// true
|
// true
|
||||||
|
|
@ -29,6 +28,6 @@ func ExampleRoot() {
|
||||||
cause := NewError("original")
|
cause := NewError("original")
|
||||||
wrapped := Wrap(cause, "op1", "first wrap")
|
wrapped := Wrap(cause, "op1", "first wrap")
|
||||||
double := Wrap(wrapped, "op2", "second wrap")
|
double := Wrap(wrapped, "op2", "second wrap")
|
||||||
fmt.Println(Root(double))
|
Println(Root(double))
|
||||||
// Output: original
|
// Output: original
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,7 @@ func TestError_ErrorJoin_Good(t *testing.T) {
|
||||||
|
|
||||||
func TestError_ErrorPanic_Reports_Good(t *testing.T) {
|
func TestError_ErrorPanic_Reports_Good(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
path := dir + "/crashes.json"
|
path := Path(dir, "crashes.json")
|
||||||
|
|
||||||
// Create ErrorPanic with file output
|
// Create ErrorPanic with file output
|
||||||
c := New()
|
c := New()
|
||||||
|
|
@ -213,7 +213,7 @@ func TestError_ErrorPanic_Reports_Good(t *testing.T) {
|
||||||
|
|
||||||
func TestError_ErrorPanic_CrashFile_Good(t *testing.T) {
|
func TestError_ErrorPanic_CrashFile_Good(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
path := dir + "/crashes.json"
|
path := Path(dir, "crashes.json")
|
||||||
|
|
||||||
// Create Core, trigger a panic through SafeGo, check crash file
|
// Create Core, trigger a panic through SafeGo, check crash file
|
||||||
// ErrorPanic.filePath is unexported — but we can test via the package-level
|
// ErrorPanic.filePath is unexported — but we can test via the package-level
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -14,7 +13,7 @@ func ExampleNew() {
|
||||||
WithOption("name", "my-app"),
|
WithOption("name", "my-app"),
|
||||||
WithServiceLock(),
|
WithServiceLock(),
|
||||||
)
|
)
|
||||||
fmt.Println(c.App().Name)
|
Println(c.App().Name)
|
||||||
// Output: my-app
|
// Output: my-app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,7 +30,7 @@ func ExampleNew_withService() {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
c.ServiceStartup(context.Background(), nil)
|
c.ServiceStartup(context.Background(), nil)
|
||||||
fmt.Println(c.Services())
|
Println(c.Services())
|
||||||
c.ServiceShutdown(context.Background())
|
c.ServiceShutdown(context.Background())
|
||||||
// Output is non-deterministic (map order), so no Output comment
|
// Output is non-deterministic (map order), so no Output comment
|
||||||
}
|
}
|
||||||
|
|
@ -44,9 +43,9 @@ func ExampleNewOptions() {
|
||||||
Option{Key: "port", Value: 8080},
|
Option{Key: "port", Value: 8080},
|
||||||
Option{Key: "debug", Value: true},
|
Option{Key: "debug", Value: true},
|
||||||
)
|
)
|
||||||
fmt.Println(opts.String("name"))
|
Println(opts.String("name"))
|
||||||
fmt.Println(opts.Int("port"))
|
Println(opts.Int("port"))
|
||||||
fmt.Println(opts.Bool("debug"))
|
Println(opts.Bool("debug"))
|
||||||
// Output:
|
// Output:
|
||||||
// brain
|
// brain
|
||||||
// 8080
|
// 8080
|
||||||
|
|
@ -58,7 +57,7 @@ func ExampleNewOptions() {
|
||||||
func ExampleResult() {
|
func ExampleResult() {
|
||||||
r := Result{Value: "hello", OK: true}
|
r := Result{Value: "hello", OK: true}
|
||||||
if r.OK {
|
if r.OK {
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
}
|
}
|
||||||
// Output: hello
|
// Output: hello
|
||||||
}
|
}
|
||||||
|
|
@ -69,9 +68,9 @@ func ExampleCore_Action_register() {
|
||||||
c := New()
|
c := New()
|
||||||
c.Action("greet", func(_ context.Context, opts Options) Result {
|
c.Action("greet", func(_ context.Context, opts Options) Result {
|
||||||
name := opts.String("name")
|
name := opts.String("name")
|
||||||
return Result{Value: "hello " + name, OK: true}
|
return Result{Value: Concat("hello ", name), OK: true}
|
||||||
})
|
})
|
||||||
fmt.Println(c.Action("greet").Exists())
|
Println(c.Action("greet").Exists())
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,7 +86,7 @@ func ExampleCore_Action_invoke() {
|
||||||
Option{Key: "a", Value: 3},
|
Option{Key: "a", Value: 3},
|
||||||
Option{Key: "b", Value: 4},
|
Option{Key: "b", Value: 4},
|
||||||
))
|
))
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output: 7
|
// Output: 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,7 +95,7 @@ func ExampleCore_Actions() {
|
||||||
c.Action("process.run", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
c.Action("process.run", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
||||||
c.Action("brain.recall", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
c.Action("brain.recall", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
||||||
|
|
||||||
fmt.Println(c.Actions())
|
Println(c.Actions())
|
||||||
// Output: [process.run brain.recall]
|
// Output: [process.run brain.recall]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,7 +122,7 @@ func ExampleCore_Task() {
|
||||||
})
|
})
|
||||||
|
|
||||||
c.Task("pipeline").Run(context.Background(), c, NewOptions())
|
c.Task("pipeline").Run(context.Background(), c, NewOptions())
|
||||||
fmt.Println(order)
|
Println(order)
|
||||||
// Output: ab
|
// Output: ab
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,9 +133,9 @@ func ExampleNewRegistry() {
|
||||||
r.Set("alpha", "first")
|
r.Set("alpha", "first")
|
||||||
r.Set("bravo", "second")
|
r.Set("bravo", "second")
|
||||||
|
|
||||||
fmt.Println(r.Has("alpha"))
|
Println(r.Has("alpha"))
|
||||||
fmt.Println(r.Names())
|
Println(r.Names())
|
||||||
fmt.Println(r.Len())
|
Println(r.Len())
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// [alpha bravo]
|
// [alpha bravo]
|
||||||
|
|
@ -149,7 +148,7 @@ func ExampleRegistry_Lock() {
|
||||||
r.Lock()
|
r.Lock()
|
||||||
|
|
||||||
result := r.Set("beta", "second")
|
result := r.Set("beta", "second")
|
||||||
fmt.Println(result.OK)
|
Println(result.OK)
|
||||||
// Output: false
|
// Output: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -159,9 +158,9 @@ func ExampleRegistry_Seal() {
|
||||||
r.Seal()
|
r.Seal()
|
||||||
|
|
||||||
// Can update existing
|
// Can update existing
|
||||||
fmt.Println(r.Set("alpha", "updated").OK)
|
Println(r.Set("alpha", "updated").OK)
|
||||||
// Can't add new
|
// Can't add new
|
||||||
fmt.Println(r.Set("beta", "new").OK)
|
Println(r.Set("beta", "new").OK)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|
@ -172,8 +171,8 @@ func ExampleRegistry_Seal() {
|
||||||
func ExampleCore_Entitled_default() {
|
func ExampleCore_Entitled_default() {
|
||||||
c := New()
|
c := New()
|
||||||
e := c.Entitled("anything")
|
e := c.Entitled("anything")
|
||||||
fmt.Println(e.Allowed)
|
Println(e.Allowed)
|
||||||
fmt.Println(e.Unlimited)
|
Println(e.Unlimited)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// true
|
// true
|
||||||
|
|
@ -188,9 +187,9 @@ func ExampleCore_Entitled_custom() {
|
||||||
return Entitlement{Allowed: true, Unlimited: true}
|
return Entitlement{Allowed: true, Unlimited: true}
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(c.Entitled("basic").Allowed)
|
Println(c.Entitled("basic").Allowed)
|
||||||
fmt.Println(c.Entitled("premium").Allowed)
|
Println(c.Entitled("premium").Allowed)
|
||||||
fmt.Println(c.Entitled("premium").Reason)
|
Println(c.Entitled("premium").Reason)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|
@ -199,8 +198,8 @@ func ExampleCore_Entitled_custom() {
|
||||||
|
|
||||||
func ExampleEntitlement_NearLimit() {
|
func ExampleEntitlement_NearLimit() {
|
||||||
e := Entitlement{Allowed: true, Limit: 100, Used: 85, Remaining: 15}
|
e := Entitlement{Allowed: true, Limit: 100, Used: 85, Remaining: 15}
|
||||||
fmt.Println(e.NearLimit(0.8))
|
Println(e.NearLimit(0.8))
|
||||||
fmt.Println(e.UsagePercent())
|
Println(e.UsagePercent())
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// 85
|
// 85
|
||||||
|
|
@ -211,16 +210,16 @@ func ExampleEntitlement_NearLimit() {
|
||||||
func ExampleCore_Process() {
|
func ExampleCore_Process() {
|
||||||
c := New()
|
c := New()
|
||||||
// No go-process registered — permission by registration
|
// No go-process registered — permission by registration
|
||||||
fmt.Println(c.Process().Exists())
|
Println(c.Process().Exists())
|
||||||
|
|
||||||
// Register a mock process handler
|
// Register a mock process handler
|
||||||
c.Action("process.run", func(_ context.Context, opts Options) Result {
|
c.Action("process.run", func(_ context.Context, opts Options) Result {
|
||||||
return Result{Value: "output of " + opts.String("command"), OK: true}
|
return Result{Value: Concat("output of ", opts.String("command")), OK: true}
|
||||||
})
|
})
|
||||||
fmt.Println(c.Process().Exists())
|
Println(c.Process().Exists())
|
||||||
|
|
||||||
r := c.Process().Run(context.Background(), "echo", "hello")
|
r := c.Process().Run(context.Background(), "echo", "hello")
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output:
|
// Output:
|
||||||
// false
|
// false
|
||||||
// true
|
// true
|
||||||
|
|
@ -235,7 +234,7 @@ func ExampleJSONMarshal() {
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
}
|
}
|
||||||
r := JSONMarshal(config{Host: "localhost", Port: 8080})
|
r := JSONMarshal(config{Host: "localhost", Port: 8080})
|
||||||
fmt.Println(string(r.Value.([]byte)))
|
Println(string(r.Value.([]byte)))
|
||||||
// Output: {"host":"localhost","port":8080}
|
// Output: {"host":"localhost","port":8080}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -246,7 +245,7 @@ func ExampleJSONUnmarshalString() {
|
||||||
}
|
}
|
||||||
var cfg config
|
var cfg config
|
||||||
JSONUnmarshalString(`{"host":"localhost","port":8080}`, &cfg)
|
JSONUnmarshalString(`{"host":"localhost","port":8080}`, &cfg)
|
||||||
fmt.Println(cfg.Host, cfg.Port)
|
Println(cfg.Host, cfg.Port)
|
||||||
// Output: localhost 8080
|
// Output: localhost 8080
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -254,15 +253,15 @@ func ExampleJSONUnmarshalString() {
|
||||||
|
|
||||||
func ExampleID() {
|
func ExampleID() {
|
||||||
id := ID()
|
id := ID()
|
||||||
fmt.Println(HasPrefix(id, "id-"))
|
Println(HasPrefix(id, "id-"))
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleValidateName() {
|
func ExampleValidateName() {
|
||||||
fmt.Println(ValidateName("brain").OK)
|
Println(ValidateName("brain").OK)
|
||||||
fmt.Println(ValidateName("").OK)
|
Println(ValidateName("").OK)
|
||||||
fmt.Println(ValidateName("..").OK)
|
Println(ValidateName("..").OK)
|
||||||
fmt.Println(ValidateName("path/traversal").OK)
|
Println(ValidateName("path/traversal").OK)
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|
@ -271,9 +270,9 @@ func ExampleValidateName() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleSanitisePath() {
|
func ExampleSanitisePath() {
|
||||||
fmt.Println(SanitisePath("../../etc/passwd"))
|
Println(SanitisePath("../../etc/passwd"))
|
||||||
fmt.Println(SanitisePath(""))
|
Println(SanitisePath(""))
|
||||||
fmt.Println(SanitisePath("/some/path/file.txt"))
|
Println(SanitisePath("/some/path/file.txt"))
|
||||||
// Output:
|
// Output:
|
||||||
// passwd
|
// passwd
|
||||||
// invalid
|
// invalid
|
||||||
|
|
@ -286,12 +285,12 @@ func ExampleCore_Command() {
|
||||||
c := New()
|
c := New()
|
||||||
c.Command("deploy/to/homelab", Command{
|
c.Command("deploy/to/homelab", Command{
|
||||||
Action: func(opts Options) Result {
|
Action: func(opts Options) Result {
|
||||||
return Result{Value: "deployed to " + opts.String("_arg"), OK: true}
|
return Result{Value: Concat("deployed to ", opts.String("_arg")), OK: true}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Cli().Run("deploy", "to", "homelab")
|
r := c.Cli().Run("deploy", "to", "homelab")
|
||||||
fmt.Println(r.OK)
|
Println(r.OK)
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -303,9 +302,9 @@ func ExampleConfig() {
|
||||||
c.Config().Set("database.port", 5432)
|
c.Config().Set("database.port", 5432)
|
||||||
c.Config().Enable("dark-mode")
|
c.Config().Enable("dark-mode")
|
||||||
|
|
||||||
fmt.Println(c.Config().String("database.host"))
|
Println(c.Config().String("database.host"))
|
||||||
fmt.Println(c.Config().Int("database.port"))
|
Println(c.Config().Int("database.port"))
|
||||||
fmt.Println(c.Config().Enabled("dark-mode"))
|
Println(c.Config().Enabled("dark-mode"))
|
||||||
// Output:
|
// Output:
|
||||||
// localhost
|
// localhost
|
||||||
// 5432
|
// 5432
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -15,7 +14,7 @@ func ExampleFs_WriteAtomic() {
|
||||||
f.WriteAtomic(path, `{"status":"completed"}`)
|
f.WriteAtomic(path, `{"status":"completed"}`)
|
||||||
|
|
||||||
r := f.Read(path)
|
r := f.Read(path)
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output: {"status":"completed"}
|
// Output: {"status":"completed"}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,12 +31,12 @@ func ExampleFs_NewUnrestricted() {
|
||||||
unrestricted := sandbox.NewUnrestricted()
|
unrestricted := sandbox.NewUnrestricted()
|
||||||
|
|
||||||
r := unrestricted.Read(outside)
|
r := unrestricted.Read(outside)
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output: hello
|
// Output: hello
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleFs_Root() {
|
func ExampleFs_Root() {
|
||||||
f := (&Fs{}).New("/srv/workspaces")
|
f := (&Fs{}).New("/srv/workspaces")
|
||||||
fmt.Println(f.Root())
|
Println(f.Root())
|
||||||
// Output: /srv/workspaces
|
// Output: /srv/workspaces
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ type mockTranslator struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockTranslator) Translate(id string, args ...any) Result {
|
func (m *mockTranslator) Translate(id string, args ...any) Result {
|
||||||
return Result{"translated:" + id, true}
|
return Result{Concat("translated:", id), true}
|
||||||
}
|
}
|
||||||
func (m *mockTranslator) SetLanguage(lang string) error { m.lang = lang; return nil }
|
func (m *mockTranslator) SetLanguage(lang string) error { m.lang = lang; return nil }
|
||||||
func (m *mockTranslator) Language() string { return m.lang }
|
func (m *mockTranslator) Language() string { return m.lang }
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,17 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleEnv() {
|
func ExampleEnv() {
|
||||||
fmt.Println(Env("OS")) // e.g. "darwin"
|
Println(Env("OS")) // e.g. "darwin"
|
||||||
fmt.Println(Env("ARCH")) // e.g. "arm64"
|
Println(Env("ARCH")) // e.g. "arm64"
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEnvKeys() {
|
func ExampleEnvKeys() {
|
||||||
keys := EnvKeys()
|
keys := EnvKeys()
|
||||||
fmt.Println(len(keys) > 0)
|
Println(len(keys) > 0)
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -10,9 +9,9 @@ func ExampleCore_Lock() {
|
||||||
c := New()
|
c := New()
|
||||||
lock := c.Lock("drain")
|
lock := c.Lock("drain")
|
||||||
lock.Mutex.Lock()
|
lock.Mutex.Lock()
|
||||||
fmt.Println("locked")
|
Println("locked")
|
||||||
lock.Mutex.Unlock()
|
lock.Mutex.Unlock()
|
||||||
fmt.Println("unlocked")
|
Println("unlocked")
|
||||||
// Output:
|
// Output:
|
||||||
// locked
|
// locked
|
||||||
// unlocked
|
// unlocked
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,34 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleJoinPath() {
|
func ExampleJoinPath() {
|
||||||
fmt.Println(JoinPath("deploy", "to", "homelab"))
|
Println(JoinPath("deploy", "to", "homelab"))
|
||||||
// Output: deploy/to/homelab
|
// Output: deploy/to/homelab
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExamplePathBase() {
|
func ExamplePathBase() {
|
||||||
fmt.Println(PathBase("/srv/workspaces/alpha"))
|
Println(PathBase("/srv/workspaces/alpha"))
|
||||||
// Output: alpha
|
// Output: alpha
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExamplePathDir() {
|
func ExamplePathDir() {
|
||||||
fmt.Println(PathDir("/srv/workspaces/alpha"))
|
Println(PathDir("/srv/workspaces/alpha"))
|
||||||
// Output: /srv/workspaces
|
// Output: /srv/workspaces
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExamplePathExt() {
|
func ExamplePathExt() {
|
||||||
fmt.Println(PathExt("report.pdf"))
|
Println(PathExt("report.pdf"))
|
||||||
// Output: .pdf
|
// Output: .pdf
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleCleanPath() {
|
func ExampleCleanPath() {
|
||||||
fmt.Println(CleanPath("/tmp//file", "/"))
|
Println(CleanPath("/tmp//file", "/"))
|
||||||
fmt.Println(CleanPath("a/b/../c", "/"))
|
Println(CleanPath("a/b/../c", "/"))
|
||||||
fmt.Println(CleanPath("deploy/to/homelab", "/"))
|
Println(CleanPath("deploy/to/homelab", "/"))
|
||||||
// Output:
|
// Output:
|
||||||
// /tmp/file
|
// /tmp/file
|
||||||
// a/c
|
// a/c
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ func TestProcess_Run_Good(t *testing.T) {
|
||||||
// Register a mock process handler
|
// Register a mock process handler
|
||||||
c.Action("process.run", func(_ context.Context, opts Options) Result {
|
c.Action("process.run", func(_ context.Context, opts Options) Result {
|
||||||
cmd := opts.String("command")
|
cmd := opts.String("command")
|
||||||
return Result{Value: "output of " + cmd, OK: true}
|
return Result{Value: Concat("output of ", cmd), OK: true}
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Process().Run(context.Background(), "git", "log")
|
r := c.Process().Run(context.Background(), "git", "log")
|
||||||
|
|
@ -46,7 +46,7 @@ func TestProcess_RunIn_Good(t *testing.T) {
|
||||||
c.Action("process.run", func(_ context.Context, opts Options) Result {
|
c.Action("process.run", func(_ context.Context, opts Options) Result {
|
||||||
dir := opts.String("dir")
|
dir := opts.String("dir")
|
||||||
cmd := opts.String("command")
|
cmd := opts.String("command")
|
||||||
return Result{Value: cmd + " in " + dir, OK: true}
|
return Result{Value: Concat(cmd, " in ", dir), OK: true}
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Process().RunIn(context.Background(), "/repo", "go", "test")
|
r := c.Process().RunIn(context.Background(), "/repo", "go", "test")
|
||||||
|
|
@ -126,7 +126,7 @@ func TestProcess_Ugly_PermissionByRegistration(t *testing.T) {
|
||||||
// Full Core
|
// Full Core
|
||||||
full := New()
|
full := New()
|
||||||
full.Action("process.run", func(_ context.Context, opts Options) Result {
|
full.Action("process.run", func(_ context.Context, opts Options) Result {
|
||||||
return Result{Value: "executed " + opts.String("command"), OK: true}
|
return Result{Value: Concat("executed ", opts.String("command")), OK: true}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Sandboxed Core
|
// Sandboxed Core
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -10,7 +9,7 @@ func ExampleRegistry_Set() {
|
||||||
r := NewRegistry[string]()
|
r := NewRegistry[string]()
|
||||||
r.Set("alpha", "first")
|
r.Set("alpha", "first")
|
||||||
r.Set("bravo", "second")
|
r.Set("bravo", "second")
|
||||||
fmt.Println(r.Get("alpha").Value)
|
Println(r.Get("alpha").Value)
|
||||||
// Output: first
|
// Output: first
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -19,7 +18,7 @@ func ExampleRegistry_Names() {
|
||||||
r.Set("charlie", 3)
|
r.Set("charlie", 3)
|
||||||
r.Set("alpha", 1)
|
r.Set("alpha", 1)
|
||||||
r.Set("bravo", 2)
|
r.Set("bravo", 2)
|
||||||
fmt.Println(r.Names())
|
Println(r.Names())
|
||||||
// Output: [charlie alpha bravo]
|
// Output: [charlie alpha bravo]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,7 +29,7 @@ func ExampleRegistry_List() {
|
||||||
r.Set("brain.recall", "recall")
|
r.Set("brain.recall", "recall")
|
||||||
|
|
||||||
items := r.List("process.*")
|
items := r.List("process.*")
|
||||||
fmt.Println(len(items))
|
Println(len(items))
|
||||||
// Output: 2
|
// Output: 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -42,7 +41,7 @@ func ExampleRegistry_Each() {
|
||||||
|
|
||||||
sum := 0
|
sum := 0
|
||||||
r.Each(func(_ string, v int) { sum += v })
|
r.Each(func(_ string, v int) { sum += v })
|
||||||
fmt.Println(sum)
|
Println(sum)
|
||||||
// Output: 6
|
// Output: 6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,17 +53,17 @@ func ExampleRegistry_Disable() {
|
||||||
|
|
||||||
var names []string
|
var names []string
|
||||||
r.Each(func(name string, _ string) { names = append(names, name) })
|
r.Each(func(name string, _ string) { names = append(names, name) })
|
||||||
fmt.Println(names)
|
Println(names)
|
||||||
// Output: [bravo]
|
// Output: [bravo]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRegistry_Delete() {
|
func ExampleRegistry_Delete() {
|
||||||
r := NewRegistry[string]()
|
r := NewRegistry[string]()
|
||||||
r.Set("temp", "value")
|
r.Set("temp", "value")
|
||||||
fmt.Println(r.Has("temp"))
|
Println(r.Has("temp"))
|
||||||
|
|
||||||
r.Delete("temp")
|
r.Delete("temp")
|
||||||
fmt.Println(r.Has("temp"))
|
Println(r.Has("temp"))
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -17,7 +16,7 @@ func ExampleServiceFor() {
|
||||||
)
|
)
|
||||||
|
|
||||||
svc := c.Service("cache")
|
svc := c.Service("cache")
|
||||||
fmt.Println(svc.OK)
|
Println(svc.OK)
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -31,7 +30,7 @@ func ExampleWithService() {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
c.ServiceStartup(context.Background(), nil)
|
c.ServiceStartup(context.Background(), nil)
|
||||||
fmt.Println(started)
|
Println(started)
|
||||||
c.ServiceShutdown(context.Background())
|
c.ServiceShutdown(context.Background())
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
@ -46,6 +45,6 @@ func ExampleWithServiceLock() {
|
||||||
|
|
||||||
// Can't register after lock
|
// Can't register after lock
|
||||||
r := c.Service("blocked", Service{})
|
r := c.Service("blocked", Service{})
|
||||||
fmt.Println(r.OK)
|
Println(r.OK)
|
||||||
// Output: false
|
// Output: false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
package core_test
|
package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleContains() {
|
func ExampleContains() {
|
||||||
fmt.Println(Contains("hello world", "world"))
|
Println(Contains("hello world", "world"))
|
||||||
fmt.Println(Contains("hello world", "mars"))
|
Println(Contains("hello world", "mars"))
|
||||||
// Output:
|
// Output:
|
||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
|
|
@ -16,21 +15,21 @@ func ExampleContains() {
|
||||||
|
|
||||||
func ExampleSplit() {
|
func ExampleSplit() {
|
||||||
parts := Split("deploy/to/homelab", "/")
|
parts := Split("deploy/to/homelab", "/")
|
||||||
fmt.Println(parts)
|
Println(parts)
|
||||||
// Output: [deploy to homelab]
|
// Output: [deploy to homelab]
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleJoin() {
|
func ExampleJoin() {
|
||||||
fmt.Println(Join("/", "deploy", "to", "homelab"))
|
Println(Join("/", "deploy", "to", "homelab"))
|
||||||
// Output: deploy/to/homelab
|
// Output: deploy/to/homelab
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleConcat() {
|
func ExampleConcat() {
|
||||||
fmt.Println(Concat("hello", " ", "world"))
|
Println(Concat("hello", " ", "world"))
|
||||||
// Output: hello world
|
// Output: hello world
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleTrim() {
|
func ExampleTrim() {
|
||||||
fmt.Println(Trim(" spaced "))
|
Println(Trim(" spaced "))
|
||||||
// Output: spaced
|
// Output: spaced
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "dappco.re/go/core"
|
. "dappco.re/go/core"
|
||||||
)
|
)
|
||||||
|
|
@ -32,8 +31,8 @@ func ExampleTask_Run() {
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.Task("pipe").Run(context.Background(), c, NewOptions())
|
r := c.Task("pipe").Run(context.Background(), c, NewOptions())
|
||||||
fmt.Println(order)
|
Println(order)
|
||||||
fmt.Println(r.Value)
|
Println(r.Value)
|
||||||
// Output:
|
// Output:
|
||||||
// ab
|
// ab
|
||||||
// got:from-a
|
// got:from-a
|
||||||
|
|
@ -46,6 +45,6 @@ func ExampleCore_PerformAsync() {
|
||||||
})
|
})
|
||||||
|
|
||||||
r := c.PerformAsync("bg.work", NewOptions())
|
r := c.PerformAsync("bg.work", NewOptions())
|
||||||
fmt.Println(HasPrefix(r.Value.(string), "id-"))
|
Println(HasPrefix(r.Value.(string), "id-"))
|
||||||
// Output: true
|
// Output: true
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
utils.go
7
utils.go
|
|
@ -68,6 +68,13 @@ func SanitisePath(path string) string {
|
||||||
|
|
||||||
// --- I/O ---
|
// --- I/O ---
|
||||||
|
|
||||||
|
// Println prints values to stdout with a newline. Replaces fmt.Println.
|
||||||
|
//
|
||||||
|
// core.Println("hello", 42, true)
|
||||||
|
func Println(args ...any) {
|
||||||
|
fmt.Println(args...)
|
||||||
|
}
|
||||||
|
|
||||||
// Print writes a formatted line to a writer, defaulting to os.Stdout.
|
// Print writes a formatted line to a writer, defaulting to os.Stdout.
|
||||||
//
|
//
|
||||||
// core.Print(nil, "hello %s", "world") // → stdout
|
// core.Print(nil, "hello %s", "world") // → stdout
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue