Feat: add POSIX-lite bootstrap foundation
This commit is contained in:
@@ -4,8 +4,11 @@ package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -41,7 +44,8 @@ var (
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *database.DB
|
||||
db *database.DB
|
||||
posixRoot string
|
||||
}
|
||||
|
||||
type SaveInstanceInput struct {
|
||||
@@ -172,8 +176,8 @@ type namedRecord struct {
|
||||
Slug string `json:"slug"`
|
||||
}
|
||||
|
||||
func NewService(db *database.DB) *Service {
|
||||
return &Service{db: db}
|
||||
func NewService(db *database.DB, posixRoot string) *Service {
|
||||
return &Service{db: db, posixRoot: strings.TrimSpace(posixRoot)}
|
||||
}
|
||||
|
||||
func (service *Service) SaveInstance(ctx context.Context, input SaveInstanceInput) (InstallationRecord, error) {
|
||||
@@ -405,6 +409,10 @@ func (service *Service) SaveStructure(ctx context.Context, input SaveStructureIn
|
||||
return StructureRecord{}, err
|
||||
}
|
||||
|
||||
if err := service.ensureBootstrapPOSIXSkeleton(installation, admin, organization, department, team, project); err != nil {
|
||||
return StructureRecord{}, err
|
||||
}
|
||||
|
||||
return StructureRecord{
|
||||
Installation: installation,
|
||||
Organization: organization,
|
||||
@@ -901,3 +909,173 @@ func personalHomeTitle(displayName string) string {
|
||||
|
||||
return fmt.Sprintf("%s's Home", trimmedDisplayName)
|
||||
}
|
||||
|
||||
func (service *Service) ensureBootstrapPOSIXSkeleton(
|
||||
installation InstallationRecord,
|
||||
admin AdminSummary,
|
||||
organization namedRecord,
|
||||
department namedRecord,
|
||||
team namedRecord,
|
||||
project namedRecord,
|
||||
) error {
|
||||
rootPath := strings.TrimSpace(service.posixRoot)
|
||||
if rootPath == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(rootPath, 0o755); err != nil {
|
||||
return fmt.Errorf("create POSIX root: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(rootPath, "settings.json"), map[string]any{
|
||||
"installation": map[string]any{
|
||||
"id": installation.ID,
|
||||
"name": installation.Name,
|
||||
"mode": installation.Mode,
|
||||
"access": installation.Access,
|
||||
"protocol": installation.Protocol,
|
||||
"host": installation.Host,
|
||||
"isBootstrapped": installation.IsBootstrapped,
|
||||
},
|
||||
"organization": map[string]any{
|
||||
"id": organization.ID,
|
||||
"name": organization.Name,
|
||||
"slug": organization.Slug,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write tenant settings.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(rootPath, "layout.json"), map[string]any{
|
||||
"version": 1,
|
||||
"type": "tenant-layout",
|
||||
"home": map[string]any{
|
||||
"defaultProjectSlug": project.Slug,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write tenant layout.json: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(rootPath, "catalog", "packs"), 0o755); err != nil {
|
||||
return fmt.Errorf("create catalog packs root: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(rootPath, "catalog", "standalone"), 0o755); err != nil {
|
||||
return fmt.Errorf("create catalog standalone root: %w", err)
|
||||
}
|
||||
|
||||
departmentPath := filepath.Join(rootPath, "departments", slugDir("department", department.Slug))
|
||||
teamPath := filepath.Join(departmentPath, "teams", slugDir("team", team.Slug))
|
||||
projectPath := filepath.Join(rootPath, "projects", slugDir("project", project.Slug))
|
||||
usersPath := filepath.Join(rootPath, "users")
|
||||
|
||||
for _, dirPath := range []string{
|
||||
departmentPath,
|
||||
teamPath,
|
||||
projectPath,
|
||||
filepath.Join(projectPath, "tree"),
|
||||
filepath.Join(usersPath, "personals"),
|
||||
} {
|
||||
if err := os.MkdirAll(dirPath, 0o755); err != nil {
|
||||
return fmt.Errorf("create POSIX directory %s: %w", dirPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(departmentPath, "settings.json"), map[string]any{
|
||||
"id": department.ID,
|
||||
"name": department.Name,
|
||||
"slug": department.Slug,
|
||||
"type": "department",
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write department settings.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(departmentPath, "users.json"), map[string]any{
|
||||
"owners": []map[string]string{{
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"displayName": admin.DisplayName,
|
||||
}},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write department users.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(teamPath, "settings.json"), map[string]any{
|
||||
"id": team.ID,
|
||||
"name": team.Name,
|
||||
"slug": team.Slug,
|
||||
"type": "team",
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write team settings.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(teamPath, "users.json"), map[string]any{
|
||||
"owners": []map[string]string{{
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"displayName": admin.DisplayName,
|
||||
}},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write team users.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(projectPath, "settings.json"), map[string]any{
|
||||
"id": project.ID,
|
||||
"name": project.Name,
|
||||
"slug": project.Slug,
|
||||
"type": "project",
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write project settings.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(projectPath, "home.json"), map[string]any{
|
||||
"type": "project-home",
|
||||
"project": project.Slug,
|
||||
"widgets": []any{},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write project home.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(usersPath, "settings.json"), map[string]any{
|
||||
"primaryAdminId": admin.ID,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write users settings.json: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(usersPath, "data.json"), map[string]any{
|
||||
"users": []map[string]string{{
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"displayName": admin.DisplayName,
|
||||
}},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write users data.json: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func slugDir(prefix, slug string) string {
|
||||
trimmedSlug := strings.TrimSpace(slug)
|
||||
if trimmedSlug == "" {
|
||||
return prefix
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s-%s", prefix, trimmedSlug)
|
||||
}
|
||||
|
||||
func writeJSONFile(path string, payload any) error {
|
||||
parentDir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(parentDir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(payload, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data = append(data, '\n')
|
||||
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user