feat: init go project

This commit is contained in:
2026-06-06 22:19:21 +00:00
parent 3b6e443483
commit 7809552d05
8 changed files with 235 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
package app
import (
"context"
"errors"
"fmt"
"io"
"os"
)
type Options struct {
ConfigPath string
Stdout io.Writer
}
func Run(ctx context.Context, opts Options) error {
if err := ctx.Err(); err != nil {
return err
}
if opts.ConfigPath == "" {
return errors.New("config path is required")
}
if opts.Stdout == nil {
opts.Stdout = io.Discard
}
if _, err := os.Stat(opts.ConfigPath); err != nil {
if errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("config file %q does not exist; copy config.example.yaml to config.yaml and fill credentials", opts.ConfigPath)
}
return fmt.Errorf("check config file %q: %w", opts.ConfigPath, err)
}
fmt.Fprintf(opts.Stdout, "overnight trading bot initialized with config %q\n", opts.ConfigPath)
return nil
}
+51
View File
@@ -0,0 +1,51 @@
package app
import (
"bytes"
"context"
"os"
"strings"
"testing"
)
func TestRunRequiresConfigPath(t *testing.T) {
err := Run(context.Background(), Options{})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "config path is required") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunReportsMissingConfig(t *testing.T) {
err := Run(context.Background(), Options{ConfigPath: "missing.yaml"})
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "does not exist") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestRunUsesExistingConfig(t *testing.T) {
touch := t.TempDir() + "/config.yaml"
if err := os.WriteFile(touch, []byte("instruments: []\n"), 0o600); err != nil {
t.Fatal(err)
}
var stdout bytes.Buffer
err := Run(context.Background(), Options{
ConfigPath: touch,
Stdout: &stdout,
})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(stdout.String(), "initialized") {
t.Fatalf("unexpected stdout: %q", stdout.String())
}
}