Add files using upload-large-folder tool
Browse files- aiml/02_models/elizabeth/deployment_configs/.env.cloudflare.example +18 -0
- aiml/02_models/elizabeth/legacy_workspace/.env +42 -0
- aiml/04_data/etl_pipelines/legacy_etl/corpus-pipeline/.env +34 -0
- projects/ui/crush/internal/session/session.go +156 -0
- projects/ui/crush/internal/shell/command_block_test.go +377 -0
- projects/ui/crush/internal/shell/comparison_test.go +41 -0
- projects/ui/crush/internal/shell/doc.go +30 -0
- projects/ui/crush/internal/shell/persistent.go +37 -0
- projects/ui/crush/internal/shell/shell.go +276 -0
- projects/ui/crush/internal/shell/shell_test.go +120 -0
aiml/02_models/elizabeth/deployment_configs/.env.cloudflare.example
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Cloudflare API Configuration
|
| 2 |
+
# Copy this file to .env.cloudflare and fill in your credentials
|
| 3 |
+
|
| 4 |
+
CLOUDFLARE_API_TOKEN=your_cloudflare_api_token_here
|
| 5 |
+
CLOUDFLARE_ZONE_ID=your_cloudflare_zone_id_here
|
| 6 |
+
CLOUDFLARE_ACCOUNT_ID=your_cloudflare_account_id_here
|
| 7 |
+
|
| 8 |
+
# Server Configuration (update with actual server IP)
|
| 9 |
+
SERVER_PUBLIC_IP=your_server_public_ip_here
|
| 10 |
+
SERVER_PORT=8000
|
| 11 |
+
|
| 12 |
+
# Domain Configuration
|
| 13 |
+
PRIMARY_DOMAIN=adaptdev.ai
|
| 14 |
+
CUSTOM_DOMAIN=corpus.adaptdev.ai
|
| 15 |
+
CDN_BASE=https://cdn.adaptdev.ai
|
| 16 |
+
|
| 17 |
+
# API Configuration
|
| 18 |
+
API_BASE_URL=https://api.cloudflare.com/client/v4
|
aiml/02_models/elizabeth/legacy_workspace/.env
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Elizabeth Environment Variables
|
| 2 |
+
ELIZABETH_VERSION=v0.0.2
|
| 3 |
+
VLLM_BASE_URL=http://localhost:8
|
| 4 |
+
MODEL_PATH=/workspace/models/qwen3-8b
|
| 5 |
+
|
| 6 |
+
# Database Connections
|
| 7 |
+
PG_HOST=localhost
|
| 8 |
+
PG_DATABASE=elizabeth_prod
|
| 9 |
+
PG_USER=elizabeth
|
| 10 |
+
PG_PASSWORD=
|
| 11 |
+
|
| 12 |
+
REDIS_HOST=localhost
|
| 13 |
+
REDIS_PORT=6379
|
| 14 |
+
|
| 15 |
+
MONGO_URI=mongodb://localhost:27017/
|
| 16 |
+
|
| 17 |
+
# HuggingFace API Configuration - #FRESH - 250705
|
| 18 |
+
HUGGING_FACE_API_KEY=
|
| 19 |
+
HF_TOKEN=
|
| 20 |
+
HF_ORG=LevelUp2x
|
| 21 |
+
|
| 22 |
+
# Xet Configuration
|
| 23 |
+
XET_REPO_URL=https://xetbeta.com/adaptnova/elizabeth-data
|
| 24 |
+
XET_LOCAL_PATH=/workspace/xet_data
|
| 25 |
+
|
| 26 |
+
# Search API Keys - Configure for enhanced web search
|
| 27 |
+
FIRECRAWL_API_KEY=fc-94ebc24ae6a4492f9022d6a83001fc54
|
| 28 |
+
TAVILY_API_KEY=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
|
| 29 |
+
SERPER_API_KEY=aace3627d2b7d008f85ce06100984ae7c2a2066f
|
| 30 |
+
|
| 31 |
+
# Search Configuration
|
| 32 |
+
SEARCH_PROVIDER=firecrawl # firecrawl, tavily, serper, duckduckgo, perplexity
|
| 33 |
+
SEARCH_MAX_RESULTS=5
|
| 34 |
+
SEARCH_TIMEOUT=30
|
| 35 |
+
|
| 36 |
+
# Additional Search Services
|
| 37 |
+
PERPLEXITY_API_KEY=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
|
| 38 |
+
|
| 39 |
+
# Algolia Search Configuration
|
| 40 |
+
ALGOLIA_APPLICATION_ID=H23FYV09PA
|
| 41 |
+
ALGOLIA_SEARCH_API_KEY=7d71ebe7bf1f76a6bb14aecfae798372
|
| 42 |
+
ALGOLIA_WRITE_API_KEY=4d8787832cc1a7920f68df7e791e75bb
|
aiml/04_data/etl_pipelines/legacy_etl/corpus-pipeline/.env
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Nebius COS S3 Configuration
|
| 2 |
+
AWS_ACCESS_KEY_ID=NAKIK7HQMWO2I8Y315Y6
|
| 3 |
+
AWS_SECRET_ACCESS_KEY=O7+KZpqwNfAMHV3cz6anSaFz3f8ppI1M1cfEeYU5
|
| 4 |
+
AWS_ENDPOINT_URL=https://storage.us-central1.nebius.cloud:443
|
| 5 |
+
AWS_DEFAULT_REGION=us-central1
|
| 6 |
+
S3_BUCKET=cos
|
| 7 |
+
|
| 8 |
+
# Nebius API Configuration
|
| 9 |
+
NEBIUS_TENANT_ID=tenant-e00x2jmwjyzm485zsy
|
| 10 |
+
NEBIUS_SERVICE_ACCOUNT_ID=serviceaccount-e00q63c932nw0qp6s0
|
| 11 |
+
NEBIUS_API_KEY_ID=NAKIK7HQMWO2I8Y315Y6
|
| 12 |
+
|
| 13 |
+
# Google Cloud Storage
|
| 14 |
+
GCS_BUCKET=gs://gc-cos
|
| 15 |
+
|
| 16 |
+
# NFS Mount Points
|
| 17 |
+
NFS_INTERNAL=10.128.0.3:/lssd
|
| 18 |
+
NFS_EXTERNAL=35.223.4.118:/lssd
|
| 19 |
+
LOCAL_MOUNT=/mnt/corpus-storage
|
| 20 |
+
|
| 21 |
+
# Pipeline Configuration
|
| 22 |
+
CORPUS_DIR=/data/adaptai/corpus-data
|
| 23 |
+
RAW_DIR=/data/adaptai/corpus-data/raw
|
| 24 |
+
PROCESSED_DIR=/data/adaptai/corpus-data/processed
|
| 25 |
+
TRAINING_DIR=/data/adaptai/corpus-data/training
|
| 26 |
+
LOG_DIR=/data/adaptai/corpus-data/logs
|
| 27 |
+
|
| 28 |
+
# PostgreSQL Configuration
|
| 29 |
+
POSTGRES_HOST=localhost
|
| 30 |
+
POSTGRES_PORT=5432
|
| 31 |
+
POSTGRES_DB=nova_conversations
|
| 32 |
+
POSTGRES_USER=mlops_etl_user
|
| 33 |
+
POSTGRES_PASSWORD=quantum_secure_20250824_vox_atlas_archimedes
|
| 34 |
+
POSTGRES_SCHEMA=conversation_corpus
|
projects/ui/crush/internal/session/session.go
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package session
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"database/sql"
|
| 6 |
+
|
| 7 |
+
"github.com/charmbracelet/crush/internal/db"
|
| 8 |
+
"github.com/charmbracelet/crush/internal/pubsub"
|
| 9 |
+
"github.com/google/uuid"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
type Session struct {
|
| 13 |
+
ID string
|
| 14 |
+
ParentSessionID string
|
| 15 |
+
Title string
|
| 16 |
+
MessageCount int64
|
| 17 |
+
PromptTokens int64
|
| 18 |
+
CompletionTokens int64
|
| 19 |
+
SummaryMessageID string
|
| 20 |
+
Cost float64
|
| 21 |
+
CreatedAt int64
|
| 22 |
+
UpdatedAt int64
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
type Service interface {
|
| 26 |
+
pubsub.Suscriber[Session]
|
| 27 |
+
Create(ctx context.Context, title string) (Session, error)
|
| 28 |
+
CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error)
|
| 29 |
+
CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error)
|
| 30 |
+
Get(ctx context.Context, id string) (Session, error)
|
| 31 |
+
List(ctx context.Context) ([]Session, error)
|
| 32 |
+
Save(ctx context.Context, session Session) (Session, error)
|
| 33 |
+
Delete(ctx context.Context, id string) error
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
type service struct {
|
| 37 |
+
*pubsub.Broker[Session]
|
| 38 |
+
q db.Querier
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
func (s *service) Create(ctx context.Context, title string) (Session, error) {
|
| 42 |
+
dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
|
| 43 |
+
ID: uuid.New().String(),
|
| 44 |
+
Title: title,
|
| 45 |
+
})
|
| 46 |
+
if err != nil {
|
| 47 |
+
return Session{}, err
|
| 48 |
+
}
|
| 49 |
+
session := s.fromDBItem(dbSession)
|
| 50 |
+
s.Publish(pubsub.CreatedEvent, session)
|
| 51 |
+
return session, nil
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
func (s *service) CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error) {
|
| 55 |
+
dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
|
| 56 |
+
ID: toolCallID,
|
| 57 |
+
ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
|
| 58 |
+
Title: title,
|
| 59 |
+
})
|
| 60 |
+
if err != nil {
|
| 61 |
+
return Session{}, err
|
| 62 |
+
}
|
| 63 |
+
session := s.fromDBItem(dbSession)
|
| 64 |
+
s.Publish(pubsub.CreatedEvent, session)
|
| 65 |
+
return session, nil
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
func (s *service) CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error) {
|
| 69 |
+
dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
|
| 70 |
+
ID: "title-" + parentSessionID,
|
| 71 |
+
ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
|
| 72 |
+
Title: "Generate a title",
|
| 73 |
+
})
|
| 74 |
+
if err != nil {
|
| 75 |
+
return Session{}, err
|
| 76 |
+
}
|
| 77 |
+
session := s.fromDBItem(dbSession)
|
| 78 |
+
s.Publish(pubsub.CreatedEvent, session)
|
| 79 |
+
return session, nil
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
func (s *service) Delete(ctx context.Context, id string) error {
|
| 83 |
+
session, err := s.Get(ctx, id)
|
| 84 |
+
if err != nil {
|
| 85 |
+
return err
|
| 86 |
+
}
|
| 87 |
+
err = s.q.DeleteSession(ctx, session.ID)
|
| 88 |
+
if err != nil {
|
| 89 |
+
return err
|
| 90 |
+
}
|
| 91 |
+
s.Publish(pubsub.DeletedEvent, session)
|
| 92 |
+
return nil
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
func (s *service) Get(ctx context.Context, id string) (Session, error) {
|
| 96 |
+
dbSession, err := s.q.GetSessionByID(ctx, id)
|
| 97 |
+
if err != nil {
|
| 98 |
+
return Session{}, err
|
| 99 |
+
}
|
| 100 |
+
return s.fromDBItem(dbSession), nil
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
func (s *service) Save(ctx context.Context, session Session) (Session, error) {
|
| 104 |
+
dbSession, err := s.q.UpdateSession(ctx, db.UpdateSessionParams{
|
| 105 |
+
ID: session.ID,
|
| 106 |
+
Title: session.Title,
|
| 107 |
+
PromptTokens: session.PromptTokens,
|
| 108 |
+
CompletionTokens: session.CompletionTokens,
|
| 109 |
+
SummaryMessageID: sql.NullString{
|
| 110 |
+
String: session.SummaryMessageID,
|
| 111 |
+
Valid: session.SummaryMessageID != "",
|
| 112 |
+
},
|
| 113 |
+
Cost: session.Cost,
|
| 114 |
+
})
|
| 115 |
+
if err != nil {
|
| 116 |
+
return Session{}, err
|
| 117 |
+
}
|
| 118 |
+
session = s.fromDBItem(dbSession)
|
| 119 |
+
s.Publish(pubsub.UpdatedEvent, session)
|
| 120 |
+
return session, nil
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
func (s *service) List(ctx context.Context) ([]Session, error) {
|
| 124 |
+
dbSessions, err := s.q.ListSessions(ctx)
|
| 125 |
+
if err != nil {
|
| 126 |
+
return nil, err
|
| 127 |
+
}
|
| 128 |
+
sessions := make([]Session, len(dbSessions))
|
| 129 |
+
for i, dbSession := range dbSessions {
|
| 130 |
+
sessions[i] = s.fromDBItem(dbSession)
|
| 131 |
+
}
|
| 132 |
+
return sessions, nil
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
func (s service) fromDBItem(item db.Session) Session {
|
| 136 |
+
return Session{
|
| 137 |
+
ID: item.ID,
|
| 138 |
+
ParentSessionID: item.ParentSessionID.String,
|
| 139 |
+
Title: item.Title,
|
| 140 |
+
MessageCount: item.MessageCount,
|
| 141 |
+
PromptTokens: item.PromptTokens,
|
| 142 |
+
CompletionTokens: item.CompletionTokens,
|
| 143 |
+
SummaryMessageID: item.SummaryMessageID.String,
|
| 144 |
+
Cost: item.Cost,
|
| 145 |
+
CreatedAt: item.CreatedAt,
|
| 146 |
+
UpdatedAt: item.UpdatedAt,
|
| 147 |
+
}
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
func NewService(q db.Querier) Service {
|
| 151 |
+
broker := pubsub.NewBroker[Session]()
|
| 152 |
+
return &service{
|
| 153 |
+
broker,
|
| 154 |
+
q,
|
| 155 |
+
}
|
| 156 |
+
}
|
projects/ui/crush/internal/shell/command_block_test.go
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package shell
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"strings"
|
| 6 |
+
"testing"
|
| 7 |
+
|
| 8 |
+
"github.com/stretchr/testify/require"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
func TestCommandBlocking(t *testing.T) {
|
| 12 |
+
tests := []struct {
|
| 13 |
+
name string
|
| 14 |
+
blockFuncs []BlockFunc
|
| 15 |
+
command string
|
| 16 |
+
shouldBlock bool
|
| 17 |
+
}{
|
| 18 |
+
{
|
| 19 |
+
name: "block simple command",
|
| 20 |
+
blockFuncs: []BlockFunc{
|
| 21 |
+
func(args []string) bool {
|
| 22 |
+
return len(args) > 0 && args[0] == "curl"
|
| 23 |
+
},
|
| 24 |
+
},
|
| 25 |
+
command: "curl https://example.com",
|
| 26 |
+
shouldBlock: true,
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
name: "allow non-blocked command",
|
| 30 |
+
blockFuncs: []BlockFunc{
|
| 31 |
+
func(args []string) bool {
|
| 32 |
+
return len(args) > 0 && args[0] == "curl"
|
| 33 |
+
},
|
| 34 |
+
},
|
| 35 |
+
command: "echo hello",
|
| 36 |
+
shouldBlock: false,
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
name: "block subcommand",
|
| 40 |
+
blockFuncs: []BlockFunc{
|
| 41 |
+
func(args []string) bool {
|
| 42 |
+
return len(args) >= 2 && args[0] == "brew" && args[1] == "install"
|
| 43 |
+
},
|
| 44 |
+
},
|
| 45 |
+
command: "brew install wget",
|
| 46 |
+
shouldBlock: true,
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
name: "allow different subcommand",
|
| 50 |
+
blockFuncs: []BlockFunc{
|
| 51 |
+
func(args []string) bool {
|
| 52 |
+
return len(args) >= 2 && args[0] == "brew" && args[1] == "install"
|
| 53 |
+
},
|
| 54 |
+
},
|
| 55 |
+
command: "brew list",
|
| 56 |
+
shouldBlock: false,
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
name: "block npm global install with -g",
|
| 60 |
+
blockFuncs: []BlockFunc{
|
| 61 |
+
ArgumentsBlocker("npm", []string{"install"}, []string{"-g"}),
|
| 62 |
+
},
|
| 63 |
+
command: "npm install -g typescript",
|
| 64 |
+
shouldBlock: true,
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
name: "block npm global install with --global",
|
| 68 |
+
blockFuncs: []BlockFunc{
|
| 69 |
+
ArgumentsBlocker("npm", []string{"install"}, []string{"--global"}),
|
| 70 |
+
},
|
| 71 |
+
command: "npm install --global typescript",
|
| 72 |
+
shouldBlock: true,
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
name: "allow npm local install",
|
| 76 |
+
blockFuncs: []BlockFunc{
|
| 77 |
+
ArgumentsBlocker("npm", []string{"install"}, []string{"-g"}),
|
| 78 |
+
ArgumentsBlocker("npm", []string{"install"}, []string{"--global"}),
|
| 79 |
+
},
|
| 80 |
+
command: "npm install typescript",
|
| 81 |
+
shouldBlock: false,
|
| 82 |
+
},
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
for _, tt := range tests {
|
| 86 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 87 |
+
// Create a temporary directory for each test
|
| 88 |
+
tmpDir := t.TempDir()
|
| 89 |
+
|
| 90 |
+
shell := NewShell(&Options{
|
| 91 |
+
WorkingDir: tmpDir,
|
| 92 |
+
BlockFuncs: tt.blockFuncs,
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
_, _, err := shell.Exec(context.Background(), tt.command)
|
| 96 |
+
|
| 97 |
+
if tt.shouldBlock {
|
| 98 |
+
if err == nil {
|
| 99 |
+
t.Errorf("Expected command to be blocked, but it was allowed")
|
| 100 |
+
} else if !strings.Contains(err.Error(), "not allowed for security reasons") {
|
| 101 |
+
t.Errorf("Expected security error, got: %v", err)
|
| 102 |
+
}
|
| 103 |
+
} else {
|
| 104 |
+
// For non-blocked commands, we might get other errors (like command not found)
|
| 105 |
+
// but we shouldn't get the security error
|
| 106 |
+
if err != nil && strings.Contains(err.Error(), "not allowed for security reasons") {
|
| 107 |
+
t.Errorf("Command was unexpectedly blocked: %v", err)
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
})
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
func TestArgumentsBlocker(t *testing.T) {
|
| 115 |
+
tests := []struct {
|
| 116 |
+
name string
|
| 117 |
+
cmd string
|
| 118 |
+
args []string
|
| 119 |
+
flags []string
|
| 120 |
+
input []string
|
| 121 |
+
shouldBlock bool
|
| 122 |
+
}{
|
| 123 |
+
// Basic command blocking
|
| 124 |
+
{
|
| 125 |
+
name: "block exact command match",
|
| 126 |
+
cmd: "npm",
|
| 127 |
+
args: []string{"install"},
|
| 128 |
+
flags: nil,
|
| 129 |
+
input: []string{"npm", "install", "package"},
|
| 130 |
+
shouldBlock: true,
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
name: "allow different command",
|
| 134 |
+
cmd: "npm",
|
| 135 |
+
args: []string{"install"},
|
| 136 |
+
flags: nil,
|
| 137 |
+
input: []string{"yarn", "install", "package"},
|
| 138 |
+
shouldBlock: false,
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
name: "allow different subcommand",
|
| 142 |
+
cmd: "npm",
|
| 143 |
+
args: []string{"install"},
|
| 144 |
+
flags: nil,
|
| 145 |
+
input: []string{"npm", "list"},
|
| 146 |
+
shouldBlock: false,
|
| 147 |
+
},
|
| 148 |
+
|
| 149 |
+
// Flag-based blocking
|
| 150 |
+
{
|
| 151 |
+
name: "block with single flag",
|
| 152 |
+
cmd: "npm",
|
| 153 |
+
args: []string{"install"},
|
| 154 |
+
flags: []string{"-g"},
|
| 155 |
+
input: []string{"npm", "install", "-g", "typescript"},
|
| 156 |
+
shouldBlock: true,
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
name: "block with flag in different position",
|
| 160 |
+
cmd: "npm",
|
| 161 |
+
args: []string{"install"},
|
| 162 |
+
flags: []string{"-g"},
|
| 163 |
+
input: []string{"npm", "install", "typescript", "-g"},
|
| 164 |
+
shouldBlock: true,
|
| 165 |
+
},
|
| 166 |
+
{
|
| 167 |
+
name: "allow without required flag",
|
| 168 |
+
cmd: "npm",
|
| 169 |
+
args: []string{"install"},
|
| 170 |
+
flags: []string{"-g"},
|
| 171 |
+
input: []string{"npm", "install", "typescript"},
|
| 172 |
+
shouldBlock: false,
|
| 173 |
+
},
|
| 174 |
+
{
|
| 175 |
+
name: "block with multiple flags",
|
| 176 |
+
cmd: "pip",
|
| 177 |
+
args: []string{"install"},
|
| 178 |
+
flags: []string{"--user"},
|
| 179 |
+
input: []string{"pip", "install", "--user", "--upgrade", "package"},
|
| 180 |
+
shouldBlock: true,
|
| 181 |
+
},
|
| 182 |
+
|
| 183 |
+
// Complex argument patterns
|
| 184 |
+
{
|
| 185 |
+
name: "block multi-arg subcommand",
|
| 186 |
+
cmd: "yarn",
|
| 187 |
+
args: []string{"global", "add"},
|
| 188 |
+
flags: nil,
|
| 189 |
+
input: []string{"yarn", "global", "add", "typescript"},
|
| 190 |
+
shouldBlock: true,
|
| 191 |
+
},
|
| 192 |
+
{
|
| 193 |
+
name: "allow partial multi-arg match",
|
| 194 |
+
cmd: "yarn",
|
| 195 |
+
args: []string{"global", "add"},
|
| 196 |
+
flags: nil,
|
| 197 |
+
input: []string{"yarn", "global", "list"},
|
| 198 |
+
shouldBlock: false,
|
| 199 |
+
},
|
| 200 |
+
|
| 201 |
+
// Edge cases
|
| 202 |
+
{
|
| 203 |
+
name: "handle empty input",
|
| 204 |
+
cmd: "npm",
|
| 205 |
+
args: []string{"install"},
|
| 206 |
+
flags: nil,
|
| 207 |
+
input: []string{},
|
| 208 |
+
shouldBlock: false,
|
| 209 |
+
},
|
| 210 |
+
{
|
| 211 |
+
name: "handle command only",
|
| 212 |
+
cmd: "npm",
|
| 213 |
+
args: []string{"install"},
|
| 214 |
+
flags: nil,
|
| 215 |
+
input: []string{"npm"},
|
| 216 |
+
shouldBlock: false,
|
| 217 |
+
},
|
| 218 |
+
{
|
| 219 |
+
name: "block pacman with -S flag",
|
| 220 |
+
cmd: "pacman",
|
| 221 |
+
args: nil,
|
| 222 |
+
flags: []string{"-S"},
|
| 223 |
+
input: []string{"pacman", "-S", "package"},
|
| 224 |
+
shouldBlock: true,
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
name: "allow pacman without -S flag",
|
| 228 |
+
cmd: "pacman",
|
| 229 |
+
args: nil,
|
| 230 |
+
flags: []string{"-S"},
|
| 231 |
+
input: []string{"pacman", "-Q", "package"},
|
| 232 |
+
shouldBlock: false,
|
| 233 |
+
},
|
| 234 |
+
|
| 235 |
+
// `go test -exec`
|
| 236 |
+
{
|
| 237 |
+
name: "go test exec",
|
| 238 |
+
cmd: "go",
|
| 239 |
+
args: []string{"test"},
|
| 240 |
+
flags: []string{"-exec"},
|
| 241 |
+
input: []string{"go", "test", "-exec", "bash -c 'echo hello'"},
|
| 242 |
+
shouldBlock: true,
|
| 243 |
+
},
|
| 244 |
+
{
|
| 245 |
+
name: "go test exec",
|
| 246 |
+
cmd: "go",
|
| 247 |
+
args: []string{"test"},
|
| 248 |
+
flags: []string{"-exec"},
|
| 249 |
+
input: []string{"go", "test", `-exec="bash -c 'echo hello'"`},
|
| 250 |
+
shouldBlock: true,
|
| 251 |
+
},
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
for _, tt := range tests {
|
| 255 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 256 |
+
blocker := ArgumentsBlocker(tt.cmd, tt.args, tt.flags)
|
| 257 |
+
result := blocker(tt.input)
|
| 258 |
+
require.Equal(t, tt.shouldBlock, result,
|
| 259 |
+
"Expected block=%v for input %v", tt.shouldBlock, tt.input)
|
| 260 |
+
})
|
| 261 |
+
}
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
func TestCommandsBlocker(t *testing.T) {
|
| 265 |
+
tests := []struct {
|
| 266 |
+
name string
|
| 267 |
+
banned []string
|
| 268 |
+
input []string
|
| 269 |
+
shouldBlock bool
|
| 270 |
+
}{
|
| 271 |
+
{
|
| 272 |
+
name: "block single banned command",
|
| 273 |
+
banned: []string{"curl"},
|
| 274 |
+
input: []string{"curl", "https://example.com"},
|
| 275 |
+
shouldBlock: true,
|
| 276 |
+
},
|
| 277 |
+
{
|
| 278 |
+
name: "allow non-banned command",
|
| 279 |
+
banned: []string{"curl", "wget"},
|
| 280 |
+
input: []string{"echo", "hello"},
|
| 281 |
+
shouldBlock: false,
|
| 282 |
+
},
|
| 283 |
+
{
|
| 284 |
+
name: "block from multiple banned",
|
| 285 |
+
banned: []string{"curl", "wget", "nc"},
|
| 286 |
+
input: []string{"wget", "https://example.com"},
|
| 287 |
+
shouldBlock: true,
|
| 288 |
+
},
|
| 289 |
+
{
|
| 290 |
+
name: "handle empty input",
|
| 291 |
+
banned: []string{"curl"},
|
| 292 |
+
input: []string{},
|
| 293 |
+
shouldBlock: false,
|
| 294 |
+
},
|
| 295 |
+
{
|
| 296 |
+
name: "case sensitive matching",
|
| 297 |
+
banned: []string{"curl"},
|
| 298 |
+
input: []string{"CURL", "https://example.com"},
|
| 299 |
+
shouldBlock: false,
|
| 300 |
+
},
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
for _, tt := range tests {
|
| 304 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 305 |
+
blocker := CommandsBlocker(tt.banned)
|
| 306 |
+
result := blocker(tt.input)
|
| 307 |
+
require.Equal(t, tt.shouldBlock, result,
|
| 308 |
+
"Expected block=%v for input %v", tt.shouldBlock, tt.input)
|
| 309 |
+
})
|
| 310 |
+
}
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
func TestSplitArgsFlags(t *testing.T) {
|
| 314 |
+
tests := []struct {
|
| 315 |
+
name string
|
| 316 |
+
input []string
|
| 317 |
+
wantArgs []string
|
| 318 |
+
wantFlags []string
|
| 319 |
+
}{
|
| 320 |
+
{
|
| 321 |
+
name: "only args",
|
| 322 |
+
input: []string{"install", "package", "another"},
|
| 323 |
+
wantArgs: []string{"install", "package", "another"},
|
| 324 |
+
wantFlags: []string{},
|
| 325 |
+
},
|
| 326 |
+
{
|
| 327 |
+
name: "only flags",
|
| 328 |
+
input: []string{"-g", "--verbose", "-f"},
|
| 329 |
+
wantArgs: []string{},
|
| 330 |
+
wantFlags: []string{"-g", "--verbose", "-f"},
|
| 331 |
+
},
|
| 332 |
+
{
|
| 333 |
+
name: "mixed args and flags",
|
| 334 |
+
input: []string{"install", "-g", "package", "--verbose"},
|
| 335 |
+
wantArgs: []string{"install", "package"},
|
| 336 |
+
wantFlags: []string{"-g", "--verbose"},
|
| 337 |
+
},
|
| 338 |
+
{
|
| 339 |
+
name: "empty input",
|
| 340 |
+
input: []string{},
|
| 341 |
+
wantArgs: []string{},
|
| 342 |
+
wantFlags: []string{},
|
| 343 |
+
},
|
| 344 |
+
{
|
| 345 |
+
name: "single dash flag",
|
| 346 |
+
input: []string{"-S", "package"},
|
| 347 |
+
wantArgs: []string{"package"},
|
| 348 |
+
wantFlags: []string{"-S"},
|
| 349 |
+
},
|
| 350 |
+
{
|
| 351 |
+
name: "flag with equals sign",
|
| 352 |
+
input: []string{"-exec=bash", "package"},
|
| 353 |
+
wantArgs: []string{"package"},
|
| 354 |
+
wantFlags: []string{"-exec"},
|
| 355 |
+
},
|
| 356 |
+
{
|
| 357 |
+
name: "long flag with equals sign",
|
| 358 |
+
input: []string{"--config=/path/to/config", "run"},
|
| 359 |
+
wantArgs: []string{"run"},
|
| 360 |
+
wantFlags: []string{"--config"},
|
| 361 |
+
},
|
| 362 |
+
{
|
| 363 |
+
name: "flag with complex value",
|
| 364 |
+
input: []string{`-exec="bash -c 'echo hello'"`, "test"},
|
| 365 |
+
wantArgs: []string{"test"},
|
| 366 |
+
wantFlags: []string{"-exec"},
|
| 367 |
+
},
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
for _, tt := range tests {
|
| 371 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 372 |
+
args, flags := splitArgsFlags(tt.input)
|
| 373 |
+
require.Equal(t, tt.wantArgs, args, "args mismatch")
|
| 374 |
+
require.Equal(t, tt.wantFlags, flags, "flags mismatch")
|
| 375 |
+
})
|
| 376 |
+
}
|
| 377 |
+
}
|
projects/ui/crush/internal/shell/comparison_test.go
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package shell
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"testing"
|
| 5 |
+
"time"
|
| 6 |
+
|
| 7 |
+
"github.com/stretchr/testify/require"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
func TestShellPerformanceComparison(t *testing.T) {
|
| 11 |
+
shell := NewShell(&Options{WorkingDir: t.TempDir()})
|
| 12 |
+
|
| 13 |
+
// Test quick command
|
| 14 |
+
start := time.Now()
|
| 15 |
+
stdout, stderr, err := shell.Exec(t.Context(), "echo 'hello'")
|
| 16 |
+
exitCode := ExitCode(err)
|
| 17 |
+
duration := time.Since(start)
|
| 18 |
+
|
| 19 |
+
require.NoError(t, err)
|
| 20 |
+
require.Equal(t, 0, exitCode)
|
| 21 |
+
require.Contains(t, stdout, "hello")
|
| 22 |
+
require.Empty(t, stderr)
|
| 23 |
+
|
| 24 |
+
t.Logf("Quick command took: %v", duration)
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// Benchmark CPU usage during polling
|
| 28 |
+
func BenchmarkShellPolling(b *testing.B) {
|
| 29 |
+
shell := NewShell(&Options{WorkingDir: b.TempDir()})
|
| 30 |
+
|
| 31 |
+
b.ReportAllocs()
|
| 32 |
+
|
| 33 |
+
for b.Loop() {
|
| 34 |
+
// Use a short sleep to measure polling overhead
|
| 35 |
+
_, _, err := shell.Exec(b.Context(), "sleep 0.02")
|
| 36 |
+
exitCode := ExitCode(err)
|
| 37 |
+
if err != nil || exitCode != 0 {
|
| 38 |
+
b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
}
|
projects/ui/crush/internal/shell/doc.go
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package shell
|
| 2 |
+
|
| 3 |
+
// Example usage of the shell package:
|
| 4 |
+
//
|
| 5 |
+
// 1. For one-off commands:
|
| 6 |
+
//
|
| 7 |
+
// shell := shell.NewShell(nil)
|
| 8 |
+
// stdout, stderr, err := shell.Exec(context.Background(), "echo hello")
|
| 9 |
+
//
|
| 10 |
+
// 2. For maintaining state across commands:
|
| 11 |
+
//
|
| 12 |
+
// shell := shell.NewShell(&shell.Options{
|
| 13 |
+
// WorkingDir: "/tmp",
|
| 14 |
+
// Logger: myLogger,
|
| 15 |
+
// })
|
| 16 |
+
// shell.Exec(ctx, "export FOO=bar")
|
| 17 |
+
// shell.Exec(ctx, "echo $FOO") // Will print "bar"
|
| 18 |
+
//
|
| 19 |
+
// 3. For the singleton persistent shell (used by tools):
|
| 20 |
+
//
|
| 21 |
+
// shell := shell.GetPersistentShell("/path/to/cwd")
|
| 22 |
+
// stdout, stderr, err := shell.Exec(ctx, "ls -la")
|
| 23 |
+
//
|
| 24 |
+
// 4. Managing environment and working directory:
|
| 25 |
+
//
|
| 26 |
+
// shell := shell.NewShell(nil)
|
| 27 |
+
// shell.SetEnv("MY_VAR", "value")
|
| 28 |
+
// shell.SetWorkingDir("/tmp")
|
| 29 |
+
// cwd := shell.GetWorkingDir()
|
| 30 |
+
// env := shell.GetEnv()
|
projects/ui/crush/internal/shell/persistent.go
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package shell
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"log/slog"
|
| 5 |
+
"sync"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
// PersistentShell is a singleton shell instance that maintains state across the application
|
| 9 |
+
type PersistentShell struct {
|
| 10 |
+
*Shell
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
var (
|
| 14 |
+
once sync.Once
|
| 15 |
+
shellInstance *PersistentShell
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
// GetPersistentShell returns the singleton persistent shell instance
|
| 19 |
+
// This maintains backward compatibility with the existing API
|
| 20 |
+
func GetPersistentShell(cwd string) *PersistentShell {
|
| 21 |
+
once.Do(func() {
|
| 22 |
+
shellInstance = &PersistentShell{
|
| 23 |
+
Shell: NewShell(&Options{
|
| 24 |
+
WorkingDir: cwd,
|
| 25 |
+
Logger: &loggingAdapter{},
|
| 26 |
+
}),
|
| 27 |
+
}
|
| 28 |
+
})
|
| 29 |
+
return shellInstance
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// slog.dapter adapts the internal slog.package to the Logger interface
|
| 33 |
+
type loggingAdapter struct{}
|
| 34 |
+
|
| 35 |
+
func (l *loggingAdapter) InfoPersist(msg string, keysAndValues ...any) {
|
| 36 |
+
slog.Info(msg, keysAndValues...)
|
| 37 |
+
}
|
projects/ui/crush/internal/shell/shell.go
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Package shell provides cross-platform shell execution capabilities.
|
| 2 |
+
//
|
| 3 |
+
// This package offers two main types:
|
| 4 |
+
// - Shell: A general-purpose shell executor for one-off or managed commands
|
| 5 |
+
// - PersistentShell: A singleton shell that maintains state across the application
|
| 6 |
+
//
|
| 7 |
+
// WINDOWS COMPATIBILITY:
|
| 8 |
+
// This implementation provides both POSIX shell emulation (mvdan.cc/sh/v3),
|
| 9 |
+
// even on Windows. Some caution has to be taken: commands should have forward
|
| 10 |
+
// slashes (/) as path separators to work, even on Windows.
|
| 11 |
+
package shell
|
| 12 |
+
|
| 13 |
+
import (
|
| 14 |
+
"bytes"
|
| 15 |
+
"context"
|
| 16 |
+
"errors"
|
| 17 |
+
"fmt"
|
| 18 |
+
"os"
|
| 19 |
+
"slices"
|
| 20 |
+
"strings"
|
| 21 |
+
"sync"
|
| 22 |
+
|
| 23 |
+
"github.com/charmbracelet/x/exp/slice"
|
| 24 |
+
"mvdan.cc/sh/moreinterp/coreutils"
|
| 25 |
+
"mvdan.cc/sh/v3/expand"
|
| 26 |
+
"mvdan.cc/sh/v3/interp"
|
| 27 |
+
"mvdan.cc/sh/v3/syntax"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
// ShellType represents the type of shell to use
|
| 31 |
+
type ShellType int
|
| 32 |
+
|
| 33 |
+
const (
|
| 34 |
+
ShellTypePOSIX ShellType = iota
|
| 35 |
+
ShellTypeCmd
|
| 36 |
+
ShellTypePowerShell
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
// Logger interface for optional logging
|
| 40 |
+
type Logger interface {
|
| 41 |
+
InfoPersist(msg string, keysAndValues ...any)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
// noopLogger is a logger that does nothing
|
| 45 |
+
type noopLogger struct{}
|
| 46 |
+
|
| 47 |
+
func (noopLogger) InfoPersist(msg string, keysAndValues ...any) {}
|
| 48 |
+
|
| 49 |
+
// BlockFunc is a function that determines if a command should be blocked
|
| 50 |
+
type BlockFunc func(args []string) bool
|
| 51 |
+
|
| 52 |
+
// Shell provides cross-platform shell execution with optional state persistence
|
| 53 |
+
type Shell struct {
|
| 54 |
+
env []string
|
| 55 |
+
cwd string
|
| 56 |
+
mu sync.Mutex
|
| 57 |
+
logger Logger
|
| 58 |
+
blockFuncs []BlockFunc
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
// Options for creating a new shell
|
| 62 |
+
type Options struct {
|
| 63 |
+
WorkingDir string
|
| 64 |
+
Env []string
|
| 65 |
+
Logger Logger
|
| 66 |
+
BlockFuncs []BlockFunc
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// NewShell creates a new shell instance with the given options
|
| 70 |
+
func NewShell(opts *Options) *Shell {
|
| 71 |
+
if opts == nil {
|
| 72 |
+
opts = &Options{}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
cwd := opts.WorkingDir
|
| 76 |
+
if cwd == "" {
|
| 77 |
+
cwd, _ = os.Getwd()
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
env := opts.Env
|
| 81 |
+
if env == nil {
|
| 82 |
+
env = os.Environ()
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
logger := opts.Logger
|
| 86 |
+
if logger == nil {
|
| 87 |
+
logger = noopLogger{}
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
return &Shell{
|
| 91 |
+
cwd: cwd,
|
| 92 |
+
env: env,
|
| 93 |
+
logger: logger,
|
| 94 |
+
blockFuncs: opts.BlockFuncs,
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// Exec executes a command in the shell
|
| 99 |
+
func (s *Shell) Exec(ctx context.Context, command string) (string, string, error) {
|
| 100 |
+
s.mu.Lock()
|
| 101 |
+
defer s.mu.Unlock()
|
| 102 |
+
|
| 103 |
+
return s.execPOSIX(ctx, command)
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
// GetWorkingDir returns the current working directory
|
| 107 |
+
func (s *Shell) GetWorkingDir() string {
|
| 108 |
+
s.mu.Lock()
|
| 109 |
+
defer s.mu.Unlock()
|
| 110 |
+
return s.cwd
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// SetWorkingDir sets the working directory
|
| 114 |
+
func (s *Shell) SetWorkingDir(dir string) error {
|
| 115 |
+
s.mu.Lock()
|
| 116 |
+
defer s.mu.Unlock()
|
| 117 |
+
|
| 118 |
+
// Verify the directory exists
|
| 119 |
+
if _, err := os.Stat(dir); err != nil {
|
| 120 |
+
return fmt.Errorf("directory does not exist: %w", err)
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
s.cwd = dir
|
| 124 |
+
return nil
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// GetEnv returns a copy of the environment variables
|
| 128 |
+
func (s *Shell) GetEnv() []string {
|
| 129 |
+
s.mu.Lock()
|
| 130 |
+
defer s.mu.Unlock()
|
| 131 |
+
|
| 132 |
+
env := make([]string, len(s.env))
|
| 133 |
+
copy(env, s.env)
|
| 134 |
+
return env
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
// SetEnv sets an environment variable
|
| 138 |
+
func (s *Shell) SetEnv(key, value string) {
|
| 139 |
+
s.mu.Lock()
|
| 140 |
+
defer s.mu.Unlock()
|
| 141 |
+
|
| 142 |
+
// Update or add the environment variable
|
| 143 |
+
keyPrefix := key + "="
|
| 144 |
+
for i, env := range s.env {
|
| 145 |
+
if strings.HasPrefix(env, keyPrefix) {
|
| 146 |
+
s.env[i] = keyPrefix + value
|
| 147 |
+
return
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
s.env = append(s.env, keyPrefix+value)
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
// SetBlockFuncs sets the command block functions for the shell
|
| 154 |
+
func (s *Shell) SetBlockFuncs(blockFuncs []BlockFunc) {
|
| 155 |
+
s.mu.Lock()
|
| 156 |
+
defer s.mu.Unlock()
|
| 157 |
+
s.blockFuncs = blockFuncs
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
// CommandsBlocker creates a BlockFunc that blocks exact command matches
|
| 161 |
+
func CommandsBlocker(cmds []string) BlockFunc {
|
| 162 |
+
bannedSet := make(map[string]struct{})
|
| 163 |
+
for _, cmd := range cmds {
|
| 164 |
+
bannedSet[cmd] = struct{}{}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
return func(args []string) bool {
|
| 168 |
+
if len(args) == 0 {
|
| 169 |
+
return false
|
| 170 |
+
}
|
| 171 |
+
_, ok := bannedSet[args[0]]
|
| 172 |
+
return ok
|
| 173 |
+
}
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
// ArgumentsBlocker creates a BlockFunc that blocks specific subcommand
|
| 177 |
+
func ArgumentsBlocker(cmd string, args []string, flags []string) BlockFunc {
|
| 178 |
+
return func(parts []string) bool {
|
| 179 |
+
if len(parts) == 0 || parts[0] != cmd {
|
| 180 |
+
return false
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
argParts, flagParts := splitArgsFlags(parts[1:])
|
| 184 |
+
if len(argParts) < len(args) || len(flagParts) < len(flags) {
|
| 185 |
+
return false
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
argsMatch := slices.Equal(argParts[:len(args)], args)
|
| 189 |
+
flagsMatch := slice.IsSubset(flags, flagParts)
|
| 190 |
+
|
| 191 |
+
return argsMatch && flagsMatch
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
func splitArgsFlags(parts []string) (args []string, flags []string) {
|
| 196 |
+
args = make([]string, 0, len(parts))
|
| 197 |
+
flags = make([]string, 0, len(parts))
|
| 198 |
+
for _, part := range parts {
|
| 199 |
+
if strings.HasPrefix(part, "-") {
|
| 200 |
+
// Extract flag name before '=' if present
|
| 201 |
+
flag := part
|
| 202 |
+
if idx := strings.IndexByte(part, '='); idx != -1 {
|
| 203 |
+
flag = part[:idx]
|
| 204 |
+
}
|
| 205 |
+
flags = append(flags, flag)
|
| 206 |
+
} else {
|
| 207 |
+
args = append(args, part)
|
| 208 |
+
}
|
| 209 |
+
}
|
| 210 |
+
return
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
func (s *Shell) blockHandler() func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
|
| 214 |
+
return func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
|
| 215 |
+
return func(ctx context.Context, args []string) error {
|
| 216 |
+
if len(args) == 0 {
|
| 217 |
+
return next(ctx, args)
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
for _, blockFunc := range s.blockFuncs {
|
| 221 |
+
if blockFunc(args) {
|
| 222 |
+
return fmt.Errorf("command is not allowed for security reasons: %s", strings.Join(args, " "))
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
return next(ctx, args)
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
// execPOSIX executes commands using POSIX shell emulation (cross-platform)
|
| 232 |
+
func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string, error) {
|
| 233 |
+
line, err := syntax.NewParser().Parse(strings.NewReader(command), "")
|
| 234 |
+
if err != nil {
|
| 235 |
+
return "", "", fmt.Errorf("could not parse command: %w", err)
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
var stdout, stderr bytes.Buffer
|
| 239 |
+
runner, err := interp.New(
|
| 240 |
+
interp.StdIO(nil, &stdout, &stderr),
|
| 241 |
+
interp.Interactive(false),
|
| 242 |
+
interp.Env(expand.ListEnviron(s.env...)),
|
| 243 |
+
interp.Dir(s.cwd),
|
| 244 |
+
interp.ExecHandlers(s.blockHandler(), coreutils.ExecHandler),
|
| 245 |
+
)
|
| 246 |
+
if err != nil {
|
| 247 |
+
return "", "", fmt.Errorf("could not run command: %w", err)
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
err = runner.Run(ctx, line)
|
| 251 |
+
s.cwd = runner.Dir
|
| 252 |
+
s.env = []string{}
|
| 253 |
+
for name, vr := range runner.Vars {
|
| 254 |
+
s.env = append(s.env, fmt.Sprintf("%s=%s", name, vr.Str))
|
| 255 |
+
}
|
| 256 |
+
s.logger.InfoPersist("POSIX command finished", "command", command, "err", err)
|
| 257 |
+
return stdout.String(), stderr.String(), err
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
// IsInterrupt checks if an error is due to interruption
|
| 261 |
+
func IsInterrupt(err error) bool {
|
| 262 |
+
return errors.Is(err, context.Canceled) ||
|
| 263 |
+
errors.Is(err, context.DeadlineExceeded)
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
// ExitCode extracts the exit code from an error
|
| 267 |
+
func ExitCode(err error) int {
|
| 268 |
+
if err == nil {
|
| 269 |
+
return 0
|
| 270 |
+
}
|
| 271 |
+
var exitErr interp.ExitStatus
|
| 272 |
+
if errors.As(err, &exitErr) {
|
| 273 |
+
return int(exitErr)
|
| 274 |
+
}
|
| 275 |
+
return 1
|
| 276 |
+
}
|
projects/ui/crush/internal/shell/shell_test.go
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package shell
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"path/filepath"
|
| 6 |
+
"runtime"
|
| 7 |
+
"strings"
|
| 8 |
+
"testing"
|
| 9 |
+
"time"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
// Benchmark to measure CPU efficiency
|
| 13 |
+
func BenchmarkShellQuickCommands(b *testing.B) {
|
| 14 |
+
shell := NewShell(&Options{WorkingDir: b.TempDir()})
|
| 15 |
+
|
| 16 |
+
b.ReportAllocs()
|
| 17 |
+
|
| 18 |
+
for b.Loop() {
|
| 19 |
+
_, _, err := shell.Exec(context.Background(), "echo test")
|
| 20 |
+
exitCode := ExitCode(err)
|
| 21 |
+
if err != nil || exitCode != 0 {
|
| 22 |
+
b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
func TestTestTimeout(t *testing.T) {
|
| 28 |
+
// XXX(@andreynering): This fails on Windows. Address once possible.
|
| 29 |
+
if runtime.GOOS == "windows" {
|
| 30 |
+
t.Skip("Skipping test on Windows")
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
|
| 34 |
+
t.Cleanup(cancel)
|
| 35 |
+
|
| 36 |
+
shell := NewShell(&Options{WorkingDir: t.TempDir()})
|
| 37 |
+
_, _, err := shell.Exec(ctx, "sleep 10")
|
| 38 |
+
if status := ExitCode(err); status == 0 {
|
| 39 |
+
t.Fatalf("Expected non-zero exit status, got %d", status)
|
| 40 |
+
}
|
| 41 |
+
if !IsInterrupt(err) {
|
| 42 |
+
t.Fatalf("Expected command to be interrupted, but it was not")
|
| 43 |
+
}
|
| 44 |
+
if err == nil {
|
| 45 |
+
t.Fatalf("Expected an error due to timeout, but got none")
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
func TestTestCancel(t *testing.T) {
|
| 50 |
+
ctx, cancel := context.WithCancel(t.Context())
|
| 51 |
+
cancel() // immediately cancel the context
|
| 52 |
+
|
| 53 |
+
shell := NewShell(&Options{WorkingDir: t.TempDir()})
|
| 54 |
+
_, _, err := shell.Exec(ctx, "sleep 10")
|
| 55 |
+
if status := ExitCode(err); status == 0 {
|
| 56 |
+
t.Fatalf("Expected non-zero exit status, got %d", status)
|
| 57 |
+
}
|
| 58 |
+
if !IsInterrupt(err) {
|
| 59 |
+
t.Fatalf("Expected command to be interrupted, but it was not")
|
| 60 |
+
}
|
| 61 |
+
if err == nil {
|
| 62 |
+
t.Fatalf("Expected an error due to cancel, but got none")
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
func TestRunCommandError(t *testing.T) {
|
| 67 |
+
shell := NewShell(&Options{WorkingDir: t.TempDir()})
|
| 68 |
+
_, _, err := shell.Exec(t.Context(), "nopenopenope")
|
| 69 |
+
if status := ExitCode(err); status == 0 {
|
| 70 |
+
t.Fatalf("Expected non-zero exit status, got %d", status)
|
| 71 |
+
}
|
| 72 |
+
if IsInterrupt(err) {
|
| 73 |
+
t.Fatalf("Expected command to not be interrupted, but it was")
|
| 74 |
+
}
|
| 75 |
+
if err == nil {
|
| 76 |
+
t.Fatalf("Expected an error, got nil")
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
func TestRunContinuity(t *testing.T) {
|
| 81 |
+
tempDir1 := t.TempDir()
|
| 82 |
+
tempDir2 := t.TempDir()
|
| 83 |
+
|
| 84 |
+
shell := NewShell(&Options{WorkingDir: tempDir1})
|
| 85 |
+
if _, _, err := shell.Exec(t.Context(), "export FOO=bar"); err != nil {
|
| 86 |
+
t.Fatalf("failed to set env: %v", err)
|
| 87 |
+
}
|
| 88 |
+
if _, _, err := shell.Exec(t.Context(), "cd "+filepath.ToSlash(tempDir2)); err != nil {
|
| 89 |
+
t.Fatalf("failed to change directory: %v", err)
|
| 90 |
+
}
|
| 91 |
+
out, _, err := shell.Exec(t.Context(), "echo $FOO ; pwd")
|
| 92 |
+
if err != nil {
|
| 93 |
+
t.Fatalf("failed to echo: %v", err)
|
| 94 |
+
}
|
| 95 |
+
expect := "bar\n" + tempDir2 + "\n"
|
| 96 |
+
if out != expect {
|
| 97 |
+
t.Fatalf("expected output %q, got %q", expect, out)
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
func TestCrossPlatformExecution(t *testing.T) {
|
| 102 |
+
shell := NewShell(&Options{WorkingDir: "."})
|
| 103 |
+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
| 104 |
+
defer cancel()
|
| 105 |
+
|
| 106 |
+
// Test a simple command that should work on all platforms
|
| 107 |
+
stdout, stderr, err := shell.Exec(ctx, "echo hello")
|
| 108 |
+
if err != nil {
|
| 109 |
+
t.Fatalf("Echo command failed: %v, stderr: %s", err, stderr)
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
if stdout == "" {
|
| 113 |
+
t.Error("Echo command produced no output")
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// The output should contain "hello" regardless of platform
|
| 117 |
+
if !strings.Contains(strings.ToLower(stdout), "hello") {
|
| 118 |
+
t.Errorf("Echo output should contain 'hello', got: %q", stdout)
|
| 119 |
+
}
|
| 120 |
+
}
|