feat: bootstrap control plane app skeleton
This commit is contained in:
28
cmd/cli/main.go
Normal file
28
cmd/cli/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
"sub2api-cn-relay-manager/internal/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := execute(context.Background(), log.Writer(), func(context.Context) (config.StartupConfig, error) {
|
||||
return config.LoadStartupFromEnv()
|
||||
}); err != nil {
|
||||
log.Fatalf("run cli: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func execute(ctx context.Context, output io.Writer, loadConfig func(context.Context) (config.StartupConfig, error)) error {
|
||||
cfg, err := loadConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = fmt.Fprintf(output, "sub2api-cn-relay-manager cli ready\nlisten_addr=%s\nsqlite_dsn=%s\n", cfg.Server.ListenAddr, cfg.Database.SQLiteDSN)
|
||||
return err
|
||||
}
|
||||
79
cmd/cli/main_test.go
Normal file
79
cmd/cli/main_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sub2api-cn-relay-manager/internal/config"
|
||||
)
|
||||
|
||||
type errWriter struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (w errWriter) Write([]byte) (int, error) {
|
||||
return 0, w.err
|
||||
}
|
||||
|
||||
func TestExecuteWritesConfigSummaryAfterBootstrap(t *testing.T) {
|
||||
var output bytes.Buffer
|
||||
loadCalled := false
|
||||
|
||||
err := execute(context.Background(), &output, func(context.Context) (config.StartupConfig, error) {
|
||||
loadCalled = true
|
||||
return config.StartupConfig{
|
||||
Server: config.ServerConfig{ListenAddr: ":9191"},
|
||||
Database: config.DatabaseConfig{
|
||||
SQLiteDSN: "file:test.db?_foreign_keys=on",
|
||||
},
|
||||
}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("execute() returned error: %v", err)
|
||||
}
|
||||
|
||||
if !loadCalled {
|
||||
t.Fatal("execute() did not load config")
|
||||
}
|
||||
|
||||
got := output.String()
|
||||
if !strings.Contains(got, "sub2api-cn-relay-manager cli ready") {
|
||||
t.Fatalf("execute() output = %q, want readiness line", got)
|
||||
}
|
||||
|
||||
if !strings.Contains(got, "listen_addr=:9191") {
|
||||
t.Fatalf("execute() output = %q, want listen addr summary", got)
|
||||
}
|
||||
|
||||
if !strings.Contains(got, "sqlite_dsn=file:test.db?_foreign_keys=on") {
|
||||
t.Fatalf("execute() output = %q, want sqlite dsn summary", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReturnsBootstrapError(t *testing.T) {
|
||||
wantErr := errors.New("load config failed")
|
||||
|
||||
err := execute(context.Background(), &bytes.Buffer{}, func(context.Context) (config.StartupConfig, error) {
|
||||
return config.StartupConfig{}, wantErr
|
||||
})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("execute() error = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteReturnsWriteError(t *testing.T) {
|
||||
wantErr := errors.New("write failed")
|
||||
|
||||
err := execute(context.Background(), errWriter{err: wantErr}, func(context.Context) (config.StartupConfig, error) {
|
||||
return config.StartupConfig{
|
||||
Server: config.ServerConfig{ListenAddr: ":9292"},
|
||||
Database: config.DatabaseConfig{SQLiteDSN: "file:test.db"},
|
||||
}, nil
|
||||
})
|
||||
if !errors.Is(err, wantErr) {
|
||||
t.Fatalf("execute() error = %v, want %v", err, wantErr)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user