Compare commits

..

14 Commits

Author SHA1 Message Date
22ea09f0c1 fix(contrib/drivers/pgsql): Fixed table field call issue in primary key acquisition logic (#4546)
`pgsql driver`中`getPrimaryKeys`未使用现有缓存,导致每次`insert`都会重新查询表字段
2026-05-18 20:36:38 +00:00
4080452ead fix(contrib/drivers/pgsql): Fixed table field call issue in primary key acquisition logic (#4546)
`pgsql driver`中`getPrimaryKeys`未使用现有缓存,导致每次`insert`都会重新查询表字段
2025-12-08 16:27:17 +08:00
67a8a28a18 Update contrib/drivers/pgsql/pgsql_do_insert.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-08 15:01:01 +08:00
d8fa0a7922 Update contrib/drivers/pgsql/pgsql_do_insert.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-08 15:00:52 +08:00
b7cd39a8b8 up 2025-12-08 14:56:21 +08:00
01cd4a3384 up 2025-12-08 14:55:19 +08:00
111f8b3264 Update contrib/drivers/pgsql/pgsql_z_unit_upsert_test.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-08 14:54:48 +08:00
ba44475765 Update contrib/drivers/pgsql/pgsql_do_insert.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-08 14:54:26 +08:00
99536c8bef up 2025-12-08 14:37:35 +08:00
91e3f1eab1 Merge branch 'master' of github.com:gogf/gf into feat/gdb-pgsql-replace 2025-12-08 11:40:45 +08:00
ffe65d9d4a merge master 2025-12-04 20:41:28 +08:00
8723999afc up 2025-12-04 20:33:08 +08:00
0d122d6fee up 2025-12-04 17:33:57 +08:00
63c2bb7c86 feat(contrib/drivers/dm): add Replace/InsertIgnore support for dm 2025-12-04 17:29:39 +08:00
338 changed files with 2304 additions and 23251 deletions

View File

@ -198,17 +198,6 @@ jobs:
ports:
- 5236:5236
# openGauss server
# docker run --privileged=true -e GS_PASSWORD=UTpass@1234 -p 9950:5432 opengauss/opengauss:7.0.0-RC1.B023
gaussdb:
image: opengauss/opengauss:7.0.0-RC1.B023
env:
GS_PASSWORD: UTpass@1234
TZ: Asia/Shanghai
ports:
- 9950:5432
zookeeper:
image: zookeeper:3.8
ports:

View File

@ -1,785 +0,0 @@
#!/usr/bin/env bash
#
# GoFrame Docker Services Manager
# For managing Docker services used in local development and testing
#
set -e
# Container name prefix
PREFIX="goframe"
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Service definitions
declare -A SERVICES
declare -A SERVICE_PORTS
declare -A SERVICE_ENVS
declare -A SERVICE_OPTS
# Basic services
SERVICES["etcd"]="bitnamilegacy/etcd:3.4.24"
SERVICE_PORTS["etcd"]="2379:2379"
SERVICE_ENVS["etcd"]="-e ALLOW_NONE_AUTHENTICATION=yes"
SERVICES["redis"]="redis:7.0"
SERVICE_PORTS["redis"]="6379:6379"
SERVICE_OPTS["redis"]="--health-cmd 'redis-cli ping' --health-interval 10s --health-timeout 5s --health-retries 5"
SERVICES["mysql"]="mysql:5.7"
SERVICE_PORTS["mysql"]="3306:3306"
SERVICE_ENVS["mysql"]="-e MYSQL_DATABASE=test -e MYSQL_ROOT_PASSWORD=12345678"
SERVICES["mariadb"]="mariadb:11.4"
SERVICE_PORTS["mariadb"]="3307:3306"
SERVICE_ENVS["mariadb"]="-e MARIADB_DATABASE=test -e MARIADB_ROOT_PASSWORD=12345678"
SERVICES["postgres"]="postgres:17-alpine"
SERVICE_PORTS["postgres"]="5432:5432"
SERVICE_ENVS["postgres"]="-e POSTGRES_PASSWORD=12345678 -e POSTGRES_USER=postgres -e POSTGRES_DB=test -e TZ=Asia/Shanghai"
SERVICE_OPTS["postgres"]="--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5"
SERVICES["mssql"]="mcr.microsoft.com/mssql/server:2022-latest"
SERVICE_PORTS["mssql"]="1433:1433"
SERVICE_ENVS["mssql"]="-e TZ=Asia/Shanghai -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD=LoremIpsum86"
SERVICES["clickhouse"]="clickhouse/clickhouse-server:24.11.1.2557-alpine"
SERVICE_PORTS["clickhouse"]="9000:9000 -p 8123:8123 -p 9001:9001"
SERVICES["polaris"]="polarismesh/polaris-standalone:v1.17.2"
SERVICE_PORTS["polaris"]="8090:8090 -p 8091:8091 -p 8093:8093 -p 9090:9090 -p 9091:9091"
SERVICES["oracle"]="loads/oracle-xe-11g-r2:11.2.0"
SERVICE_PORTS["oracle"]="1521:1521"
SERVICE_ENVS["oracle"]="-e ORACLE_ALLOW_REMOTE=true -e ORACLE_SID=XE -e ORACLE_DB_USER_NAME=system -e ORACLE_DB_PASSWORD=oracle"
SERVICES["dm"]="loads/dm:v8.1.2.128_ent_x86_64_ctm_pack4"
SERVICE_PORTS["dm"]="5236:5236"
SERVICES["gaussdb"]="opengauss/opengauss:7.0.0-RC1.B023"
SERVICE_PORTS["gaussdb"]="9950:5432"
SERVICE_ENVS["gaussdb"]="-e GS_PASSWORD=UTpass@1234 -e TZ=Asia/Shanghai"
SERVICE_OPTS["gaussdb"]="--privileged=true"
SERVICES["zookeeper"]="zookeeper:3.8"
SERVICE_PORTS["zookeeper"]="2181:2181"
# Service groups
GROUP_DB="mysql mariadb postgres mssql oracle dm gaussdb clickhouse"
GROUP_CACHE="redis etcd"
GROUP_REGISTRY="polaris zookeeper"
GROUP_ALL="etcd redis mysql mariadb postgres mssql clickhouse polaris oracle dm gaussdb zookeeper"
# Working directories
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
WORKFLOW_DIR="$PROJECT_ROOT/.github/workflows"
# Print colored messages
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[OK]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if Docker is available
check_docker() {
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed or not in PATH"
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker service is not running"
exit 1
fi
}
# Get container name
get_container_name() {
echo "${PREFIX}-$1"
}
# Start a single service
start_service() {
local service=$1
local container_name=$(get_container_name "$service")
local image="${SERVICES[$service]}"
local ports="${SERVICE_PORTS[$service]}"
local envs="${SERVICE_ENVS[$service]}"
local opts="${SERVICE_OPTS[$service]}"
if [ -z "$image" ]; then
print_error "Unknown service: $service"
return 1
fi
# Check if container already exists
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
if docker ps --format '{{.Names}}' | grep -q "^${container_name}$"; then
print_warning "$service is already running"
return 0
else
print_info "Starting existing container $service..."
docker start "$container_name" > /dev/null
print_success "$service started"
return 0
fi
fi
print_info "Starting $service..."
# Build docker run command
local cmd="docker run -d --name $container_name"
# Add port mappings
for port in $ports; do
cmd="$cmd -p $port"
done
# Add environment variables
if [ -n "$envs" ]; then
cmd="$cmd $envs"
fi
# Add other options
if [ -n "$opts" ]; then
cmd="$cmd $opts"
fi
cmd="$cmd $image"
if eval "$cmd" > /dev/null 2>&1; then
print_success "$service started (container: $container_name)"
else
print_error "Failed to start $service"
return 1
fi
}
# Stop a single service
stop_service() {
local service=$1
local container_name=$(get_container_name "$service")
if docker ps --format '{{.Names}}' | grep -q "^${container_name}$"; then
print_info "Stopping $service..."
docker stop "$container_name" > /dev/null
print_success "$service stopped"
else
print_warning "$service is not running"
fi
}
# Remove a single service
remove_service() {
local service=$1
local container_name=$(get_container_name "$service")
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
print_info "Removing $service..."
docker rm -f "$container_name" > /dev/null
print_success "$service removed"
else
print_warning "$service container does not exist"
fi
}
# View service logs
logs_service() {
local service=$1
local container_name=$(get_container_name "$service")
local lines=${2:-100}
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
docker logs --tail "$lines" -f "$container_name"
else
print_error "$service container does not exist"
return 1
fi
}
# Start docker-compose service
start_compose_service() {
local service=$1
local compose_file=""
case $service in
apollo)
compose_file="$WORKFLOW_DIR/apollo/docker-compose.yml"
;;
nacos)
compose_file="$WORKFLOW_DIR/nacos/docker-compose.yml"
;;
redis-cluster)
compose_file="$WORKFLOW_DIR/redis/docker-compose.yml"
;;
consul)
compose_file="$WORKFLOW_DIR/consul/docker-compose.yml"
;;
*)
print_error "Unknown compose service: $service"
return 1
;;
esac
if [ -f "$compose_file" ]; then
print_info "Starting $service (docker-compose)..."
docker compose -f "$compose_file" up -d
print_success "$service started"
else
print_error "Compose file does not exist: $compose_file"
return 1
fi
}
# Stop docker-compose service
stop_compose_service() {
local service=$1
local compose_file=""
case $service in
apollo)
compose_file="$WORKFLOW_DIR/apollo/docker-compose.yml"
;;
nacos)
compose_file="$WORKFLOW_DIR/nacos/docker-compose.yml"
;;
redis-cluster)
compose_file="$WORKFLOW_DIR/redis/docker-compose.yml"
;;
consul)
compose_file="$WORKFLOW_DIR/consul/docker-compose.yml"
;;
*)
print_error "Unknown compose service: $service"
return 1
;;
esac
if [ -f "$compose_file" ]; then
print_info "Stopping $service (docker-compose)..."
docker compose -f "$compose_file" down
print_success "$service stopped"
else
print_error "Compose file does not exist: $compose_file"
return 1
fi
}
# Show service status
show_status() {
echo ""
echo -e "${CYAN}========== GoFrame Docker Services Status ==========${NC}"
echo ""
printf "%-15s %-12s %-30s %s\n" "SERVICE" "STATUS" "CONTAINER" "PORTS"
echo "--------------------------------------------------------------------------------"
for service in $GROUP_ALL; do
local container_name=$(get_container_name "$service")
local status="stopped"
local ports="-"
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^${container_name}$"; then
status="${GREEN}running${NC}"
ports=$(docker port "$container_name" 2>/dev/null | tr '\n' ' ' || echo "-")
elif docker ps -a --format '{{.Names}}' 2>/dev/null | grep -q "^${container_name}$"; then
status="${YELLOW}stopped${NC}"
else
status="${RED}not created${NC}"
fi
printf "%-15s %-22b %-30s %s\n" "$service" "$status" "$container_name" "$ports"
done
echo ""
echo -e "${CYAN}========== Compose Services ==========${NC}"
echo ""
for compose_svc in apollo nacos redis-cluster consul; do
local running=0
case $compose_svc in
apollo)
running=$(docker ps --filter "name=apollo" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
nacos)
running=$(docker ps --filter "name=nacos" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
redis-cluster)
running=$(docker ps --filter "name=redis-" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
consul)
running=$(docker ps --filter "name=consul" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
esac
if [ "$running" -gt 0 ]; then
printf "%-15s ${GREEN}running${NC} (%d containers)\n" "$compose_svc" "$running"
else
printf "%-15s ${RED}stopped${NC}\n" "$compose_svc"
fi
done
echo ""
}
# Show service information
show_service_info() {
echo ""
echo -e "${CYAN}========== Available Services ==========${NC}"
echo ""
echo -e "${YELLOW}Basic Services (standalone containers):${NC}"
echo ""
printf "%-15s %-50s %s\n" "SERVICE" "IMAGE" "PORTS"
echo "--------------------------------------------------------------------------------"
for service in $GROUP_ALL; do
printf "%-15s %-50s %s\n" "$service" "${SERVICES[$service]}" "${SERVICE_PORTS[$service]}"
done
echo ""
echo -e "${YELLOW}Compose Services (multi-container):${NC}"
echo " apollo - Apollo Config Center (8080, 8070, 8060, 13306)"
echo " nacos - Nacos Registry (8848, 9848, 9555)"
echo " redis-cluster - Redis Primary-Replica + Sentinel Cluster (6380-6382, 26379-26381)"
echo " consul - Consul Service Discovery (8500, 8600)"
echo ""
echo -e "${YELLOW}Service Groups:${NC}"
echo " db - Databases: $GROUP_DB"
echo " cache - Cache: $GROUP_CACHE"
echo " registry - Registry: $GROUP_REGISTRY"
echo " all - All basic services"
echo ""
}
# Show help
show_help() {
echo ""
echo -e "${CYAN}GoFrame Docker Services Manager${NC}"
echo ""
echo "Usage: $0 <command> [service|group] [options]"
echo ""
echo "Commands:"
echo " start <service|group> Start service or service group"
echo " stop <service|group> Stop service or service group"
echo " restart <service|group> Restart service or service group"
echo " remove <service|group> Remove service container"
echo " logs <service> [lines] View service logs (default 100 lines)"
echo " status Show all service status"
echo " info Show available service information"
echo " clean Remove all goframe containers"
echo " pull [service] Pull images"
echo ""
echo "Services:"
echo " Basic: etcd, redis, mysql, mariadb, postgres, mssql,"
echo " clickhouse, polaris, oracle, dm, gaussdb, zookeeper"
echo " Compose: apollo, nacos, redis-cluster, consul"
echo ""
echo "Service Groups:"
echo " db - All database services"
echo " cache - Cache services (redis, etcd)"
echo " registry - Registry services (polaris, zookeeper)"
echo " all - All basic services"
echo ""
echo "Examples:"
echo " $0 start mysql # Start MySQL"
echo " $0 start db # Start all databases"
echo " $0 start all # Start all basic services"
echo " $0 start apollo # Start Apollo (compose)"
echo " $0 stop all # Stop all basic services"
echo " $0 logs mysql 50 # View last 50 lines of MySQL logs"
echo " $0 status # View service status"
echo ""
}
# Parse service groups
parse_services() {
local input=$1
case $input in
db)
echo "$GROUP_DB"
;;
cache)
echo "$GROUP_CACHE"
;;
registry)
echo "$GROUP_REGISTRY"
;;
all)
echo "$GROUP_ALL"
;;
*)
echo "$input"
;;
esac
}
# Check if it's a compose service
is_compose_service() {
local service=$1
case $service in
apollo|nacos|redis-cluster|consul)
return 0
;;
*)
return 1
;;
esac
}
# Pull images
pull_images() {
local services=$1
if [ -z "$services" ]; then
services="$GROUP_ALL"
fi
for service in $services; do
if [ -n "${SERVICES[$service]}" ]; then
print_info "Pulling image: ${SERVICES[$service]}"
docker pull "${SERVICES[$service]}"
fi
done
}
# Clean all goframe containers
clean_all() {
print_info "Removing all $PREFIX containers..."
local containers=$(docker ps -a --filter "name=$PREFIX" --format '{{.Names}}')
if [ -n "$containers" ]; then
for container in $containers; do
docker rm -f "$container" > /dev/null
print_success "Removed: $container"
done
else
print_info "No $PREFIX containers found"
fi
}
# Get service status mark
get_service_status_mark() {
local service=$1
local container_name=$(get_container_name "$service")
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q "^${container_name}$"; then
echo -e "${GREEN}*${NC}"
else
echo " "
fi
}
# Get compose service status mark
get_compose_status_mark() {
local service=$1
local running=0
case $service in
apollo)
running=$(docker ps --filter "name=apollo" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
nacos)
running=$(docker ps --filter "name=nacos" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
redis-cluster)
running=$(docker ps --filter "name=redis-" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
consul)
running=$(docker ps --filter "name=consul" --format '{{.Names}}' 2>/dev/null | wc -l)
;;
esac
if [ "$running" -gt 0 ]; then
echo -e "${GREEN}*${NC}"
else
echo " "
fi
}
# Service selection menu
select_service_menu() {
local action=$1
local action_name=$2
echo ""
echo -e "${CYAN}========== Select Service to ${action_name} ==========${NC}"
# Show running status for stop/restart/logs operations
if [[ "$action" == "stop" || "$action" == "restart" || "$action" == "logs" ]]; then
echo -e " (${GREEN}*${NC} indicates running)"
fi
echo ""
echo -e "${YELLOW}Basic Services:${NC}"
printf " %b1) etcd %b2) redis %b3) mysql\n" \
"$(get_service_status_mark etcd)" "$(get_service_status_mark redis)" "$(get_service_status_mark mysql)"
printf " %b4) mariadb %b5) postgres %b6) mssql\n" \
"$(get_service_status_mark mariadb)" "$(get_service_status_mark postgres)" "$(get_service_status_mark mssql)"
printf " %b7) clickhouse %b8) polaris %b9) oracle\n" \
"$(get_service_status_mark clickhouse)" "$(get_service_status_mark polaris)" "$(get_service_status_mark oracle)"
printf " %b10) dm %b11) gaussdb %b12) zookeeper\n" \
"$(get_service_status_mark dm)" "$(get_service_status_mark gaussdb)" "$(get_service_status_mark zookeeper)"
echo ""
echo -e "${YELLOW}Compose Services:${NC}"
printf " %b13) apollo %b14) nacos %b15) redis-cluster\n" \
"$(get_compose_status_mark apollo)" "$(get_compose_status_mark nacos)" "$(get_compose_status_mark redis-cluster)"
printf " %b16) consul\n" "$(get_compose_status_mark consul)"
echo ""
echo -e "${YELLOW}Service Groups:${NC}"
echo " 17) db (all databases) 18) cache (cache services)"
echo " 19) registry (registry services) 20) all (all basic services)"
echo ""
echo " 0) Back to main menu"
echo ""
read -p "Select [0-20]: " svc_choice
local svc=""
case $svc_choice in
1) svc="etcd" ;;
2) svc="redis" ;;
3) svc="mysql" ;;
4) svc="mariadb" ;;
5) svc="postgres" ;;
6) svc="mssql" ;;
7) svc="clickhouse" ;;
8) svc="polaris" ;;
9) svc="oracle" ;;
10) svc="dm" ;;
11) svc="gaussdb" ;;
12) svc="zookeeper" ;;
13) svc="apollo" ;;
14) svc="nacos" ;;
15) svc="redis-cluster" ;;
16) svc="consul" ;;
17) svc="db" ;;
18) svc="cache" ;;
19) svc="registry" ;;
20) svc="all" ;;
0) return ;;
*)
print_error "Invalid selection"
return
;;
esac
case $action in
start)
if is_compose_service "$svc"; then
start_compose_service "$svc"
else
for s in $(parse_services "$svc"); do
start_service "$s"
done
fi
;;
stop)
if is_compose_service "$svc"; then
stop_compose_service "$svc"
else
for s in $(parse_services "$svc"); do
stop_service "$s"
done
fi
;;
restart)
if is_compose_service "$svc"; then
stop_compose_service "$svc"
start_compose_service "$svc"
else
for s in $(parse_services "$svc"); do
stop_service "$s"
start_service "$s"
done
fi
;;
remove)
for s in $(parse_services "$svc"); do
remove_service "$s"
done
;;
logs)
if is_compose_service "$svc"; then
print_error "For Compose services, please use 'docker compose logs'"
else
read -p "Number of lines (default 100): " lines
lines=${lines:-100}
logs_service "$svc" "$lines"
fi
;;
pull)
pull_images "$(parse_services "$svc")"
;;
esac
}
# Interactive menu
interactive_menu() {
while true; do
echo ""
echo -e "${CYAN}========== GoFrame Docker Services Manager ==========${NC}"
echo ""
echo " 1) Start Service"
echo " 2) Stop Service"
echo " 3) Restart Service"
echo " 4) Remove Service"
echo " 5) View Logs"
echo " 6) View Status"
echo " 7) Service Info"
echo " 8) Clean All Containers"
echo " 9) Pull Images"
echo " 0) Exit"
echo ""
read -p "Select operation [0-9]: " choice
case $choice in
1)
select_service_menu "start" "Start"
;;
2)
select_service_menu "stop" "Stop"
;;
3)
select_service_menu "restart" "Restart"
;;
4)
select_service_menu "remove" "Remove"
;;
5)
select_service_menu "logs" "View Logs"
;;
6)
show_status
;;
7)
show_service_info
;;
8)
read -p "Confirm removing all goframe containers? [y/N]: " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
clean_all
fi
;;
9)
select_service_menu "pull" "Pull Images"
;;
0)
echo "Goodbye!"
exit 0
;;
*)
print_error "Invalid selection"
;;
esac
done
}
# Main function
main() {
check_docker
if [ $# -eq 0 ]; then
interactive_menu
exit 0
fi
local command=$1
local target=$2
local extra=$3
case $command in
start)
if [ -z "$target" ]; then
print_error "Please specify service name or service group"
exit 1
fi
if is_compose_service "$target"; then
start_compose_service "$target"
else
for service in $(parse_services "$target"); do
start_service "$service"
done
fi
;;
stop)
if [ -z "$target" ]; then
print_error "Please specify service name or service group"
exit 1
fi
if is_compose_service "$target"; then
stop_compose_service "$target"
else
for service in $(parse_services "$target"); do
stop_service "$service"
done
fi
;;
restart)
if [ -z "$target" ]; then
print_error "Please specify service name or service group"
exit 1
fi
if is_compose_service "$target"; then
stop_compose_service "$target"
start_compose_service "$target"
else
for service in $(parse_services "$target"); do
stop_service "$service"
start_service "$service"
done
fi
;;
remove|rm)
if [ -z "$target" ]; then
print_error "Please specify service name or service group"
exit 1
fi
for service in $(parse_services "$target"); do
remove_service "$service"
done
;;
logs)
if [ -z "$target" ]; then
print_error "Please specify service name"
exit 1
fi
logs_service "$target" "${extra:-100}"
;;
status|ps)
show_status
;;
info|list)
show_service_info
;;
clean)
clean_all
;;
pull)
pull_images "$target"
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $command"
show_help
exit 1
;;
esac
}
main "$@"

3
.gitignore vendored
View File

@ -24,5 +24,4 @@ node_modules
.docusaurus
output
.example/
.golangci.bck.yml
*.exe
.golangci.bck.yml

3
.gitmodules vendored
View File

@ -0,0 +1,3 @@
[submodule "examples"]
path = examples
url = git@github.com:gogf/examples.git

View File

@ -6,7 +6,6 @@ tidy:
./.make_tidy.sh
# execute "golangci-lint" to check code style
# go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest
.PHONY: lint
lint:
golangci-lint run -c .golangci.yml
@ -76,13 +75,3 @@ subsync: subup
git push origin; \
fi; \
cd ..;
# manage docker services for local development
# usage: make docker or make docker cmd=start svc=mysql
.PHONY: docker
docker:
@if [ -z "$(cmd)" ]; then \
./.github/workflows/scripts/docker-services.sh; \
else \
./.github/workflows/scripts/docker-services.sh $(cmd) $(svc) $(extra); \
fi

View File

@ -24,12 +24,6 @@ English | [简体中文](README.zh_CN.MD)
A powerful framework for faster, easier, and more efficient project development.
## Installation
```bash
go get -u github.com/gogf/gf/v2
```
## Documentation
- Official Site: [https://goframe.org](https://goframe.org)
@ -38,14 +32,13 @@ go get -u github.com/gogf/gf/v2
- Mirror Site: [Github Pages](https://pages.goframe.org)
- Mirror Site: [Offline Docs](https://github.com/gogf/goframe.org-pdf?tab=readme-ov-file#%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC)
- GoDoc API: [https://pkg.go.dev/github.com/gogf/gf/v2](https://pkg.go.dev/github.com/gogf/gf/v2)
- Doc Source: [https://github.com/gogf/gf-site](https://github.com/gogf/gf-site)
## Contributors
💖 [Thanks to all the contributors who made GoFrame possible](https://github.com/gogf/gf/graphs/contributors) 💖
<a href="https://github.com/gogf/gf/graphs/contributors">
<img src="https://goframe.org/img/contributors.svg?version=v2.9.8" alt="goframe contributors"/>
<img src="https://goframe.org/img/contributors.svg?version=v2.9.6" alt="goframe contributors"/>
</a>
## License

View File

@ -24,12 +24,6 @@
一个强大的框架,为了更快、更轻松、更高效的项目开发。
## 安装
```bash
go get -u github.com/gogf/gf/v2
```
## 文档
- 官方网站: [https://goframe.org](https://goframe.org)
@ -37,8 +31,7 @@ go get -u github.com/gogf/gf/v2
- 国内镜像: [https://goframe.org.cn](https://goframe.org.cn)
- 镜像网站: [Github Pages](https://pages.goframe.org)
- 镜像网站: [离线文档](https://github.com/gogf/goframe.org-pdf?tab=readme-ov-file#%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC)
- Go包文档: [https://pkg.go.dev/github.com/gogf/gf/v2](https://pkg.go.dev/github.com/gogf/gf/v2)
- 文档源码: [https://github.com/gogf/gf-site](https://github.com/gogf/gf-site)
- GoDoc API: [https://pkg.go.dev/github.com/gogf/gf/v2](https://pkg.go.dev/github.com/gogf/gf/v2)
## 贡献者

View File

@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
go 1.23.0
require (
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.8
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.8
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.8
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.8
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.8
github.com/gogf/gf/v2 v2.9.8
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.6
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.6
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.6
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.6
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.6
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.6
github.com/gogf/gf/v2 v2.9.6
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
github.com/olekukonko/tablewriter v1.1.0
github.com/schollz/progressbar/v3 v3.15.0

View File

@ -46,20 +46,20 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.8 h1:L72OB2HPuZSHtJ2ipBzI+62rGGDRdwYjequ1v+zctpg=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.8/go.mod h1:D0UySg70Bd264F5AScYmz1Hl8vjzlUJ7YvqBJc5OFbo=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.8 h1:DT5zHfo9/VkbJ+TF7kUasvv4dbU5uctoj+JGbrzgdYE=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.8/go.mod h1:cDd91Zd8LxFF+xxOflRRqw0WTTCpAJ0nf0KKRA+nvTE=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8 h1:XZ4Ya/50xpjf81+4genr33iJXR2dxJmqYKxGyXlLRqA=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.8/go.mod h1:wtm2NJb/L3CbDOmyUc7TsOpWHTCMakg1QRG7B/oKrRs=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.8 h1:ZrqABJsUnhNDz8VAem1XXONBTywl6r+GHQH05i+4W1g=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.8/go.mod h1:YTFyeVk2Rgu/JMUhFxkjYzWaBc+yZ6wAvY54XVZoNko=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.8 h1:Dc227FD1uf9nNBPFEjMEgIoAJbAgeYeNrOrjviDgPzY=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.8/go.mod h1:o3EpB4Ti3+x/axzRMJg2k7TrLiWZiSTxP0v64LBkk5k=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.8 h1:LHEhzsBfIo8xHvOUuLDQW1q7Qix1vnBabH/iivCRghs=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.8/go.mod h1:SX6dRONaJGafzCoMIrn8CkRM4fIvtmJRt/aYclUHy3Q=
github.com/gogf/gf/v2 v2.9.8 h1:El0HwksTzeRk0DQV4Lh7S9DbsIwKInhHSHGcH7qJumM=
github.com/gogf/gf/v2 v2.9.8/go.mod h1:Svl1N+E8G/QshU2DUbh/3J/AJauqCgUnxHurXWR4Qx0=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.6 h1:rJzRmA5TGWMeKDebdDosYODoUrMUHqfA5pWO1MBC5b0=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.9.6/go.mod h1:u+bUsuftf8qpKpPZPdOFhzh3F5KQzo6Wqa9JFTCLFqg=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.6 h1:3QTlIbSdrVYvRMNUF6nckspA6Eh5Uy2NqwB3/auxIwk=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.9.6/go.mod h1:oMteYgkWImPpUVe1aqPKtZ8jX1dG3v60lS7IA87MwFQ=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.6 h1:BY1ThxMo0bTx2P18PuCe57ARmjHuEithSdob/CbH/rw=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.6/go.mod h1:v/jKO9JJdLctlPlnUSnnG0SNSEpElM51Qx3KoI5crkU=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.6 h1:12+sWI/hm1D4KxG+1FMZpfoU3PwtSLJ9KbLNa20roLg=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.6/go.mod h1:gjjhgxqjafnORK0F4Fa5W8TJlassw7svKy7RFj5GKss=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.6 h1:LG/bTOJEpyNu6+IdREqFyi6J8LdZIeceeyxhuyV58LQ=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.6/go.mod h1:Ekd5IgUGyBlbfqKD/69hkIL9vHF6F4V2FeEP3h/pH08=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.6 h1:3QZvWIlz3dLjNELQU+5ZZZWuzEx9gsRFLU+qIKVUG6M=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.6/go.mod h1:7EEAe8UYI5dLeuwCWN3HgC62OhjIYbkynaoavw1U/k4=
github.com/gogf/gf/v2 v2.9.6 h1:fQ6uPtS1Ra8qY+OuzPPZTlgksJ4eOXmTZ1/a2l3Idog=
github.com/gogf/gf/v2 v2.9.6/go.mod h1:Svl1N+E8G/QshU2DUbh/3J/AJauqCgUnxHurXWR4Qx0=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f h1:7xfXR/BhG3JDqO1s45n65Oyx9t4E/UqDOXep6jXdLCM=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f/go.mod h1:HnYoio6S7VaFJdryKcD/r9HgX+4QzYfr00XiXUo/xz0=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

View File

@ -14,9 +14,5 @@ replace (
github.com/gogf/gf/contrib/drivers/oracle/v2 => ../../contrib/drivers/oracle
github.com/gogf/gf/contrib/drivers/pgsql/v2 => ../../contrib/drivers/pgsql
github.com/gogf/gf/contrib/drivers/sqlite/v2 => ../../contrib/drivers/sqlite
github.com/gogf/gf/contrib/drivers/mariadb/v2 => ../../contrib/drivers/mariadb
github.com/gogf/gf/contrib/drivers/tidb/v2 => ../../contrib/drivers/tidb
github.com/gogf/gf/contrib/drivers/oceanbase/v2 => ../../contrib/drivers/oceanbase
github.com/gogf/gf/contrib/drivers/gaussdb/v2 => ../../contrib/drivers/gaussdb
github.com/gogf/gf/v2 => ../../
)

View File

@ -37,13 +37,11 @@ type cEnvInput struct {
type cEnvOutput struct{}
func (c cEnv) Index(ctx context.Context, in cEnvInput) (out *cEnvOutput, err error) {
result, execErr := gproc.ShellExec(ctx, "go env")
// Note: go env may return non-zero exit code when there are warnings (e.g., invalid characters in env vars),
// but it still outputs valid environment variables. So we only fail if result is empty.
result, err := gproc.ShellExec(ctx, "go env")
if err != nil {
mlog.Fatal(err)
}
if result == "" {
if execErr != nil {
mlog.Fatal(execErr)
}
mlog.Fatal(`retrieving Golang environment variables failed, did you install Golang?`)
}
var (
@ -61,9 +59,7 @@ func (c cEnv) Index(ctx context.Context, in cEnvInput) (out *cEnvOutput, err err
}
match, _ := gregex.MatchString(`(.+?)=(.*)`, line)
if len(match) < 3 {
// Skip lines that don't match key=value format (e.g., warning messages from go env)
mlog.Debugf(`invalid Golang environment variable: "%s"`, line)
continue
mlog.Fatalf(`invalid Golang environment variable: "%s"`, line)
}
array = append(array, []string{gstr.Trim(match[1]), gstr.Trim(match[2])})
}

View File

@ -7,11 +7,9 @@
package cmd
import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"
"github.com/gogf/gf/v2/frame/g"
@ -22,7 +20,6 @@ import (
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gtag"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/geninit"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/allyes"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/utils"
@ -47,13 +44,6 @@ const (
gf init my-project
gf init my-mono-repo -m
gf init my-mono-repo -a
gf init my-project -u
gf init my-project -g "github.com/myorg/myproject"
gf init -r github.com/gogf/template-single my-project
gf init -r github.com/gogf/examples/httpserver/jwt my-jwt
gf init -r github.com/gogf/gf/cmd/gf/v2@v2.9.7 mygf
gf init -r github.com/gogf/gf/cmd/gf/v2 mygf -s
gf init -i
`
cInitNameBrief = `
name for the project. It will create a folder with NAME in current directory.
@ -65,16 +55,6 @@ The NAME will also be the module name for the project.
cInitGitignore = ".gitignore"
)
// defaultTemplates is the list of predefined templates for interactive selection
var defaultTemplates = []struct {
Name string
Repo string
Desc string
}{
{"template-single", "github.com/gogf/template-single", "Single project template"},
{"template-mono", "github.com/gogf/template-mono", "Mono-repo project template"},
}
func init() {
gtag.Sets(g.MapStrStr{
`cInitBrief`: cInitBrief,
@ -84,86 +64,17 @@ func init() {
}
type cInitInput struct {
g.Meta `name:"init"`
Name string `name:"NAME" arg:"true" brief:"{cInitNameBrief}"`
Mono bool `name:"mono" short:"m" brief:"initialize a mono-repo instead a single-repo" orphan:"true"`
MonoApp bool `name:"monoApp" short:"a" brief:"initialize a mono-repo-app instead a single-repo" orphan:"true"`
Update bool `name:"update" short:"u" brief:"update to the latest goframe version" orphan:"true"`
Module string `name:"module" short:"g" brief:"custom go module"`
Repo string `name:"repo" short:"r" brief:"remote repository URL for template download"`
SelectVer bool `name:"select" short:"s" brief:"enable interactive version selection for remote template" orphan:"true"`
Interactive bool `name:"interactive" short:"i" brief:"enable interactive mode to select template" orphan:"true"`
g.Meta `name:"init"`
Name string `name:"NAME" arg:"true" v:"required" brief:"{cInitNameBrief}"`
Mono bool `name:"mono" short:"m" brief:"initialize a mono-repo instead a single-repo" orphan:"true"`
MonoApp bool `name:"monoApp" short:"a" brief:"initialize a mono-repo-app instead a single-repo" orphan:"true"`
Update bool `name:"update" short:"u" brief:"update to the latest goframe version" orphan:"true"`
Module string `name:"module" short:"g" brief:"custom go module"`
}
type cInitOutput struct{}
func (c cInit) Index(ctx context.Context, in cInitInput) (out *cInitOutput, err error) {
// Check if using remote template mode
if in.Repo != "" || in.Interactive {
return c.initFromRemote(ctx, in)
}
// If no name provided and no remote mode, enter interactive mode
if in.Name == "" {
return c.initInteractive(ctx, in)
}
// Default: use built-in template
return c.initFromBuiltin(ctx, in)
}
// initFromRemote initializes project from remote repository
func (c cInit) initFromRemote(ctx context.Context, in cInitInput) (out *cInitOutput, err error) {
repo := in.Repo
name := in.Name
// If interactive mode and no repo specified, let user select
if in.Interactive && repo == "" {
var modPath string
var upgradeDeps bool
repo, name, modPath, upgradeDeps, err = interactiveSelectTemplate()
if err != nil {
return nil, err
}
if modPath != "" {
in.Module = modPath
}
if upgradeDeps {
in.Update = true
}
}
if repo == "" {
return nil, fmt.Errorf("repository URL is required for remote template mode")
}
// Default name to repo basename if empty
if name == "" {
name = gfile.Basename(repo)
mlog.Printf("Using repository basename as project name: %s", name)
}
mlog.Print("initializing from remote template...")
opts := &geninit.ProcessOptions{
SelectVersion: in.SelectVer,
ModulePath: in.Module,
UpgradeDeps: in.Update,
}
if err = geninit.Process(ctx, repo, name, opts); err != nil {
return nil, err
}
mlog.Print("initialization done!")
if name != "" && name != "." {
mlog.Printf(`you can now run "cd %s && gf run main.go" to start your journey, enjoy!`, name)
}
return
}
// initFromBuiltin initializes project from built-in template
func (c cInit) initFromBuiltin(ctx context.Context, in cInitInput) (out *cInitOutput, err error) {
var overwrote = false
if !gfile.IsEmpty(in.Name) && !allyes.Check() {
s := gcmd.Scanf(`the folder "%s" is not empty, files might be overwrote, continue? [y/n]: `, in.Name)
@ -238,9 +149,6 @@ func (c cInit) initFromBuiltin(ctx context.Context, in cInitInput) (out *cInitOu
return
}
// Format the generated Go files.
utils.GoFmt(in.Name)
// Update the GoFrame version.
if in.Update {
mlog.Print("update goframe...")
@ -272,170 +180,3 @@ func (c cInit) initFromBuiltin(ctx context.Context, in cInitInput) (out *cInitOu
}
return
}
// initInteractive enters interactive mode when no arguments provided
func (c cInit) initInteractive(ctx context.Context, in cInitInput) (out *cInitOutput, err error) {
reader := bufio.NewReader(os.Stdin)
// Ask user which mode to use
fmt.Println("\nPlease select initialization mode:")
fmt.Println(strings.Repeat("-", 50))
fmt.Println(" [1] Built-in template (default)")
fmt.Println(" [2] Remote template")
fmt.Println(strings.Repeat("-", 50))
fmt.Print("Select mode [1-2] (default: 1): ")
input, err := reader.ReadString('\n')
if err != nil {
mlog.Fatalf("failed to read input: %v", err)
return
}
input = strings.TrimSpace(input)
if input == "2" {
in.Interactive = true
return c.initFromRemote(ctx, in)
}
// Built-in template mode
fmt.Println("\nPlease select project type:")
fmt.Println(strings.Repeat("-", 50))
fmt.Println(" [1] Single project (default)")
fmt.Println(" [2] Mono-repo project")
fmt.Println(" [3] Mono-repo app")
fmt.Println(strings.Repeat("-", 50))
fmt.Print("Select type [1-3] (default: 1): ")
input, err = reader.ReadString('\n')
if err != nil {
mlog.Fatalf("failed to read input: %v", err)
return
}
input = strings.TrimSpace(input)
switch input {
case "2":
in.Mono = true
case "3":
in.MonoApp = true
}
// Get project name
for {
fmt.Print("Enter project name: ")
input, err = reader.ReadString('\n')
if err != nil {
mlog.Fatalf("failed to read input: %v", err)
return
}
in.Name = strings.TrimSpace(input)
if in.Name != "" {
break
}
fmt.Println("Project name cannot be empty")
}
// Get module path (optional)
fmt.Printf("Enter Go module path (leave empty to use \"%s\"): ", in.Name)
input, err = reader.ReadString('\n')
if err != nil {
mlog.Fatalf("failed to read input: %v", err)
return
}
in.Module = strings.TrimSpace(input)
// Ask about update
fmt.Print("Update to latest GoFrame version? [y/N]: ")
input, err = reader.ReadString('\n')
if err != nil {
mlog.Fatalf("failed to read input: %v", err)
return
}
input = strings.TrimSpace(strings.ToLower(input))
in.Update = input == "y" || input == "yes"
fmt.Println()
return c.initFromBuiltin(ctx, in)
}
// interactiveSelectTemplate prompts user to select a template interactively
func interactiveSelectTemplate() (repo, name, modPath string, upgradeDeps bool, err error) {
reader := bufio.NewReader(os.Stdin)
// 1. Select template
fmt.Println("\nPlease select a project template:")
fmt.Println(strings.Repeat("-", 50))
for i, t := range defaultTemplates {
fmt.Printf(" [%d] %s - %s\n", i+1, t.Name, t.Desc)
}
fmt.Printf(" [%d] Custom repository URL\n", len(defaultTemplates)+1)
fmt.Println(strings.Repeat("-", 50))
for {
fmt.Printf("Select template [1-%d]: ", len(defaultTemplates)+1)
input, err := reader.ReadString('\n')
if err != nil {
return "", "", "", false, fmt.Errorf("failed to read template selection: %w", err)
}
input = strings.TrimSpace(input)
idx, e := strconv.Atoi(input)
if e != nil || idx < 1 || idx > len(defaultTemplates)+1 {
fmt.Printf("Invalid selection, please enter a number between 1-%d\n", len(defaultTemplates)+1)
continue
}
if idx <= len(defaultTemplates) {
repo = defaultTemplates[idx-1].Repo
fmt.Printf("Selected: %s\n\n", repo)
} else {
// Custom URL
fmt.Print("Enter repository URL: ")
input, err = reader.ReadString('\n')
if err != nil {
return "", "", "", false, fmt.Errorf("failed to read repository URL: %w", err)
}
repo = strings.TrimSpace(input)
if repo == "" {
fmt.Println("Repository URL cannot be empty")
continue
}
}
break
}
// 2. Enter project name
for {
fmt.Print("Enter project name: ")
input, err := reader.ReadString('\n')
if err != nil {
return "", "", "", false, fmt.Errorf("failed to read project name: %w", err)
}
name = strings.TrimSpace(input)
if name == "" {
fmt.Println("Project name cannot be empty")
continue
}
break
}
// 3. Enter module path (optional)
fmt.Printf("Enter Go module path (leave empty to use \"%s\"): ", name)
input, err := reader.ReadString('\n')
if err != nil {
return "", "", "", false, fmt.Errorf("failed to read module path: %w", err)
}
modPath = strings.TrimSpace(input)
// 4. Ask about upgrade
fmt.Print("Upgrade dependencies to latest (go get -u)? [y/N]: ")
input, err = reader.ReadString('\n')
if err != nil {
return "", "", "", false, fmt.Errorf("failed to read upgrade confirmation: %w", err)
}
input = strings.TrimSpace(strings.ToLower(input))
upgradeDeps = input == "y" || input == "yes"
fmt.Println()
return repo, name, modPath, upgradeDeps, nil
}

View File

@ -33,18 +33,12 @@ type cRun struct {
g.Meta `name:"run" usage:"{cRunUsage}" brief:"{cRunBrief}" eg:"{cRunEg}" dc:"{cRunDc}"`
}
type watchPath struct {
Path string
Recursive bool
}
type cRunApp struct {
File string // Go run file name.
Path string // Directory storing built binary.
Options string // Extra "go run" options.
Args string // Custom arguments.
WatchPaths []string // Watch paths for live reload.
IgnorePatterns []string // Custom ignore patterns.
File string // Go run file name.
Path string // Directory storing built binary.
Options string // Extra "go run" options.
Args string // Custom arguments.
WatchPaths []string // Watch paths for live reload.
}
const (
@ -54,47 +48,43 @@ const (
gf run main.go
gf run main.go --args "server -p 8080"
gf run main.go -mod=vendor
gf run main.go -w internal,api
gf run main.go -i ".git,node_modules"
gf run main.go -w "manifest/config/*.yaml"
`
cRunDc = `
The "run" command is used for running go codes with hot-compiled-like feature,
which compiles and runs the go codes asynchronously when codes change.
`
cRunFileBrief = `building file path.`
cRunPathBrief = `output directory path for built binary file. it's "./" in default`
cRunExtraBrief = `the same options as "go run"/"go build" except some options as follows defined`
cRunArgsBrief = `custom arguments for your process`
cRunWatchPathsBrief = `watch additional paths for live reload, separated by ",". i.e. "internal,api"`
cRunIgnorePatternBrief = `custom ignore patterns for watch, separated by ",". i.e. ".git,node_modules". default patterns: node_modules, vendor, .*, _*. Glob syntax: "*" matches any chars, "?" matches single char, "[abc]" matches char class. Note: patterns match directory names only, not paths`
cRunFileBrief = `building file path.`
cRunPathBrief = `output directory path for built binary file. it's "./" in default`
cRunExtraBrief = `the same options as "go run"/"go build" except some options as follows defined`
cRunArgsBrief = `custom arguments for your process`
cRunWatchPathsBrief = `watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"`
)
var process *gproc.Process
func init() {
gtag.Sets(g.MapStrStr{
`cRunUsage`: cRunUsage,
`cRunBrief`: cRunBrief,
`cRunEg`: cRunEg,
`cRunDc`: cRunDc,
`cRunFileBrief`: cRunFileBrief,
`cRunPathBrief`: cRunPathBrief,
`cRunExtraBrief`: cRunExtraBrief,
`cRunArgsBrief`: cRunArgsBrief,
`cRunWatchPathsBrief`: cRunWatchPathsBrief,
`cRunIgnorePatternBrief`: cRunIgnorePatternBrief,
`cRunUsage`: cRunUsage,
`cRunBrief`: cRunBrief,
`cRunEg`: cRunEg,
`cRunDc`: cRunDc,
`cRunFileBrief`: cRunFileBrief,
`cRunPathBrief`: cRunPathBrief,
`cRunExtraBrief`: cRunExtraBrief,
`cRunArgsBrief`: cRunArgsBrief,
`cRunWatchPathsBrief`: cRunWatchPathsBrief,
})
}
type (
cRunInput struct {
g.Meta `name:"run" config:"gfcli.run"`
File string `name:"FILE" arg:"true" brief:"{cRunFileBrief}" v:"required"`
Path string `name:"path" short:"p" brief:"{cRunPathBrief}" d:"./"`
Extra string `name:"extra" short:"e" brief:"{cRunExtraBrief}"`
Args string `name:"args" short:"a" brief:"{cRunArgsBrief}"`
WatchPaths []string `name:"watchPaths" short:"w" brief:"{cRunWatchPathsBrief}"`
IgnorePatterns []string `name:"ignorePatterns" short:"i" brief:"{cRunIgnorePatternBrief}"`
g.Meta `name:"run" config:"gfcli.run"`
File string `name:"FILE" arg:"true" brief:"{cRunFileBrief}" v:"required"`
Path string `name:"path" short:"p" brief:"{cRunPathBrief}" d:"./"`
Extra string `name:"extra" short:"e" brief:"{cRunExtraBrief}"`
Args string `name:"args" short:"a" brief:"{cRunArgsBrief}"`
WatchPaths []string `name:"watchPaths" short:"w" brief:"{cRunWatchPathsBrief}"`
}
cRunOutput struct{}
)
@ -111,25 +101,17 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
mlog.Fatalf(`command "go" not found in your environment, please install golang first to proceed this command`)
}
// Parse comma-separated values in WatchPaths
if len(in.WatchPaths) > 0 {
in.WatchPaths = parseCommaSeparatedArgs(in.WatchPaths)
if len(in.WatchPaths) == 1 {
in.WatchPaths = strings.Split(in.WatchPaths[0], ",")
mlog.Printf("watchPaths: %v", in.WatchPaths)
}
// Parse comma-separated values in IgnorePatterns
if len(in.IgnorePatterns) > 0 {
in.IgnorePatterns = parseCommaSeparatedArgs(in.IgnorePatterns)
mlog.Printf("ignorePatterns: %v", in.IgnorePatterns)
}
app := &cRunApp{
File: in.File,
Path: filepath.FromSlash(in.Path),
Options: in.Extra,
Args: in.Args,
WatchPaths: in.WatchPaths,
IgnorePatterns: in.IgnorePatterns,
File: in.File,
Path: filepath.FromSlash(in.Path),
Options: in.Extra,
Args: in.Args,
WatchPaths: in.WatchPaths,
}
dirty := gtype.NewBool()
@ -139,7 +121,6 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
return
}
// Check if the file extension is 'go'.
if gfile.ExtName(event.Path) != "go" {
return
}
@ -157,11 +138,15 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err
})
}
// Get directories to watch (recursive or non-recursive monitoring).
watchPaths := app.getWatchPaths()
for _, wp := range watchPaths {
option := gfsnotify.WatchOption{NoRecursive: !wp.Recursive}
_, err = gfsnotify.Add(wp.Path, callbackFunc, option)
if len(app.WatchPaths) > 0 {
for _, path := range app.WatchPaths {
_, err = gfsnotify.Add(gfile.RealPath(path), callbackFunc)
if err != nil {
mlog.Fatal(err)
}
}
} else {
_, err = gfsnotify.Add(gfile.RealPath("."), callbackFunc)
if err != nil {
mlog.Fatal(err)
}
@ -264,181 +249,35 @@ func (app *cRunApp) End(ctx context.Context, sig os.Signal, outputPath string) {
}
func (app *cRunApp) genOutputPath() (outputPath string) {
var renamePath string
outputPath = gfile.Join(app.Path, gfile.Name(app.File))
if runtime.GOOS == "windows" {
outputPath += ".exe"
if gfile.Exists(outputPath) {
renamePath := outputPath + "~"
renamePath = outputPath + "~"
if err := gfile.Rename(outputPath, renamePath); err != nil {
mlog.Print(err)
}
// Clean up the renamed old binary file
defer func() {
if gfile.Exists(renamePath) {
_ = gfile.Remove(renamePath)
}
}()
}
}
return filepath.FromSlash(outputPath)
}
// getWatchPaths uses DFS to find the minimal set of directories to watch.
// Rule: if a directory and all its descendants have no ignored subdirectories, watch it;
// otherwise, recurse into valid children and watch the current directory non-recursively.
func (app *cRunApp) getWatchPaths() []watchPath {
roots := []string{"."}
if len(app.WatchPaths) > 0 {
roots = app.WatchPaths
}
// Use custom ignore patterns if provided, otherwise use default.
ignorePatterns := defaultIgnorePatterns
if len(app.IgnorePatterns) > 0 {
ignorePatterns = app.IgnorePatterns
}
var watchPaths []watchPath
for _, root := range roots {
absRoot := gfile.RealPath(root)
if absRoot == "" {
mlog.Printf("watch path '%s' not found, skipping", root)
func matchWatchPaths(watchPaths []string, eventPath string) bool {
for _, path := range watchPaths {
absPath, err := filepath.Abs(path)
if err != nil {
mlog.Printf("match watchPath '%s' error: %s", path, err.Error())
continue
}
if isIgnoredDirName(absRoot, ignorePatterns) {
matched, err := filepath.Match(absPath, eventPath)
if err != nil {
mlog.Printf("match watchPath '%s' error: %s", path, err.Error())
continue
}
app.collectWatchPaths(absRoot, ignorePatterns, &watchPaths)
}
if len(watchPaths) == 0 {
mlog.Printf("no directories to watch, using current directory")
if absCur := gfile.RealPath("."); absCur != "" {
return []watchPath{{Path: absCur, Recursive: true}}
}
return []watchPath{{Path: ".", Recursive: true}}
}
mlog.Printf("watching %d paths", len(watchPaths))
for _, wp := range watchPaths {
recursiveStr := "recursive"
if !wp.Recursive {
recursiveStr = "non-recursive"
}
mlog.Debugf(" - %s (%s)", wp.Path, recursiveStr)
}
return watchPaths
}
// collectWatchPaths performs a DFS traversal to collect the minimal set of directories to watch.
// Returns true if the directory or any of its descendants contains ignored directories.
// Rule: if a directory has no ignored descendants at any depth, watch it recursively;
// otherwise, watch it non-recursively and recurse into valid children.
func (app *cRunApp) collectWatchPaths(dir string, ignorePatterns []string, watchPaths *[]watchPath) bool {
entries, err := gfile.ScanDir(dir, "*", false)
if err != nil {
mlog.Printf("scan directory '%s' error: %s", dir, err.Error())
// If we can't scan the directory, add it to watch list as fallback
*watchPaths = append(*watchPaths, watchPath{Path: dir, Recursive: true})
return false
}
// First pass: identify valid subdirectories and check for directly ignored children
var validSubDirs []string
hasIgnoredChild := false
for _, entry := range entries {
if !gfile.IsDir(entry) {
continue
}
if isIgnoredDirName(entry, ignorePatterns) {
hasIgnoredChild = true
} else {
validSubDirs = append(validSubDirs, entry)
}
}
// If already has ignored child, we know this dir needs non-recursive watch
if hasIgnoredChild {
*watchPaths = append(*watchPaths, watchPath{Path: dir, Recursive: false})
for _, subDir := range validSubDirs {
app.collectWatchPaths(subDir, ignorePatterns, watchPaths)
}
return true
}
// No ignored children, but need to check descendants recursively
// Collect results from all subdirectories first
subResults := make([]bool, len(validSubDirs))
subWatchPaths := make([][]watchPath, len(validSubDirs))
hasIgnoredDescendant := false
for i, subDir := range validSubDirs {
var subPaths []watchPath
subResults[i] = app.collectWatchPaths(subDir, ignorePatterns, &subPaths)
subWatchPaths[i] = subPaths
if subResults[i] {
hasIgnoredDescendant = true
}
}
if !hasIgnoredDescendant {
// No ignored descendants at any depth, watch this directory recursively
*watchPaths = append(*watchPaths, watchPath{Path: dir, Recursive: true})
return false
}
// Has ignored descendants, watch current directory non-recursively
// and add all collected subdirectory watch paths
*watchPaths = append(*watchPaths, watchPath{Path: dir, Recursive: false})
for _, subPaths := range subWatchPaths {
*watchPaths = append(*watchPaths, subPaths...)
}
return true
}
// defaultIgnorePatterns contains glob patterns for directory names that should be ignored when watching.
// These directories typically contain third-party code or non-source files.
// Supported glob syntax (filepath.Match):
// - "*" matches any sequence of non-separator characters
// - "?" matches any single non-separator character
// - "[abc]" matches any character in the bracket
// - "[a-z]" matches any character in the range
// - "[^abc]" or "[!abc]" matches any character not in the bracket
//
// Note: patterns match directory base names only, not full paths (no "/" or path separators allowed).
var defaultIgnorePatterns = []string{
"node_modules",
"vendor",
".*", // All hidden directories (covers .git, .svn, .hg, .idea, .vscode, etc.)
"_*", // Directories starting with underscore
}
// isIgnoredDirName checks if a directory name matches any ignored pattern.
// It accepts either a full path or just the directory name, but only matches against the base name.
// Note: patterns should not contain "/" as they only match directory names, not paths.
func isIgnoredDirName(name string, ignorePatterns []string) bool {
baseName := gfile.Basename(name)
for _, pattern := range ignorePatterns {
if matched, _ := filepath.Match(pattern, baseName); matched {
if matched {
return true
}
}
return false
}
// parseCommaSeparatedArgs parses command line arguments that may contain comma-separated values.
// It handles both single argument with commas (e.g., "a,b,c") and multiple arguments.
func parseCommaSeparatedArgs(args []string) []string {
var result []string
for _, arg := range args {
parts := strings.Split(arg, ",")
for _, part := range parts {
trimmed := strings.TrimSpace(part)
if trimmed != "" {
result = append(result, trimmed)
}
}
}
return result
}

View File

@ -15,11 +15,9 @@ import (
)
var (
ctx = context.Background()
testDB gdb.DB
testPgDB gdb.DB
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
linkPg = "pgsql:postgres:12345678@tcp(127.0.0.1:5432)/test"
ctx = context.Background()
testDB gdb.DB
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
)
func init() {
@ -30,10 +28,6 @@ func init() {
if err != nil {
panic(err)
}
// PostgreSQL connection (optional, may not be available in all environments)
testPgDB, _ = gdb.New(gdb.ConfigNode{
Link: linkPg,
})
}
func dropTableWithDb(db gdb.DB, table string) {
@ -42,11 +36,3 @@ func dropTableWithDb(db gdb.DB, table string) {
gtest.Error(err)
}
}
// dropTableStd uses standard SQL syntax compatible with MySQL and PostgreSQL.
func dropTableStd(db gdb.DB, table string) {
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS %s", table)
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
gtest.Error(err)
}
}

View File

@ -1,84 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package cmd
import (
"testing"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
)
func Test_Env_Index(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test that env command runs without error
_, err := Env.Index(ctx, cEnvInput{})
t.AssertNil(err)
})
}
func Test_Env_ParseGoEnvOutput(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test parsing normal go env output
lines := []string{
"set GOPATH=C:\\Users\\test\\go",
"set GOROOT=C:\\Go",
"set GOOS=windows",
"GOARCH=amd64", // Unix format without "set " prefix
"CGO_ENABLED=0",
}
for _, line := range lines {
line = gstr.Trim(line)
if gstr.Pos(line, "set ") == 0 {
line = line[4:]
}
match, _ := gregex.MatchString(`(.+?)=(.*)`, line)
t.Assert(len(match) >= 3, true)
}
})
}
func Test_Env_ParseGoEnvOutput_WithWarnings(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test parsing go env output that contains warning messages
// These lines should be skipped without causing errors
lines := []string{
"go: stripping unprintable or unescapable characters from %\"GOPROXY\"%",
"go: warning: some warning message",
"# this is a comment",
"",
"set GOPATH=C:\\Users\\test\\go",
"set GOOS=windows",
}
array := make([][]string, 0)
for _, line := range lines {
line = gstr.Trim(line)
if line == "" {
continue
}
if gstr.Pos(line, "set ") == 0 {
line = line[4:]
}
match, _ := gregex.MatchString(`(.+?)=(.*)`, line)
if len(match) < 3 {
// Skip lines that don't match key=value format (e.g., warning messages)
continue
}
array = append(array, []string{gstr.Trim(match[1]), gstr.Trim(match[2])})
}
// Should have parsed 2 valid environment variables
t.Assert(len(array), 2)
t.Assert(array[0][0], "GOPATH")
t.Assert(array[0][1], "C:\\Users\\test\\go")
t.Assert(array[1][0], "GOOS")
t.Assert(array[1][1], "windows")
})
}

View File

@ -10,7 +10,6 @@ import (
"testing"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)
func Test_Fix_doFixV25Content(t *testing.T) {
@ -23,82 +22,3 @@ func Test_Fix_doFixV25Content(t *testing.T) {
t.AssertNil(err)
})
}
func Test_Fix_doFixV25Content_WithReplacement(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `s.BindHookHandlerByMap("/path", map[string]ghttp.HandlerFunc{
ghttp.HookBeforeServe: func(r *ghttp.Request) {},
})`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
// Verify the replacement was made
t.Assert(gstr.Contains(newContent, "map[ghttp.HookName]ghttp.HandlerFunc"), true)
t.Assert(gstr.Contains(newContent, "map[string]ghttp.HandlerFunc"), false)
})
}
func Test_Fix_doFixV25Content_NoMatch(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `package main
func main() {
fmt.Println("Hello World")
}
`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
// Content should remain unchanged
t.Assert(newContent, content)
})
}
func Test_Fix_doFixV25Content_MultipleMatches(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `
s.BindHookHandlerByMap("/path1", map[string]ghttp.HandlerFunc{})
s.BindHookHandlerByMap("/path2", map[string]ghttp.HandlerFunc{})
`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
// Both should be replaced
count := gstr.Count(newContent, "map[ghttp.HookName]ghttp.HandlerFunc")
t.Assert(count, 2)
})
}
func Test_Fix_doFixV25Content_EmptyContent(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = ""
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
t.Assert(newContent, "")
})
}
func Test_Fix_doFixV25Content_ComplexPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
f = cFix{}
content = `s.BindHookHandlerByMap("/api/v1/user/{id}/profile", map[string]ghttp.HandlerFunc{
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.Response.Write("before")
},
})`
)
newContent, err := f.doFixV25Content(content)
t.AssertNil(err)
t.Assert(gstr.Contains(newContent, "map[ghttp.HookName]ghttp.HandlerFunc"), true)
})
}

View File

@ -22,7 +22,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
apiFolder = gtest.DataPath("genctrl", "default", "api")
apiFolder = gtest.DataPath("genctrl", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: path,
@ -39,7 +39,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.RemoveAll(path)
defer gfile.Remove(path)
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
@ -49,7 +49,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
genApi = apiFolder + filepath.FromSlash("/article/article.go")
genApiExpect = apiFolder + filepath.FromSlash("/article/article_expect.go")
)
defer gfile.RemoveAll(genApi)
defer gfile.Remove(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))
// files
@ -67,7 +67,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
})
// content
testPath := gtest.DataPath("genctrl", "default", "controller")
testPath := gtest.DataPath("genctrl", "controller")
expectFiles := []string{
testPath + filepath.FromSlash("/article/article.go"),
testPath + filepath.FromSlash("/article/article_new.go"),
@ -84,104 +84,6 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
})
}
func Test_Gen_Ctrl_Default_Multi(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
apiFolder = gtest.DataPath("genctrl", "multi", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: path,
WatchFile: "",
SdkPath: "",
SdkStdVersion: false,
SdkNoV1: false,
Clear: false,
Merge: false,
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.RemoveAll(path)
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
// apiInterface file
var (
genApiSlice = []string{
apiFolder + filepath.FromSlash("/admin/article/article.go"),
apiFolder + filepath.FromSlash("/admin/user/user.go"),
apiFolder + filepath.FromSlash("/app/user/user.go"),
apiFolder + filepath.FromSlash("/app/user/user_ext/user_ext.go"),
}
genApiSliceExpect = []string{
apiFolder + filepath.FromSlash("/admin/article/article_expect.go"),
apiFolder + filepath.FromSlash("/admin/user/user_expect.go"),
apiFolder + filepath.FromSlash("/app/user/user_expect.go"),
apiFolder + filepath.FromSlash("/app/user/user_ext/user_ext_expect.go"),
}
)
for i := range genApiSlice {
t.Assert(gfile.GetContents(genApiSlice[i]), gfile.GetContents(genApiSliceExpect[i]))
gfile.RemoveAll(genApiSlice[i])
}
// files
files, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(files, []string{
path + filepath.FromSlash("/admin/article/article.go"),
path + filepath.FromSlash("/admin/article/article_new.go"),
path + filepath.FromSlash("/admin/article/article_v1_create.go"),
path + filepath.FromSlash("/admin/user/user.go"),
path + filepath.FromSlash("/admin/user/user_new.go"),
path + filepath.FromSlash("/admin/user/user_v1_create.go"),
path + filepath.FromSlash("/app/user/user.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext_new.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext_v1_create.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext_v1_update.go"),
path + filepath.FromSlash("/app/user/user_new.go"),
path + filepath.FromSlash("/app/user/user_v1_create.go"),
path + filepath.FromSlash("/app/user/user_v1_update.go"),
})
// content
testPath := gtest.DataPath("genctrl", "multi", "controller")
expectFiles := []string{
testPath + filepath.FromSlash("/admin/article/article.go"),
testPath + filepath.FromSlash("/admin/article/article_new.go"),
testPath + filepath.FromSlash("/admin/article/article_v1_create.go"),
testPath + filepath.FromSlash("/admin/user/user.go"),
testPath + filepath.FromSlash("/admin/user/user_new.go"),
testPath + filepath.FromSlash("/admin/user/user_v1_create.go"),
testPath + filepath.FromSlash("/app/user/user.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext_new.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext_v1_create.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext_v1_update.go"),
testPath + filepath.FromSlash("/app/user/user_new.go"),
testPath + filepath.FromSlash("/app/user/user_v1_create.go"),
testPath + filepath.FromSlash("/app/user/user_v1_update.go"),
}
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})
}
func expectFilesContent(t *gtest.T, paths []string, expectPaths []string) {
for i, expectFile := range expectPaths {
val := gfile.GetContents(paths[i])
@ -196,8 +98,8 @@ func Test_Gen_Ctrl_UseMerge_AddNewFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctrlPath = gfile.Temp(guid.S())
// ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl", "merge", "add_new_file", "api")
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl-merge", "add_new_file", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: ctrlPath,
@ -216,7 +118,7 @@ type DictTypeAddRes struct {
err := gfile.Mkdir(ctrlPath)
t.AssertNil(err)
defer gfile.RemoveAll(ctrlPath)
defer gfile.Remove(ctrlPath)
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
@ -225,7 +127,7 @@ type DictTypeAddRes struct {
genApi = filepath.Join(apiFolder, "/dict/dict.go")
genApiExpect = filepath.Join(apiFolder, "/dict/dict_expect.go")
)
defer gfile.RemoveAll(genApi)
defer gfile.Remove(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))
genCtrlFiles, err := gfile.ScanDir(ctrlPath, "*.go", true)
@ -236,7 +138,7 @@ type DictTypeAddRes struct {
filepath.Join(ctrlPath, "/dict/dict_v1_dict_type.go"),
})
expectCtrlPath := gtest.DataPath("genctrl", "merge", "add_new_file", "controller")
expectCtrlPath := gtest.DataPath("genctrl-merge", "add_new_file", "controller")
expectFiles := []string{
filepath.Join(expectCtrlPath, "/dict/dict.go"),
filepath.Join(expectCtrlPath, "/dict/dict_new.go"),
@ -250,7 +152,7 @@ type DictTypeAddRes struct {
newApiFilePath := filepath.Join(apiFolder, "/dict/v1/test_new.go")
err = gfile.PutContents(newApiFilePath, testNewApiFile)
t.AssertNil(err)
defer gfile.RemoveAll(newApiFilePath)
defer gfile.Remove(newApiFilePath)
// Then execute the command
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
@ -277,8 +179,8 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctrlPath = gfile.Temp(guid.S())
// ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl", "merge", "add_new_ctrl", "api")
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl-merge", "add_new_ctrl", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: ctrlPath,
@ -288,7 +190,7 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
err := gfile.Mkdir(ctrlPath)
t.AssertNil(err)
defer gfile.RemoveAll(ctrlPath)
defer gfile.Remove(ctrlPath)
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
@ -297,7 +199,7 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
genApi = filepath.Join(apiFolder, "/dict/dict.go")
genApiExpect = filepath.Join(apiFolder, "/dict/dict_expect.go")
)
defer gfile.RemoveAll(genApi)
defer gfile.Remove(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))
genCtrlFiles, err := gfile.ScanDir(ctrlPath, "*.go", true)
@ -308,7 +210,7 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
filepath.Join(ctrlPath, "/dict/dict_v1_dict_type.go"),
})
expectCtrlPath := gtest.DataPath("genctrl", "merge", "add_new_ctrl", "controller")
expectCtrlPath := gtest.DataPath("genctrl-merge", "add_new_ctrl", "controller")
expectFiles := []string{
filepath.Join(expectCtrlPath, "/dict/dict.go"),
filepath.Join(expectCtrlPath, "/dict/dict_new.go"),
@ -334,7 +236,7 @@ type DictTypeAddRes struct {
err = gfile.PutContentsAppend(dictModuleFileName, testNewApiFile)
t.AssertNil(err)
// ==================================
//==================================
// Then execute the command
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
@ -360,7 +262,7 @@ func Test_Gen_Ctrl_UseMerge_Issue3460(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctrlPath = gfile.Temp(guid.S())
// ctrlPath = gtest.DataPath("issue", "3460", "controller")
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("issue", "3460", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
@ -376,7 +278,7 @@ func Test_Gen_Ctrl_UseMerge_Issue3460(t *testing.T) {
err := gfile.Mkdir(ctrlPath)
t.AssertNil(err)
defer gfile.RemoveAll(ctrlPath)
defer gfile.Remove(ctrlPath)
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)

View File

@ -460,398 +460,3 @@ func Test_Gen_Dao_Issue3749(t *testing.T) {
}
})
}
// https://github.com/gogf/gf/issues/4629
// Test tables pattern matching with * wildcard.
func Test_Gen_Dao_Issue4629_TablesPattern_Star(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "trade_order"
table2 = "trade_item"
table3 = "user_info"
table4 = "user_log"
table5 = "config"
sqlFilePath = gtest.DataPath(`gendao`, `tables_pattern.sql`)
)
dropTableStd(db, table1)
dropTableStd(db, table2)
dropTableStd(db, table3)
dropTableStd(db, table4)
dropTableStd(db, table5)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableStd(db, table1)
defer dropTableStd(db, table2)
defer dropTableStd(db, table3)
defer dropTableStd(db, table4)
defer dropTableStd(db, table5)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Group: group,
Tables: "trade_*", // Should match trade_order, trade_item
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 2 dao files: trade_order.go, trade_item.go
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
t.AssertNil(err)
t.Assert(len(generatedFiles), 2)
// Verify the correct files are generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_order.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_item.go")), true)
// user_* and config should NOT be generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_info.go")), false)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_log.go")), false)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "config.go")), false)
})
}
// https://github.com/gogf/gf/issues/4629
// Test tables pattern matching with multiple patterns.
func Test_Gen_Dao_Issue4629_TablesPattern_Multiple(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "trade_order"
table2 = "trade_item"
table3 = "user_info"
table4 = "user_log"
table5 = "config"
sqlFilePath = gtest.DataPath(`gendao`, `tables_pattern.sql`)
)
dropTableStd(db, table1)
dropTableStd(db, table2)
dropTableStd(db, table3)
dropTableStd(db, table4)
dropTableStd(db, table5)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableStd(db, table1)
defer dropTableStd(db, table2)
defer dropTableStd(db, table3)
defer dropTableStd(db, table4)
defer dropTableStd(db, table5)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Group: group,
Tables: "trade_*,user_*", // Should match trade_order, trade_item, user_info, user_log
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 4 dao files
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
t.AssertNil(err)
t.Assert(len(generatedFiles), 4)
// Verify the correct files are generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_order.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_item.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_info.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_log.go")), true)
// config should NOT be generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "config.go")), false)
})
}
// https://github.com/gogf/gf/issues/4629
// Test tables pattern mixed with exact table name.
func Test_Gen_Dao_Issue4629_TablesPattern_Mixed(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "trade_order"
table2 = "trade_item"
table3 = "user_info"
table4 = "user_log"
table5 = "config"
sqlFilePath = gtest.DataPath(`gendao`, `tables_pattern.sql`)
)
dropTableStd(db, table1)
dropTableStd(db, table2)
dropTableStd(db, table3)
dropTableStd(db, table4)
dropTableStd(db, table5)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableStd(db, table1)
defer dropTableStd(db, table2)
defer dropTableStd(db, table3)
defer dropTableStd(db, table4)
defer dropTableStd(db, table5)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Group: group,
Tables: "trade_*,config", // Pattern + exact name
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 3 dao files: trade_order.go, trade_item.go, config.go
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
t.AssertNil(err)
t.Assert(len(generatedFiles), 3)
// Verify the correct files are generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_order.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_item.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "config.go")), true)
// user_* should NOT be generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_info.go")), false)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_log.go")), false)
})
}
// https://github.com/gogf/gf/issues/4629
// Test tables pattern with ? wildcard (single character match).
func Test_Gen_Dao_Issue4629_TablesPattern_Question(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "trade_order"
table2 = "trade_item"
table3 = "user_info"
table4 = "user_log"
table5 = "config"
sqlFilePath = gtest.DataPath(`gendao`, `tables_pattern.sql`)
)
dropTableStd(db, table1)
dropTableStd(db, table2)
dropTableStd(db, table3)
dropTableStd(db, table4)
dropTableStd(db, table5)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableStd(db, table1)
defer dropTableStd(db, table2)
defer dropTableStd(db, table3)
defer dropTableStd(db, table4)
defer dropTableStd(db, table5)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Group: group,
Tables: "user_???", // ? matches single char: user_log (3 chars) but not user_info (4 chars)
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 1 dao file: user_log.go (3 chars after user_)
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
t.AssertNil(err)
t.Assert(len(generatedFiles), 1)
// Verify only user_log is generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_log.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_info.go")), false) // 4 chars, doesn't match
})
}
// https://github.com/gogf/gf/issues/4629
// Test that exact table names still work (backward compatibility).
func Test_Gen_Dao_Issue4629_TablesPattern_ExactNames(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
table1 = "trade_order"
table2 = "trade_item"
table3 = "user_info"
table4 = "user_log"
table5 = "config"
sqlFilePath = gtest.DataPath(`gendao`, `tables_pattern.sql`)
)
dropTableStd(db, table1)
dropTableStd(db, table2)
dropTableStd(db, table3)
dropTableStd(db, table4)
dropTableStd(db, table5)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableStd(db, table1)
defer dropTableStd(db, table2)
defer dropTableStd(db, table3)
defer dropTableStd(db, table4)
defer dropTableStd(db, table5)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Group: group,
Tables: "trade_order,config", // Exact names, no patterns
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 2 dao files
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
t.AssertNil(err)
t.Assert(len(generatedFiles), 2)
// Verify exactly the specified tables are generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_order.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "config.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_item.go")), false)
})
}
// https://github.com/gogf/gf/issues/4629
// Test tables pattern matching with PostgreSQL.
func Test_Gen_Dao_Issue4629_TablesPattern_PgSql(t *testing.T) {
if testPgDB == nil {
t.Skip("PostgreSQL database not available, skipping test")
return
}
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testPgDB
table1 = "trade_order"
table2 = "trade_item"
table3 = "user_info"
table4 = "user_log"
table5 = "config"
sqlFilePath = gtest.DataPath(`gendao`, `tables_pattern.sql`)
)
dropTableStd(db, table1)
dropTableStd(db, table2)
dropTableStd(db, table3)
dropTableStd(db, table4)
dropTableStd(db, table5)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableStd(db, table1)
defer dropTableStd(db, table2)
defer dropTableStd(db, table3)
defer dropTableStd(db, table4)
defer dropTableStd(db, table5)
// Test tables pattern with tablesEx pattern
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: linkPg,
Group: group,
Tables: "trade_*,user_*,config", // Match only our test tables
TablesEx: "user_*", // Exclude user_* tables
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 3 dao files: trade_order, trade_item, config (user_* excluded by tablesEx)
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
t.AssertNil(err)
t.Assert(len(generatedFiles), 3)
// Verify the correct files are generated
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_order.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "trade_item.go")), true)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "config.go")), true)
// user_* should NOT be generated (excluded by tablesEx)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_info.go")), false)
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_log.go")), false)
})
}

View File

@ -18,92 +18,6 @@ import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
)
// Test_Gen_Dao_Sharding_Overlapping tests the fix for issue #4603.
// When sharding patterns have overlapping prefixes (like "a_?", "a_b_?", "a_c_?"),
// longer (more specific) patterns should be matched first.
// https://github.com/gogf/gf/issues/4603
func Test_Gen_Dao_Sharding_Overlapping(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
db = testDB
tableA1 = "a_1"
tableA2 = "a_2"
tableAB1 = "a_b_1"
tableAB2 = "a_b_2"
tableAC1 = "a_c_1"
tableAC2 = "a_c_2"
sqlFilePath = gtest.DataPath(`gendao`, `sharding`, `sharding_overlapping.sql`)
)
dropTableWithDb(db, tableA1)
dropTableWithDb(db, tableA2)
dropTableWithDb(db, tableAB1)
dropTableWithDb(db, tableAB2)
dropTableWithDb(db, tableAC1)
dropTableWithDb(db, tableAC2)
t.AssertNil(execSqlFile(db, sqlFilePath))
defer dropTableWithDb(db, tableA1)
defer dropTableWithDb(db, tableA2)
defer dropTableWithDb(db, tableAB1)
defer dropTableWithDb(db, tableAB2)
defer dropTableWithDb(db, tableAC1)
defer dropTableWithDb(db, tableAC2)
var (
path = gfile.Temp(guid.S())
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: link,
Group: group,
Prefix: "",
// Patterns with overlapping prefixes - order should not matter due to sorting fix
ShardingPattern: []string{
`a_?`, // shortest, matches a_1, a_2 but also a_b_1, a_c_1 without fix
`a_b_?`, // longer, should match a_b_1, a_b_2
`a_c_?`, // longer, should match a_c_1, a_c_2
},
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
pwd := gfile.Pwd()
err = gfile.Chdir(path)
t.AssertNil(err)
defer gfile.Chdir(pwd)
defer gfile.RemoveAll(path)
_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
// Should generate 3 dao files: a.go, a_b.go, a_c.go (plus internal versions)
generatedFiles, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
// 3 sharding groups * 4 files each (dao, internal, do, entity) = 12 files
t.Assert(len(generatedFiles), 12)
var (
daoAContent = gfile.GetContents(gfile.Join(path, "dao", "a.go"))
daoABContent = gfile.GetContents(gfile.Join(path, "dao", "a_b.go"))
daoACContent = gfile.GetContents(gfile.Join(path, "dao", "a_c.go"))
)
// Verify each sharding group has correct dao file generated
t.Assert(gstr.Contains(daoAContent, "aShardingHandler"), true)
t.Assert(gstr.Contains(daoAContent, "m.Sharding(gdb.ShardingConfig{"), true)
t.Assert(gstr.Contains(daoABContent, "aBShardingHandler"), true)
t.Assert(gstr.Contains(daoABContent, "m.Sharding(gdb.ShardingConfig{"), true)
t.Assert(gstr.Contains(daoACContent, "aCShardingHandler"), true)
t.Assert(gstr.Contains(daoACContent, "m.Sharding(gdb.ShardingConfig{"), true)
})
}
func Test_Gen_Dao_Sharding(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (

View File

@ -1,158 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package cmd
import (
"path/filepath"
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/util/gutil"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genenums"
)
// https://github.com/gogf/gf/issues/4387
// Test that the output path is relative to the original working directory,
// not the source directory after Chdir.
func Test_Gen_Enums_Issue4387_RelativePath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
// Create temp directory to simulate user's project
tempPath = gfile.Temp(guid.S())
// Copy testdata to temp directory
srcTestData = gtest.DataPath("issue", "4387")
)
// Setup: create temp project structure
err := gfile.CopyDir(srcTestData, tempPath)
t.AssertNil(err)
defer gfile.Remove(tempPath)
// Save original working directory
originalWd := gfile.Pwd()
// Change to temp directory (simulate user being in project root)
err = gfile.Chdir(tempPath)
t.AssertNil(err)
defer gfile.Chdir(originalWd) // Restore original working directory
// Run gen enums with relative paths
var (
srcFolder = "api"
outputPath = filepath.FromSlash("internal/packed/packed_enums.go")
in = genenums.CGenEnumsInput{
Src: srcFolder,
Path: outputPath,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
_, err = genenums.CGenEnums{}.Enums(ctx, in)
t.AssertNil(err)
// Expected: file should be created at tempPath/internal/packed/packed_enums.go
expectedPath := filepath.Join(tempPath, "internal", "packed", "packed_enums.go")
// Bug: file is created at tempPath/api/internal/packed/packed_enums.go
wrongPath := filepath.Join(tempPath, "api", "internal", "packed", "packed_enums.go")
// Assert the file is at the expected location
t.Assert(gfile.Exists(expectedPath), true)
// Assert the file is NOT at the wrong location
t.Assert(gfile.Exists(wrongPath), false)
})
}
// Test gen enums with absolute output path (should work correctly)
func Test_Gen_Enums_AbsolutePath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
tempPath = gfile.Temp(guid.S())
srcTestData = gtest.DataPath("issue", "4387")
)
err := gfile.CopyDir(srcTestData, tempPath)
t.AssertNil(err)
defer gfile.Remove(tempPath)
originalWd := gfile.Pwd()
err = gfile.Chdir(tempPath)
t.AssertNil(err)
defer gfile.Chdir(originalWd)
// Use absolute path for output
var (
srcFolder = "api"
outputPath = filepath.Join(tempPath, "internal", "packed", "packed_enums.go")
in = genenums.CGenEnumsInput{
Src: srcFolder,
Path: outputPath,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
_, err = genenums.CGenEnums{}.Enums(ctx, in)
t.AssertNil(err)
// Assert the file exists at absolute path
t.Assert(gfile.Exists(outputPath), true)
})
}
// Test gen enums in monorepo mode (cd app/xxx/ then run command)
func Test_Gen_Enums_Issue4387_Monorepo(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
// Simulate monorepo structure
tempPath = gfile.Temp(guid.S())
srcTestData = gtest.DataPath("issue", "4387")
// app/myapp is the subdirectory in monorepo
appPath = filepath.Join(tempPath, "app", "myapp")
)
// Create monorepo structure: tempPath/app/myapp/api/...
err := gfile.Mkdir(appPath)
t.AssertNil(err)
// Copy testdata into app/myapp
err = gfile.CopyDir(srcTestData, appPath)
t.AssertNil(err)
defer gfile.Remove(tempPath)
originalWd := gfile.Pwd()
// cd app/myapp (simulate user in monorepo subdirectory)
err = gfile.Chdir(appPath)
t.AssertNil(err)
defer gfile.Chdir(originalWd)
var (
srcFolder = "api"
outputPath = filepath.FromSlash("internal/packed/packed_enums.go")
in = genenums.CGenEnumsInput{
Src: srcFolder,
Path: outputPath,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)
_, err = genenums.CGenEnums{}.Enums(ctx, in)
t.AssertNil(err)
// Expected: file at app/myapp/internal/packed/packed_enums.go
expectedPath := filepath.Join(appPath, "internal", "packed", "packed_enums.go")
// Bug: file at app/myapp/api/internal/packed/packed_enums.go
wrongPath := filepath.Join(appPath, "api", "internal", "packed", "packed_enums.go")
t.Assert(gfile.Exists(expectedPath), true)
t.Assert(gfile.Exists(wrongPath), false)
})
}

View File

@ -88,76 +88,3 @@ func TestGenPbIssue3953(t *testing.T) {
t.Assert(gstr.Contains(genContent, notExceptText), false)
})
}
func TestGenPb_MultipleTags(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
outputPath = gfile.Temp(guid.S())
outputApiPath = filepath.Join(outputPath, "api")
outputCtrlPath = filepath.Join(outputPath, "controller")
protobufFolder = gtest.DataPath("genpb")
in = genpb.CGenPbInput{
Path: protobufFolder,
OutputApi: outputApiPath,
OutputCtrl: outputCtrlPath,
}
err error
)
err = gfile.Mkdir(outputApiPath)
t.AssertNil(err)
err = gfile.Mkdir(outputCtrlPath)
t.AssertNil(err)
defer gfile.Remove(outputPath)
_, err = genpb.CGenPb{}.Pb(ctx, in)
t.AssertNil(err)
// Test multiple_tags.proto output
genContent := gfile.GetContents(filepath.Join(outputApiPath, "multiple_tags.pb.go"))
// Id field should have combined validation tags: v:"required#Id > 0"
t.Assert(gstr.Contains(genContent, `v:"required#Id > 0"`), true)
// Name field should have dc tag from plain comment
t.Assert(gstr.Contains(genContent, `dc:"User name for login"`), true)
// Email field should have combined validation and dc tag
t.Assert(gstr.Contains(genContent, `v:"requiredemail"`), true)
t.Assert(gstr.Contains(genContent, `dc:"User email address"`), true)
})
}
func TestGenPb_NestedMessage(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
outputPath = gfile.Temp(guid.S())
outputApiPath = filepath.Join(outputPath, "api")
outputCtrlPath = filepath.Join(outputPath, "controller")
protobufFolder = gtest.DataPath("genpb")
in = genpb.CGenPbInput{
Path: protobufFolder,
OutputApi: outputApiPath,
OutputCtrl: outputCtrlPath,
}
err error
)
err = gfile.Mkdir(outputApiPath)
t.AssertNil(err)
err = gfile.Mkdir(outputCtrlPath)
t.AssertNil(err)
defer gfile.Remove(outputPath)
_, err = genpb.CGenPb{}.Pb(ctx, in)
t.AssertNil(err)
// Test nested_message.proto output
genContent := gfile.GetContents(filepath.Join(outputApiPath, "nested_message.pb.go"))
// Order.OrderId should have v:"required"
t.Assert(gstr.Contains(genContent, `v:"required"`), true)
// Order.Detail should have dc:"Order details"
t.Assert(gstr.Contains(genContent, `dc:"Order details"`), true)
// OrderDetail.Quantity should have v:"min:1"
t.Assert(gstr.Contains(genContent, `v:"min:1"`), true)
// OrderDetail.Price should have v:"min:0.01"
t.Assert(gstr.Contains(genContent, `v:"min:0.01"`), true)
})
}

View File

@ -156,130 +156,3 @@ func Test_Issue3835(t *testing.T) {
t.Assert(gfile.GetContents(genFile), gfile.GetContents(expectFile))
})
}
func Test_Gen_Service_CamelCase(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
srvFolder = gtest.DataPath("genservice", "logic")
in = genservice.CGenServiceInput{
SrcFolder: srvFolder,
DstFolder: dstFolder,
DstFileNameCase: "Camel",
WatchFile: "",
StPattern: "",
Packages: nil,
ImportPrefix: "",
Clear: false,
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
// Clean up generated logic.go
genSrv := srvFolder + filepath.FromSlash("/logic.go")
defer gfile.Remove(genSrv)
_, err = genservice.CGenService{}.Service(ctx, in)
t.AssertNil(err)
// Files should be in CamelCase
files, err := gfile.ScanDir(dstFolder, "*.go", true)
t.AssertNil(err)
t.Assert(files, []string{
dstFolder + filepath.FromSlash("/Article.go"),
dstFolder + filepath.FromSlash("/Base.go"),
dstFolder + filepath.FromSlash("/Delivery.go"),
dstFolder + filepath.FromSlash("/User.go"),
})
})
}
func Test_Gen_Service_PackagesFilter(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
srvFolder = gtest.DataPath("genservice", "logic")
in = genservice.CGenServiceInput{
SrcFolder: srvFolder,
DstFolder: dstFolder,
DstFileNameCase: "Snake",
WatchFile: "",
StPattern: "",
Packages: []string{"user"},
ImportPrefix: "",
Clear: false,
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
// Clean up generated logic.go
genSrv := srvFolder + filepath.FromSlash("/logic.go")
defer gfile.Remove(genSrv)
_, err = genservice.CGenService{}.Service(ctx, in)
t.AssertNil(err)
// Only user.go should be generated
files, err := gfile.ScanDir(dstFolder, "*.go", true)
t.AssertNil(err)
t.Assert(len(files), 1)
t.Assert(files[0], dstFolder+filepath.FromSlash("/user.go"))
})
}
// https://github.com/gogf/gf/issues/4242
// Test that versioned imports and aliased imports are correctly preserved.
// The issue is that imports like "github.com/minio/minio-go/v7" were being
// incorrectly handled because the package name (minio) differs from
// the directory name (minio-go).
func Test_Issue4242(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
srvFolder = gtest.DataPath("issue", "4242", "logic")
in = genservice.CGenServiceInput{
SrcFolder: srvFolder,
DstFolder: dstFolder,
DstFileNameCase: "Snake",
WatchFile: "",
StPattern: "",
Packages: nil,
ImportPrefix: "",
Clear: false,
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
_, err = genservice.CGenService{}.Service(ctx, in)
t.AssertNil(err)
// Test versioned imports
t.Assert(
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242.go")),
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242.go")),
)
// Test aliased imports
t.Assert(
gfile.GetContents(dstFolder+filepath.FromSlash("/issue_4242_alias.go")),
gfile.GetContents(gtest.DataPath("issue", "4242", "service", "issue_4242_alias.go")),
)
})
}

View File

@ -1,346 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package cmd
import (
"context"
"path/filepath"
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/guid"
)
func Test_Pack_ToGoFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "packed", "data.go")
)
// Create source directory with test files
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create test files
err = gfile.PutContents(filepath.Join(srcPath, "test.txt"), "hello world")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(srcPath, "test.json"), `{"key":"value"}`)
t.AssertNil(err)
// Create packed directory
err = gfile.Mkdir(filepath.Join(dstPath, "packed"))
t.AssertNil(err)
// Pack to go file
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
Name: "packed",
})
t.AssertNil(err)
// Verify output file exists
t.Assert(gfile.Exists(dstFile), true)
// Verify it's a valid Go file
content := gfile.GetContents(dstFile)
t.Assert(gstr.Contains(content, "package packed"), true)
t.Assert(gstr.Contains(content, "func init()"), true)
})
}
func Test_Pack_ToBinaryFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "data.bin")
)
// Create source directory with test files
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create test file
err = gfile.PutContents(filepath.Join(srcPath, "test.txt"), "binary content")
t.AssertNil(err)
// Pack to binary file (no Name specified)
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
})
t.AssertNil(err)
// Verify output file exists
t.Assert(gfile.Exists(dstFile), true)
// Verify it's a binary file (not a Go file)
content := gfile.GetContents(dstFile)
t.Assert(gstr.Contains(content, "package"), false)
})
}
func Test_Pack_MultipleSources(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath1 = gfile.Temp(guid.S())
srcPath2 = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "packed", "multi.go")
)
// Create source directories
err := gfile.Mkdir(srcPath1)
t.AssertNil(err)
defer gfile.Remove(srcPath1)
err = gfile.Mkdir(srcPath2)
t.AssertNil(err)
defer gfile.Remove(srcPath2)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create test files in each source
err = gfile.PutContents(filepath.Join(srcPath1, "file1.txt"), "content1")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(srcPath2, "file2.txt"), "content2")
t.AssertNil(err)
// Create packed directory
err = gfile.Mkdir(filepath.Join(dstPath, "packed"))
t.AssertNil(err)
// Pack multiple sources (comma-separated)
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath1 + "," + srcPath2,
Dst: dstFile,
Name: "packed",
})
t.AssertNil(err)
// Verify output file exists
t.Assert(gfile.Exists(dstFile), true)
})
}
func Test_Pack_WithPrefix(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "packed", "prefix.go")
)
// Create source directory
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create test file
err = gfile.PutContents(filepath.Join(srcPath, "test.txt"), "with prefix")
t.AssertNil(err)
// Create packed directory
err = gfile.Mkdir(filepath.Join(dstPath, "packed"))
t.AssertNil(err)
// Pack with prefix
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
Name: "packed",
Prefix: "/static",
})
t.AssertNil(err)
// Verify output file exists
t.Assert(gfile.Exists(dstFile), true)
})
}
func Test_Pack_WithKeepPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "packed", "keeppath.go")
)
// Create source directory with subdirectory
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create subdirectory and file
subDir := filepath.Join(srcPath, "subdir")
err = gfile.Mkdir(subDir)
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(subDir, "test.txt"), "keeppath content")
t.AssertNil(err)
// Create packed directory
err = gfile.Mkdir(filepath.Join(dstPath, "packed"))
t.AssertNil(err)
// Pack with keepPath
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
Name: "packed",
KeepPath: true,
})
t.AssertNil(err)
// Verify output file exists
t.Assert(gfile.Exists(dstFile), true)
})
}
func Test_Pack_AutoPackageName(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "mypackage", "data.go")
)
// Create source directory
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create test file
err = gfile.PutContents(filepath.Join(srcPath, "test.txt"), "auto package name")
t.AssertNil(err)
// Create mypackage directory
err = gfile.Mkdir(filepath.Join(dstPath, "mypackage"))
t.AssertNil(err)
// Pack without Name - should use directory name "mypackage"
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
// Name not specified, should be auto-detected as "mypackage"
})
t.AssertNil(err)
// Verify output file exists and has correct package name
t.Assert(gfile.Exists(dstFile), true)
content := gfile.GetContents(dstFile)
t.Assert(gstr.Contains(content, "package mypackage"), true)
})
}
func Test_Pack_EmptySource(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "packed", "empty.go")
)
// Create empty source directory
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create packed directory
err = gfile.Mkdir(filepath.Join(dstPath, "packed"))
t.AssertNil(err)
// Pack empty directory
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
Name: "packed",
})
t.AssertNil(err)
// Verify output file exists (even if source is empty)
t.Assert(gfile.Exists(dstFile), true)
})
}
func Test_Pack_NestedDirectories(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
srcPath = gfile.Temp(guid.S())
dstPath = gfile.Temp(guid.S())
dstFile = filepath.Join(dstPath, "packed", "nested.go")
)
// Create source directory with nested structure
err := gfile.Mkdir(srcPath)
t.AssertNil(err)
defer gfile.Remove(srcPath)
err = gfile.Mkdir(dstPath)
t.AssertNil(err)
defer gfile.Remove(dstPath)
// Create nested directories and files
level1 := filepath.Join(srcPath, "level1")
level2 := filepath.Join(level1, "level2")
level3 := filepath.Join(level2, "level3")
err = gfile.Mkdir(level3)
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(srcPath, "root.txt"), "root")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(level1, "l1.txt"), "level1")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(level2, "l2.txt"), "level2")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(level3, "l3.txt"), "level3")
t.AssertNil(err)
// Create packed directory
err = gfile.Mkdir(filepath.Join(dstPath, "packed"))
t.AssertNil(err)
// Pack nested directories
_, err = Pack.Index(context.Background(), cPackInput{
Src: srcPath,
Dst: dstFile,
Name: "packed",
})
t.AssertNil(err)
// Verify output file exists
t.Assert(gfile.Exists(dstFile), true)
// Verify content includes all files
content := gfile.GetContents(dstFile)
t.Assert(gstr.Contains(content, "package packed"), true)
})
}

View File

@ -1,336 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package cmd
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_cRunApp_getWatchPaths_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
app := &cRunApp{
WatchPaths: []string{"."},
}
watchPaths := app.getWatchPaths()
t.AssertGT(len(watchPaths), 0)
for _, v := range watchPaths {
t.Log(v)
}
})
}
func Test_cRunApp_getWatchPaths_EmptyWatchPaths(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
app := &cRunApp{
WatchPaths: []string{},
}
watchPaths := app.getWatchPaths()
// Should default to current directory "."
t.AssertGT(len(watchPaths), 0)
})
}
func Test_cRunApp_getWatchPaths_CustomIgnorePattern(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
app := &cRunApp{
WatchPaths: []string{"testdata"},
IgnorePatterns: []string{"2572"},
}
watchPaths := app.getWatchPaths()
// Ensure the "2572" directory is not watched directly.
for _, wp := range watchPaths {
t.Log("watch path:", wp)
t.Assert(strings.HasSuffix(wp.Path, "2572"), false)
}
t.AssertGT(len(watchPaths), 0)
})
}
func Test_cRunApp_getWatchPaths_WithIgnoredDirectories(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a temporary directory structure for testing
tempDir := gfile.Temp("gf_run_test")
defer gfile.Remove(tempDir)
// Create directory structure:
// tempDir/
// ├── src/
// │ ├── api/
// │ └── internal/
// ├── vendor/ <-- ignored
// └── node_modules/ <-- ignored
gfile.Mkdir(filepath.Join(tempDir, "src", "api"))
gfile.Mkdir(filepath.Join(tempDir, "src", "internal"))
gfile.Mkdir(filepath.Join(tempDir, "vendor"))
gfile.Mkdir(filepath.Join(tempDir, "node_modules"))
app := &cRunApp{
WatchPaths: []string{tempDir},
}
watchPaths := app.getWatchPaths()
// Should watch tempDir non-recursively (to catch top-level files) and src recursively
t.Assert(len(watchPaths), 2)
// First path is tempDir (non-recursive)
t.Assert(watchPaths[0].Path, tempDir)
t.Assert(watchPaths[0].Recursive, false)
// Second path is src (recursive, since it has no ignored descendants)
t.Assert(watchPaths[1].Path, filepath.Join(tempDir, "src"))
t.Assert(watchPaths[1].Recursive, true)
})
}
func Test_cRunApp_getWatchPaths_NoIgnoredDirectories(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a temporary directory structure without ignored directories
tempDir := gfile.Temp("gf_run_test_no_ignore")
defer gfile.Remove(tempDir)
// Create directory structure without ignored patterns:
// tempDir/
// ├── src/
// │ ├── api/
// │ └── internal/
gfile.Mkdir(filepath.Join(tempDir, "src", "api"))
gfile.Mkdir(filepath.Join(tempDir, "src", "internal"))
app := &cRunApp{
WatchPaths: []string{tempDir},
}
watchPaths := app.getWatchPaths()
// Should watch the root directory recursively since no ignored directories exist
t.Assert(len(watchPaths), 1)
t.Assert(watchPaths[0].Path, tempDir)
t.Assert(watchPaths[0].Recursive, true)
})
}
func Test_cRunApp_getWatchPaths_CustomIgnorePatterns(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a temporary directory structure
tempDir := gfile.Temp("gf_run_test_custom_ignore")
defer gfile.Remove(tempDir)
// Create directory structure:
// tempDir/
// ├── src/
// │ ├── api/
// │ └── internal/
// ├── build/ <-- ignored
// └── dist/ <-- ignored
gfile.Mkdir(filepath.Join(tempDir, "src", "api"))
gfile.Mkdir(filepath.Join(tempDir, "src", "internal"))
gfile.Mkdir(filepath.Join(tempDir, "build"))
gfile.Mkdir(filepath.Join(tempDir, "dist"))
app := &cRunApp{
WatchPaths: []string{tempDir},
IgnorePatterns: []string{"build", "dist"},
}
watchPaths := app.getWatchPaths()
// Should watch tempDir non-recursively and src recursively
t.Assert(len(watchPaths), 2)
t.Assert(watchPaths[0].Path, tempDir)
t.Assert(watchPaths[0].Recursive, false)
t.Assert(watchPaths[1].Path, filepath.Join(tempDir, "src"))
t.Assert(watchPaths[1].Recursive, true)
})
}
func Test_cRunApp_getWatchPaths_DeepNestedStructure(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a deep nested directory structure
tempDir := gfile.Temp("gf_run_test_deep")
defer gfile.Remove(tempDir)
// Create deep directory structure:
// tempDir/
// ├── a/
// │ ├── b/
// │ │ └── c/
// │ └── vendor/ <-- ignored
// └── d/
gfile.Mkdir(filepath.Join(tempDir, "a", "b", "c"))
gfile.Mkdir(filepath.Join(tempDir, "a", "vendor"))
gfile.Mkdir(filepath.Join(tempDir, "d"))
app := &cRunApp{
WatchPaths: []string{tempDir},
}
watchPaths := app.getWatchPaths()
// Should watch individual valid directories due to ignored vendor directory
t.AssertGT(len(watchPaths), 0)
// Verify that vendor directory is not in watch list
for _, wp := range watchPaths {
t.Assert(strings.Contains(wp.Path, "vendor"), false)
}
})
}
func Test_cRunApp_getWatchPaths_MultipleRoots(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create multiple temporary directories
tempDir1 := gfile.Temp("gf_run_test_multi1")
tempDir2 := gfile.Temp("gf_run_test_multi2")
defer gfile.Remove(tempDir1)
defer gfile.Remove(tempDir2)
gfile.Mkdir(filepath.Join(tempDir1, "src"))
gfile.Mkdir(filepath.Join(tempDir2, "api"))
app := &cRunApp{
WatchPaths: []string{tempDir1, tempDir2},
}
watchPaths := app.getWatchPaths()
// Should watch both root directories recursively
t.Assert(len(watchPaths), 2)
// Both directories should be in the watch list
foundDir1, foundDir2 := false, false
for _, wp := range watchPaths {
if wp.Path == tempDir1 {
foundDir1 = true
t.Assert(wp.Recursive, true)
}
if wp.Path == tempDir2 {
foundDir2 = true
t.Assert(wp.Recursive, true)
}
}
t.Assert(foundDir1, true)
t.Assert(foundDir2, true)
})
}
func Test_cRunApp_getWatchPaths_NonExistentDirectory(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
app := &cRunApp{
WatchPaths: []string{"/non/existent/path"},
}
watchPaths := app.getWatchPaths()
// Should fall back to current directory when no valid paths found
t.AssertGT(len(watchPaths), 0)
// Should contain current directory
currentDir, _ := os.Getwd()
foundCurrentDir := false
for _, wp := range watchPaths {
if wp.Path == currentDir {
foundCurrentDir = true
break
}
}
t.Assert(foundCurrentDir, true)
})
}
func Test_isIgnoredDirName(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test default ignore patterns
t.Assert(isIgnoredDirName("node_modules", defaultIgnorePatterns), true)
t.Assert(isIgnoredDirName("vendor", defaultIgnorePatterns), true)
t.Assert(isIgnoredDirName(".git", defaultIgnorePatterns), true)
t.Assert(isIgnoredDirName("_private", defaultIgnorePatterns), true)
t.Assert(isIgnoredDirName("src", defaultIgnorePatterns), false)
t.Assert(isIgnoredDirName("api", defaultIgnorePatterns), false)
// Test custom ignore patterns
customPatterns := []string{"build", "dist", "*.tmp"}
t.Assert(isIgnoredDirName("build", customPatterns), true)
t.Assert(isIgnoredDirName("dist", customPatterns), true)
t.Assert(isIgnoredDirName("test.tmp", customPatterns), true)
t.Assert(isIgnoredDirName("src", customPatterns), false)
})
}
func Test_cRunApp_getWatchPaths_DeeplyNestedIgnore(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a temporary directory structure with deeply nested ignored directory
tempDir := gfile.Temp("gf_run_test_deeply_nested")
defer gfile.Remove(tempDir)
// Create directory structure:
// tempDir/
// ├── a/
// │ ├── b/
// │ │ ├── c/
// │ │ │ └── vendor/ <-- deeply nested ignored (4 levels)
// │ │ └── d/
// │ └── e/
// └── f/
gfile.Mkdir(filepath.Join(tempDir, "a", "b", "c", "vendor"))
gfile.Mkdir(filepath.Join(tempDir, "a", "b", "d"))
gfile.Mkdir(filepath.Join(tempDir, "a", "e"))
gfile.Mkdir(filepath.Join(tempDir, "f"))
app := &cRunApp{
WatchPaths: []string{tempDir},
}
watchPaths := app.getWatchPaths()
// Expected watch paths:
// 1. tempDir (non-recursive) - has ignored descendant
// 2. a (non-recursive) - has ignored descendant in b/c/vendor
// 3. b (non-recursive) - has ignored descendant in c/vendor
// 4. c (non-recursive) - has ignored child vendor
// 5. d (recursive) - no ignored descendants
// 6. e (recursive) - no ignored descendants
// 7. f (recursive) - no ignored descendants
t.AssertGT(len(watchPaths), 0)
// Verify vendor is not in watch paths
for _, wp := range watchPaths {
t.Assert(strings.Contains(wp.Path, "vendor"), false)
}
// Find specific paths and verify their recursive flags
foundF := false
for _, wp := range watchPaths {
if wp.Path == filepath.Join(tempDir, "f") {
foundF = true
t.Assert(wp.Recursive, true) // f should be recursive (no ignored descendants)
}
}
t.Assert(foundF, true)
})
}
func Test_cRunApp_getWatchPaths_EmptyDirectory(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create an empty temporary directory
tempDir := gfile.Temp("gf_run_test_empty")
defer gfile.Remove(tempDir)
gfile.Mkdir(tempDir)
app := &cRunApp{
WatchPaths: []string{tempDir},
}
watchPaths := app.getWatchPaths()
// Empty directory should be watched recursively (no ignored descendants)
t.Assert(len(watchPaths), 1)
t.Assert(watchPaths[0].Path, tempDir)
t.Assert(watchPaths[0].Recursive, true)
})
}

View File

@ -89,11 +89,28 @@ func (c CGenCtrl) Ctrl(ctx context.Context, in CGenCtrlInput) (out *CGenCtrlOutp
if !gfile.Exists(in.SrcFolder) {
mlog.Fatalf(`source folder path "%s" does not exist`, in.SrcFolder)
}
err = c.generateByModules(in)
// retrieve all api modules.
apiModuleFolderPaths, err := gfile.ScanDir(in.SrcFolder, "*", false)
if err != nil {
return nil, err
}
for _, apiModuleFolderPath := range apiModuleFolderPaths {
if !gfile.IsDir(apiModuleFolderPath) {
continue
}
// generate go files by api module.
var (
module = gfile.Basename(apiModuleFolderPath)
dstModuleFolderPath = gfile.Join(in.DstFolder, module)
)
err = c.generateByModule(
apiModuleFolderPath, dstModuleFolderPath, in.SdkPath,
in.SdkStdVersion, in.SdkNoV1, in.Clear, in.Merge,
)
if err != nil {
return nil, err
}
}
mlog.Print(`done!`)
return
@ -146,56 +163,6 @@ func (c CGenCtrl) generateByWatchFile(watchFile, sdkPath string, sdkStdVersion,
)
}
// generateByModules recursively calls generateByModule for multi-level modules generation.
func (c CGenCtrl) generateByModules(in CGenCtrlInput) (err error) {
// read root folder, example: api/user or api/app
moduleFolderPaths, err := gfile.ScanDir(in.SrcFolder, "*", false)
if err != nil {
return err
}
for _, moduleFolder := range moduleFolderPaths {
if !gfile.IsDir(moduleFolder) {
continue
}
// read children folder, example: api/user/v1 or api/app/user
childrenFolderPaths, err := gfile.ScanDir(moduleFolder, "*", false)
if err != nil {
return err
}
for _, childrenFolderPath := range childrenFolderPaths {
if !gfile.IsDir(childrenFolderPath) {
continue
}
var (
inCopy = in
module = gfile.Basename(moduleFolder)
)
inCopy.SrcFolder = gfile.Join(in.SrcFolder, module)
inCopy.DstFolder = gfile.Join(in.DstFolder, module)
err = c.generateByModules(inCopy)
if err != nil {
return err
}
}
// generate go files by api module.
var (
module = gfile.Basename(moduleFolder)
dstModuleFolderPath = gfile.Join(in.DstFolder, module)
)
err = c.generateByModule(
moduleFolder, dstModuleFolderPath, in.SdkPath,
in.SdkStdVersion, in.SdkNoV1, in.Clear, in.Merge,
)
if err != nil {
return err
}
}
return
}
// parseApiModule parses certain api and generate associated go files by certain module, not all api modules.
func (c CGenCtrl) generateByModule(
apiModuleFolderPath, dstModuleFolderPath, sdkPath string,

View File

@ -8,9 +8,6 @@ package genctrl
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"path/filepath"
"strings"
@ -147,8 +144,8 @@ func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, ite
"{MethodName}": item.MethodName,
"{MethodComment}": item.GetComment(),
})
// Use AST-based checking for more accurate method detection
if methodExists(methodFilePath, ctrlName, item.MethodName) {
if gstr.Contains(gfile.GetContents(methodFilePath), fmt.Sprintf(`func (c *%v) %v(`, ctrlName, item.MethodName)) {
return
}
if err = gfile.PutContentsAppend(methodFilePath, gstr.TrimLeft(content)); err != nil {
@ -173,6 +170,7 @@ func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, ite
// use -merge
func (c *controllerGenerator) doGenerateCtrlMergeItem(dstModuleFolderPath string, apiItems []apiItem, doneApiSet *gset.StrSet) (err error) {
type controllerFileItem struct {
module string
version string
@ -195,23 +193,13 @@ func (c *controllerGenerator) doGenerateCtrlMergeItem(dstModuleFolderPath string
ctrlFileItemMap[api.FileName] = ctrlFileItem
}
ctrlName := fmt.Sprintf(`Controller%s`, gstr.UcFirst(api.Version))
ctrl := gstr.TrimLeft(gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFuncMerge, g.MapStrStr{
"{Module}": api.Module,
"{CtrlName}": ctrlName,
"{CtrlName}": fmt.Sprintf(`Controller%s`, gstr.UcFirst(api.Version)),
"{Version}": api.Version,
"{MethodName}": api.MethodName,
"{MethodComment}": api.GetComment(),
}))
ctrlFilePath := gfile.Join(dstModuleFolderPath, fmt.Sprintf(
`%s_%s_%s.go`, ctrlFileItem.module, ctrlFileItem.version, api.FileName,
))
// Use AST-based checking for more accurate method detection
if methodExists(ctrlFilePath, ctrlName, api.MethodName) {
return
}
ctrlFileItem.controllers.WriteString(ctrl)
doneApiSet.Add(api.String())
}
@ -241,41 +229,3 @@ func (c *controllerGenerator) doGenerateCtrlMergeItem(dstModuleFolderPath string
}
return
}
// methodExists checks if a method with the given receiver type and name exists in the file.
// It uses AST parsing to accurately detect method definitions regardless of formatting.
// This handles various code formatting styles including multi-line method signatures.
func methodExists(filePath, ctrlName, methodName string) bool {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments)
if err != nil {
// If parsing fails (e.g., file doesn't exist or invalid syntax), return false
return false
}
for _, decl := range node.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
// Check if it's a method (has receiver)
if funcDecl.Recv != nil && len(funcDecl.Recv.List) > 0 {
// Extract receiver type name
// Handle both *T and T patterns
recvType := ""
switch t := funcDecl.Recv.List[0].Type.(type) {
case *ast.StarExpr:
if ident, ok := t.X.(*ast.Ident); ok {
recvType = ident.Name
}
case *ast.Ident:
recvType = t.Name
}
// Check if both receiver type and method name match
if recvType == ctrlName && funcDecl.Name.Name == methodName {
return true
}
}
}
return false
}

View File

@ -9,7 +9,6 @@ package gendao
import (
"context"
"fmt"
"sort"
"strings"
"github.com/olekukonko/tablewriter"
@ -105,10 +104,6 @@ var (
"smallmoney": {
Type: "float64",
},
"uuid": {
Type: "uuid.UUID",
Import: "github.com/google/uuid",
},
}
// tablewriter Options
@ -188,27 +183,7 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
var tableNames []string
if in.Tables != "" {
inputTables := gstr.SplitAndTrim(in.Tables, ",")
// Check if any table pattern contains wildcard characters.
// https://github.com/gogf/gf/issues/4629
var hasPattern bool
for _, t := range inputTables {
if containsWildcard(t) {
hasPattern = true
break
}
}
if hasPattern {
// Fetch all tables first, then filter by patterns.
allTables, err := db.Tables(context.TODO())
if err != nil {
mlog.Fatalf("fetching tables failed: %+v", err)
}
tableNames = filterTablesByPatterns(allTables, inputTables)
} else {
// Use exact table names as before.
tableNames = inputTables
}
tableNames = gstr.SplitAndTrim(in.Tables, ",")
} else {
tableNames, err = db.Tables(context.TODO())
if err != nil {
@ -219,11 +194,22 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
if in.TablesEx != "" {
array := garray.NewStrArrayFrom(tableNames)
for _, p := range gstr.SplitAndTrim(in.TablesEx, ",") {
if containsWildcard(p) {
// Use exact match with ^ and $ anchors for consistency with tables pattern.
regPattern := "^" + patternToRegex(p) + "$"
if gstr.Contains(p, "*") || gstr.Contains(p, "?") {
p = gstr.ReplaceByMap(p, map[string]string{
"\r": "",
"\n": "",
})
p = gstr.ReplaceByMap(p, map[string]string{
"*": "\r",
"?": "\n",
})
p = gregex.Quote(p)
p = gstr.ReplaceByMap(p, map[string]string{
"\r": ".*",
"\n": ".",
})
for _, v := range array.Clone().Slice() {
if gregex.IsMatchString(regPattern, v) {
if gregex.IsMatchString(p, v) {
array.RemoveValue(v)
}
}
@ -250,22 +236,13 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
newTableNames = make([]string, len(tableNames))
shardingNewTableSet = gset.NewStrSet()
)
// Sort sharding patterns by length descending, so that longer (more specific) patterns
// are matched first. This prevents shorter patterns like "a_?" from incorrectly matching
// tables that should match longer patterns like "a_b_?" or "a_c_?".
// https://github.com/gogf/gf/issues/4603
sortedShardingPatterns := make([]string, len(in.ShardingPattern))
copy(sortedShardingPatterns, in.ShardingPattern)
sort.Slice(sortedShardingPatterns, func(i, j int) bool {
return len(sortedShardingPatterns[i]) > len(sortedShardingPatterns[j])
})
for i, tableName := range tableNames {
newTableName := tableName
for _, v := range removePrefixArray {
newTableName = gstr.TrimLeftStr(newTableName, v, 1)
}
if len(sortedShardingPatterns) > 0 {
for _, pattern := range sortedShardingPatterns {
if len(in.ShardingPattern) > 0 {
for _, pattern := range in.ShardingPattern {
var (
match []string
regPattern = gstr.Replace(pattern, "?", `(.+)`)
@ -281,11 +258,10 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
newTableName = gstr.Trim(newTableName, `_.-`)
if shardingNewTableSet.Contains(newTableName) {
tableNames[i] = ""
break
continue
}
// Add prefix to sharding table name, if not, the isSharding check would not match.
shardingNewTableSet.Add(in.Prefix + newTableName)
break
}
}
newTableName = in.Prefix + newTableName
@ -431,61 +407,3 @@ func getTemplateFromPathOrDefault(filePath string, def string) string {
}
return def
}
// containsWildcard checks if the pattern contains wildcard characters (* or ?).
func containsWildcard(pattern string) bool {
return gstr.Contains(pattern, "*") || gstr.Contains(pattern, "?")
}
// patternToRegex converts a wildcard pattern to a regex pattern.
// Wildcard characters: * matches any characters, ? matches single character.
func patternToRegex(pattern string) string {
pattern = gstr.ReplaceByMap(pattern, map[string]string{
"\r": "",
"\n": "",
})
pattern = gstr.ReplaceByMap(pattern, map[string]string{
"*": "\r",
"?": "\n",
})
pattern = gregex.Quote(pattern)
pattern = gstr.ReplaceByMap(pattern, map[string]string{
"\r": ".*",
"\n": ".",
})
return pattern
}
// filterTablesByPatterns filters tables by given patterns.
// Patterns support wildcard characters: * matches any characters, ? matches single character.
// https://github.com/gogf/gf/issues/4629
func filterTablesByPatterns(allTables []string, patterns []string) []string {
var result []string
matched := make(map[string]bool)
allTablesSet := make(map[string]bool)
for _, t := range allTables {
allTablesSet[t] = true
}
for _, p := range patterns {
if containsWildcard(p) {
regPattern := "^" + patternToRegex(p) + "$"
for _, table := range allTables {
if !matched[table] && gregex.IsMatchString(regPattern, table) {
result = append(result, table)
matched[table] = true
}
}
} else {
// Exact table name, use direct string comparison.
if !allTablesSet[p] {
mlog.Printf(`table "%s" does not exist, skipped`, p)
continue
}
if !matched[p] {
result = append(result, p)
matched[p] = true
}
}
}
return result
}

View File

@ -98,6 +98,7 @@ func generateStructFieldDefinition(
err error
localTypeName gdb.LocalType
localTypeNameStr string
jsonTag = gstr.CaseConvert(field.Name, gstr.CaseTypeMatch(in.JsonCase))
)
if in.TypeMapping != nil && len(in.TypeMapping) > 0 {
@ -155,8 +156,6 @@ func generateStructFieldDefinition(
" #" + formatFieldName(newFiledName, FieldNameCaseCamel),
" #" + localTypeNameStr,
}
jsonTag := gstr.CaseConvert(newFiledName, gstr.CaseTypeMatch(in.JsonCase))
attrLines = append(attrLines, fmt.Sprintf(` #%sjson:"%s"`, tagKey, jsonTag))
// orm tag
if !in.IsDo {

View File

@ -1,182 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gendao
import (
"testing"
"github.com/gogf/gf/v2/test/gtest"
)
// Test containsWildcard function.
func Test_containsWildcard(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(containsWildcard("trade_*"), true)
t.Assert(containsWildcard("user_?"), true)
t.Assert(containsWildcard("*"), true)
t.Assert(containsWildcard("?"), true)
t.Assert(containsWildcard("trade_order"), false)
t.Assert(containsWildcard(""), false)
})
}
// Test patternToRegex function.
func Test_patternToRegex(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// * should become .*
t.Assert(patternToRegex("trade_*"), "trade_.*")
// ? should become .
t.Assert(patternToRegex("user_???"), "user_...")
// Mixed
t.Assert(patternToRegex("*_order_?"), ".*_order_.")
// No wildcards - should escape special regex chars
t.Assert(patternToRegex("trade_order"), "trade_order")
// Just *
t.Assert(patternToRegex("*"), ".*")
})
}
// Test filterTablesByPatterns with * wildcard.
func Test_filterTablesByPatterns_Star(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info", "user_log", "config"}
// Single pattern with *
result := filterTablesByPatterns(allTables, []string{"trade_*"})
t.Assert(len(result), 2)
t.AssertIN("trade_order", result)
t.AssertIN("trade_item", result)
// Multiple patterns with *
result = filterTablesByPatterns(allTables, []string{"trade_*", "user_*"})
t.Assert(len(result), 4)
t.AssertIN("trade_order", result)
t.AssertIN("trade_item", result)
t.AssertIN("user_info", result)
t.AssertIN("user_log", result)
})
}
// Test filterTablesByPatterns with ? wildcard.
func Test_filterTablesByPatterns_Question(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info", "user_log", "config"}
// ? matches single character: user_log (3 chars) but not user_info (4 chars)
result := filterTablesByPatterns(allTables, []string{"user_???"})
t.Assert(len(result), 1)
t.AssertIN("user_log", result)
t.AssertNI("user_info", result)
// user_???? should match user_info (4 chars)
result = filterTablesByPatterns(allTables, []string{"user_????"})
t.Assert(len(result), 1)
t.AssertIN("user_info", result)
t.AssertNI("user_log", result)
})
}
// Test filterTablesByPatterns with mixed patterns and exact names.
func Test_filterTablesByPatterns_Mixed(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info", "user_log", "config"}
// Pattern + exact name
result := filterTablesByPatterns(allTables, []string{"trade_*", "config"})
t.Assert(len(result), 3)
t.AssertIN("trade_order", result)
t.AssertIN("trade_item", result)
t.AssertIN("config", result)
t.AssertNI("user_info", result)
t.AssertNI("user_log", result)
})
}
// Test filterTablesByPatterns with exact names only (backward compatibility).
func Test_filterTablesByPatterns_ExactNames(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info", "user_log", "config"}
// Exact names only
result := filterTablesByPatterns(allTables, []string{"trade_order", "config"})
t.Assert(len(result), 2)
t.AssertIN("trade_order", result)
t.AssertIN("config", result)
t.AssertNI("trade_item", result)
})
}
// Test filterTablesByPatterns - no duplicates when table matches multiple patterns.
func Test_filterTablesByPatterns_NoDuplicates(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info"}
// trade_order matches both patterns, should only appear once
result := filterTablesByPatterns(allTables, []string{"trade_*", "trade_order"})
t.Assert(len(result), 2) // trade_order, trade_item
// Count occurrences of trade_order
count := 0
for _, v := range result {
if v == "trade_order" {
count++
}
}
t.Assert(count, 1) // No duplicates
})
}
// Test filterTablesByPatterns - pattern matches nothing.
func Test_filterTablesByPatterns_NoMatch(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info"}
// Pattern that matches nothing
result := filterTablesByPatterns(allTables, []string{"nonexistent_*"})
t.Assert(len(result), 0)
})
}
// Test filterTablesByPatterns - empty input.
func Test_filterTablesByPatterns_Empty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item"}
// Empty patterns
result := filterTablesByPatterns(allTables, []string{})
t.Assert(len(result), 0)
// Empty tables
result = filterTablesByPatterns([]string{}, []string{"trade_*"})
t.Assert(len(result), 0)
})
}
// Test filterTablesByPatterns - "*" matches all tables.
func Test_filterTablesByPatterns_MatchAll(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info", "user_log", "config"}
// "*" should match all tables
result := filterTablesByPatterns(allTables, []string{"*"})
t.Assert(len(result), 5)
})
}
// Test filterTablesByPatterns - non-existent exact table name should be skipped.
func Test_filterTablesByPatterns_NonExistent(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
allTables := []string{"trade_order", "trade_item", "user_info"}
// Mix of existing and non-existing tables
result := filterTablesByPatterns(allTables, []string{"trade_order", "nonexistent", "user_info"})
t.Assert(len(result), 2)
t.AssertIN("trade_order", result)
t.AssertIN("user_info", result)
t.AssertNI("nonexistent", result)
})
}

View File

@ -55,13 +55,6 @@ func (c CGenEnums) Enums(ctx context.Context, in CGenEnumsInput) (out *CGenEnums
if realPath == "" {
mlog.Fatalf(`source folder path "%s" does not exist`, in.Src)
}
// Convert output path to absolute before Chdir, so it remains correct after directory change.
// See: https://github.com/gogf/gf/issues/4387
outputPath := gfile.Abs(in.Path)
originPwd := gfile.Pwd()
defer gfile.Chdir(originPwd)
err = gfile.Chdir(realPath)
if err != nil {
mlog.Fatal(err)
@ -79,14 +72,14 @@ func (c CGenEnums) Enums(ctx context.Context, in CGenEnumsInput) (out *CGenEnums
p := NewEnumsParser(in.Prefixes)
p.ParsePackages(pkgs)
var enumsContent = gstr.ReplaceByMap(consts.TemplateGenEnums, g.MapStrStr{
"{PackageName}": gfile.Basename(gfile.Dir(outputPath)),
"{PackageName}": gfile.Basename(gfile.Dir(in.Path)),
"{EnumsJson}": "`" + p.Export() + "`",
})
enumsContent = gstr.Trim(enumsContent)
if err = gfile.PutContents(outputPath, enumsContent); err != nil {
if err = gfile.PutContents(in.Path, enumsContent); err != nil {
return
}
mlog.Printf(`generated enums go file: %s`, outputPath)
mlog.Printf(`generated enums go file: %s`, in.Path)
mlog.Print("done!")
return
}

View File

@ -1,368 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package genenums
import (
"go/constant"
"path/filepath"
"testing"
"golang.org/x/tools/go/packages"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)
func Test_NewEnumsParser(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test creating parser without prefixes
p := NewEnumsParser(nil)
t.AssertNE(p, nil)
t.Assert(len(p.enums), 0)
t.Assert(len(p.prefixes), 0)
t.AssertNE(p.parsedPkg, nil)
t.AssertNE(p.standardPackages, nil)
})
}
func Test_NewEnumsParser_WithPrefixes(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test creating parser with prefixes
prefixes := []string{"github.com/gogf", "github.com/test"}
p := NewEnumsParser(prefixes)
t.AssertNE(p, nil)
t.Assert(len(p.prefixes), 2)
t.Assert(p.prefixes[0], "github.com/gogf")
t.Assert(p.prefixes[1], "github.com/test")
})
}
func Test_EnumsParser_Export_Empty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test exporting empty enums
p := NewEnumsParser(nil)
result := p.Export()
t.Assert(result, "{}")
})
}
func Test_EnumsParser_Export_WithEnums(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test exporting with manually added enums
p := NewEnumsParser(nil)
// Add some test enums
p.enums = []EnumItem{
{
Name: "StatusActive",
Value: "1",
Type: "pkg.Status",
Kind: constant.Int,
},
{
Name: "StatusInactive",
Value: "0",
Type: "pkg.Status",
Kind: constant.Int,
},
{
Name: "TypeA",
Value: "type_a",
Type: "pkg.Type",
Kind: constant.String,
},
}
result := p.Export()
t.AssertNE(result, "")
// Parse the result to verify - use raw map to avoid gjson path issues with "."
var resultMap map[string][]interface{}
err := gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
// Verify Status type has 2 values
statusValues := resultMap["pkg.Status"]
t.Assert(len(statusValues), 2)
// Verify Type type has 1 value
typeValues := resultMap["pkg.Type"]
t.Assert(len(typeValues), 1)
t.Assert(typeValues[0], "type_a")
})
}
func Test_EnumsParser_Export_IntValues(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p := NewEnumsParser(nil)
p.enums = []EnumItem{
{Name: "One", Value: "1", Type: "pkg.Int", Kind: constant.Int},
{Name: "Two", Value: "2", Type: "pkg.Int", Kind: constant.Int},
{Name: "Negative", Value: "-5", Type: "pkg.Int", Kind: constant.Int},
}
result := p.Export()
var resultMap map[string][]interface{}
err := gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
values := resultMap["pkg.Int"]
t.Assert(len(values), 3)
// Int values should be exported as integers (stored as float64 in JSON)
t.Assert(values[0], float64(1))
t.Assert(values[1], float64(2))
t.Assert(values[2], float64(-5))
})
}
func Test_EnumsParser_Export_FloatValues(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p := NewEnumsParser(nil)
p.enums = []EnumItem{
{Name: "Pi", Value: "3.14159", Type: "pkg.Float", Kind: constant.Float},
{Name: "E", Value: "2.71828", Type: "pkg.Float", Kind: constant.Float},
}
result := p.Export()
var resultMap map[string][]interface{}
err := gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
values := resultMap["pkg.Float"]
t.Assert(len(values), 2)
})
}
func Test_EnumsParser_Export_BoolValues(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p := NewEnumsParser(nil)
p.enums = []EnumItem{
{Name: "True", Value: "true", Type: "pkg.Bool", Kind: constant.Bool},
{Name: "False", Value: "false", Type: "pkg.Bool", Kind: constant.Bool},
}
result := p.Export()
var resultMap map[string][]interface{}
err := gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
values := resultMap["pkg.Bool"]
t.Assert(len(values), 2)
t.Assert(values[0], true)
t.Assert(values[1], false)
})
}
func Test_EnumsParser_Export_StringValues(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p := NewEnumsParser(nil)
p.enums = []EnumItem{
{Name: "Hello", Value: "hello", Type: "pkg.Str", Kind: constant.String},
{Name: "World", Value: "world", Type: "pkg.Str", Kind: constant.String},
}
result := p.Export()
var resultMap map[string][]interface{}
err := gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
values := resultMap["pkg.Str"]
t.Assert(len(values), 2)
t.Assert(values[0], "hello")
t.Assert(values[1], "world")
})
}
func Test_EnumsParser_Export_MixedTypes(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
p := NewEnumsParser(nil)
p.enums = []EnumItem{
{Name: "IntVal", Value: "42", Type: "pkg.IntType", Kind: constant.Int},
{Name: "StrVal", Value: "test", Type: "pkg.StrType", Kind: constant.String},
{Name: "BoolVal", Value: "true", Type: "pkg.BoolType", Kind: constant.Bool},
}
result := p.Export()
var resultMap map[string][]interface{}
err := gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
// Each type should have its own array
t.Assert(len(resultMap["pkg.IntType"]), 1)
t.Assert(len(resultMap["pkg.StrType"]), 1)
t.Assert(len(resultMap["pkg.BoolType"]), 1)
})
}
func Test_EnumItem_Structure(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test EnumItem structure
item := EnumItem{
Name: "TestEnum",
Value: "test_value",
Type: "github.com/test/pkg.EnumType",
Kind: constant.String,
}
t.Assert(item.Name, "TestEnum")
t.Assert(item.Value, "test_value")
t.Assert(item.Type, "github.com/test/pkg.EnumType")
t.Assert(item.Kind, constant.String)
})
}
func Test_EnumsParser_ParsePackages_Integration(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a temporary directory with a Go package containing enums
// Note: The module path must contain "/" for enums to be parsed
// (the parser skips std types without "/" in the type name)
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Create go.mod with a path containing "/"
goModContent := `module github.com/test/enumtest
go 1.21
`
err = gfile.PutContents(filepath.Join(tempDir, "go.mod"), goModContent)
t.AssertNil(err)
// Create a Go file with enum definitions
enumsContent := `package enumtest
type Status int
const (
StatusActive Status = 1
StatusInactive Status = 0
)
type Color string
const (
ColorRed Color = "red"
ColorGreen Color = "green"
ColorBlue Color = "blue"
)
`
err = gfile.PutContents(filepath.Join(tempDir, "enums.go"), enumsContent)
t.AssertNil(err)
// Load the package
cfg := &packages.Config{
Dir: tempDir,
Mode: pkgLoadMode,
Tests: false,
}
pkgs, err := packages.Load(cfg)
t.AssertNil(err)
t.Assert(len(pkgs) > 0, true)
// Parse the packages
p := NewEnumsParser(nil)
p.ParsePackages(pkgs)
// Export and verify - result should contain parsed enums
result := p.Export()
// Verify the export contains some data
t.Assert(len(result) > 2, true) // More than just "{}"
// Parse result as raw map to handle keys with "/"
var resultMap map[string][]interface{}
err = gjson.DecodeTo(result, &resultMap)
t.AssertNil(err)
// Verify Status enum was parsed (type will be "github.com/test/enumtest.Status")
statusKey := "github.com/test/enumtest.Status"
statusValues, hasStatus := resultMap[statusKey]
t.Assert(hasStatus, true)
t.Assert(len(statusValues), 2)
// Verify Color enum was parsed
colorKey := "github.com/test/enumtest.Color"
colorValues, hasColor := resultMap[colorKey]
t.Assert(hasColor, true)
t.Assert(len(colorValues), 3)
})
}
func Test_EnumsParser_ParsePackages_WithPrefixes(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create a temporary directory with a Go package
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Create go.mod with a specific module name
goModContent := `module github.com/allowed/pkg
go 1.21
`
err = gfile.PutContents(filepath.Join(tempDir, "go.mod"), goModContent)
t.AssertNil(err)
// Create a Go file with enum definitions
enumsContent := `package pkg
type Status int
const (
StatusOK Status = 1
)
`
err = gfile.PutContents(filepath.Join(tempDir, "enums.go"), enumsContent)
t.AssertNil(err)
// Load the package
cfg := &packages.Config{
Dir: tempDir,
Mode: pkgLoadMode,
Tests: false,
}
pkgs, err := packages.Load(cfg)
t.AssertNil(err)
// Parse with prefix filter that matches
p := NewEnumsParser([]string{"github.com/allowed"})
p.ParsePackages(pkgs)
result := p.Export()
// Should have enums because prefix matches
t.AssertNE(result, "{}")
// Parse with prefix filter that doesn't match
p2 := NewEnumsParser([]string{"github.com/other"})
p2.ParsePackages(pkgs)
result2 := p2.Export()
// Should be empty because prefix doesn't match
t.Assert(result2, "{}")
})
}
func Test_getStandardPackages(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
stdPkgs := getStandardPackages()
t.AssertNE(stdPkgs, nil)
t.Assert(len(stdPkgs) > 0, true)
// Verify some common standard packages are included
_, hasFmt := stdPkgs["fmt"]
t.Assert(hasFmt, true)
_, hasOs := stdPkgs["os"]
t.Assert(hasOs, true)
_, hasContext := stdPkgs["context"]
t.Assert(hasContext, true)
})
}

View File

@ -1,269 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"path/filepath"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// ProcessOptions contains options for the Process function
type ProcessOptions struct {
SelectVersion bool // Enable interactive version selection
ModulePath string // Custom go.mod module path (e.g., github.com/xxx/xxx)
UpgradeDeps bool // Upgrade dependencies to latest (go get -u ./...)
}
// Process handles the template generation flow from remote repository
func Process(ctx context.Context, repo, name string, opts *ProcessOptions) error {
if opts == nil {
opts = &ProcessOptions{}
}
// 0. Check Go environment first
mlog.Print("Checking Go environment...")
goEnv, err := CheckGoEnv(ctx)
if err != nil {
mlog.Printf("Go environment check failed: %v", err)
return err
}
mlog.Printf("Go environment OK (version: %s)", goEnv.GOVERSION)
// Check if this is a git subdirectory URL
if IsSubdirRepo(repo) {
return processGitSubdir(ctx, repo, name, opts)
}
// Try Go module download first, fallback to git subdirectory if it fails
// This handles edge cases where the heuristic may be incorrect
err = processGoModule(ctx, repo, name, opts)
if err != nil {
mlog.Printf("Go module download failed, trying git subdirectory mode: %v", err)
mlog.Print("Note: If this is a git subdirectory, you can force git mode by using a full git URL")
// If Go module download fails, try git subdirectory as fallback
// This handles cases where the heuristic incorrectly classified a git subdir as Go module
if IsSubdirRepo(repo) {
mlog.Print("Falling back to git subdirectory download...")
return processGitSubdir(ctx, repo, name, opts)
}
}
return err
}
// processGoModule handles standard Go module download via go get
func processGoModule(ctx context.Context, repo, name string, opts *ProcessOptions) error {
// Extract module path (without version)
modulePath := repo
specifiedVersion := ""
if gstr.Contains(repo, "@") {
parts := gstr.Split(repo, "@")
modulePath = parts[0]
specifiedVersion = parts[1]
}
// Default name to repo basename if empty
if name == "" {
name = filepath.Base(modulePath)
}
// Determine the target module path for go.mod
targetModulePath := name
if opts.ModulePath != "" {
targetModulePath = opts.ModulePath
}
// 1. Determine version to use
var targetVersion string
if specifiedVersion != "" {
// User specified version, try to use it first
targetVersion = specifiedVersion
mlog.Printf("Using specified version: %s", targetVersion)
} else if opts.SelectVersion {
// Interactive version selection
mlog.Print("Fetching available versions...")
versionInfo, err := GetModuleVersions(ctx, modulePath)
if err != nil {
mlog.Printf("Failed to get versions: %v", err)
return err
}
targetVersion, err = SelectVersion(ctx, versionInfo.Versions, modulePath)
if err != nil {
mlog.Printf("Version selection failed: %v", err)
return err
}
} else {
// Default: use latest version
mlog.Print("Fetching latest version...")
latest, err := GetLatestVersion(ctx, modulePath)
if err != nil {
mlog.Printf("Failed to get latest version, will try @latest tag: %v", err)
targetVersion = "latest"
} else {
targetVersion = latest
mlog.Printf("Latest version: %s", targetVersion)
}
}
// 2. Download Template with determined version
repoWithVersion := modulePath + "@" + targetVersion
srcDir, err := downloadTemplate(ctx, repoWithVersion)
if err != nil {
// If specified version download failed, offer to select from available versions
if specifiedVersion != "" {
mlog.Printf("Failed to download specified version '%s': %v", specifiedVersion, err)
mlog.Print("Fetching available versions...")
versionInfo, verErr := GetModuleVersions(ctx, modulePath)
if verErr != nil {
mlog.Printf("Failed to get available versions: %v", verErr)
return err // Return original download error
}
if len(versionInfo.Versions) == 0 {
mlog.Print("No versions available for this module")
return err
}
// Let user select from available versions
selectedVersion, selErr := SelectVersion(ctx, versionInfo.Versions, modulePath)
if selErr != nil {
mlog.Printf("Version selection failed: %v", selErr)
return selErr
}
// Retry download with selected version
targetVersion = selectedVersion
repoWithVersion = modulePath + "@" + targetVersion
srcDir, err = downloadTemplate(ctx, repoWithVersion)
if err != nil {
mlog.Printf("Download failed: %v", err)
return err
}
} else {
mlog.Printf("Download failed: %v", err)
return err
}
}
mlog.Debugf("Template located at: %s", srcDir)
// 3. Generate Project
if err := generateProject(ctx, srcDir, name, modulePath, targetModulePath); err != nil {
mlog.Printf("Generation failed: %v", err)
return err
}
// 4. Handle dependencies
var projectDir string
if name == "." {
projectDir = gfile.Pwd()
} else {
projectDir = filepath.Join(gfile.Pwd(), name)
}
if opts.UpgradeDeps {
// Upgrade all dependencies to latest
if err := upgradeDependencies(ctx, projectDir); err != nil {
mlog.Printf("Failed to upgrade dependencies: %v", err)
}
} else {
// Default: just tidy dependencies
if err := tidyDependencies(ctx, projectDir); err != nil {
mlog.Printf("Failed to tidy dependencies: %v", err)
}
}
return nil
}
// processGitSubdir handles git subdirectory download via sparse checkout
func processGitSubdir(ctx context.Context, repo, name string, opts *ProcessOptions) error {
mlog.Print("Detected subdirectory URL, using git sparse checkout...")
// Check if git is available
gitVersion, err := CheckGitEnv(ctx)
if err != nil {
mlog.Printf("Git is required for subdirectory templates: %v", err)
return err
}
mlog.Printf("Git available (%s)", gitVersion)
// Download via git sparse checkout
srcDir, gitInfo, err := downloadGitSubdir(ctx, repo)
if err != nil {
mlog.Printf("Git download failed: %v", err)
return err
}
// Clean up temp directory after generation
// The temp dir is parent of parent of srcDir (tempDir/repo/subpath)
tempDir := filepath.Dir(filepath.Dir(srcDir))
if tempDir != "" && gfile.Exists(tempDir) && gstr.Contains(tempDir, "gf-init-git") {
defer func() {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory %s: %v", tempDir, err)
} else {
mlog.Debugf("Cleaned up temp directory: %s", tempDir)
}
}()
}
// Default name to subpath basename if empty
if name == "" {
name = filepath.Base(gitInfo.SubPath)
}
// Get original module name from go.mod (might be "main" or something else)
oldModule := GetModuleNameFromGoMod(srcDir)
if oldModule == "" {
// Fallback: construct from git info
oldModule = gitInfo.Host + "/" + gitInfo.Owner + "/" + gitInfo.Repo + "/" + gitInfo.SubPath
}
// Determine the target module path for go.mod
targetModulePath := name
if opts.ModulePath != "" {
targetModulePath = opts.ModulePath
}
mlog.Debugf("Template located at: %s", srcDir)
mlog.Debugf("Original module: %s", oldModule)
// Generate Project
if err := generateProject(ctx, srcDir, name, oldModule, targetModulePath); err != nil {
mlog.Printf("Generation failed: %v", err)
return err
}
// Handle dependencies
var projectDir string
if name == "." {
projectDir = gfile.Pwd()
} else {
projectDir = filepath.Join(gfile.Pwd(), name)
}
if opts.UpgradeDeps {
// Upgrade all dependencies to latest
if err := upgradeDependencies(ctx, projectDir); err != nil {
mlog.Printf("Failed to upgrade dependencies: %v", err)
}
} else {
// Default: just tidy dependencies
if err := tidyDependencies(ctx, projectDir); err != nil {
mlog.Printf("Failed to tidy dependencies: %v", err)
}
}
return nil
}

View File

@ -1,125 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"bytes"
"context"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"os"
"path/filepath"
"strings"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// ASTReplacer handles import path replacement using Go AST
type ASTReplacer struct {
oldModule string
newModule string
fset *token.FileSet
}
// NewASTReplacer creates a new AST-based import replacer
func NewASTReplacer(oldModule, newModule string) *ASTReplacer {
return &ASTReplacer{
oldModule: oldModule,
newModule: newModule,
fset: token.NewFileSet(),
}
}
// ReplaceInFile replaces import paths in a single Go file
func (r *ASTReplacer) ReplaceInFile(ctx context.Context, filePath string) error {
// Read file content
content := gfile.GetContents(filePath)
if content == "" {
return nil
}
// Parse the file
file, err := parser.ParseFile(r.fset, filePath, content, parser.ParseComments)
if err != nil {
mlog.Debugf("Failed to parse %s: %v", filePath, err)
return nil // Skip files that can't be parsed
}
// Track if any changes were made
changed := false
// Traverse and modify imports
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.ImportSpec:
if x.Path != nil {
importPath := strings.Trim(x.Path.Value, `"`)
if strings.HasPrefix(importPath, r.oldModule) {
// Replace only the leading module prefix for clarity and correctness.
newPath := r.newModule + strings.TrimPrefix(importPath, r.oldModule)
x.Path.Value = `"` + newPath + `"`
changed = true
mlog.Debugf("Replaced import: %s -> %s in %s", importPath, newPath, filePath)
}
}
}
return true
})
if !changed {
return nil
}
// Write back to file without formatting.
// Formatting will be handled by formatGoFiles after all replacements are done.
var buf bytes.Buffer
if err := printer.Fprint(&buf, r.fset, file); err != nil {
return err
}
return gfile.PutContents(filePath, buf.String())
}
// ReplaceInDir replaces import paths in all Go files in a directory (recursively)
func (r *ASTReplacer) ReplaceInDir(ctx context.Context, dir string) error {
mlog.Printf("Replacing imports: %s -> %s", r.oldModule, r.newModule)
// Find all .go files
files, err := findGoFiles(dir)
if err != nil {
return err
}
for _, file := range files {
if err := r.ReplaceInFile(ctx, file); err != nil {
mlog.Printf("Failed to process %s: %v", file, err)
}
}
return nil
}
// findGoFiles recursively finds all .go files in a directory
func findGoFiles(dir string) ([]string, error) {
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".go") {
files = append(files, path)
}
return nil
})
return files, err
}

View File

@ -1,111 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// downloadTemplate fetches the remote repository using go get
func downloadTemplate(ctx context.Context, repo string) (string, error) {
// 1. Create a temporary directory workspace
tempDir := gfile.Temp("gf-init-cli")
if tempDir == "" {
return "", fmt.Errorf("failed to create temporary directory")
}
if err := gfile.Mkdir(tempDir); err != nil {
return "", err
}
defer func() {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory %s: %v", tempDir, err)
}
}() // Clean up the temp workspace
mlog.Debugf("Using temp workspace: %s", tempDir)
// 2. Initialize a temp go module to perform go get
// We run commands inside the temp directory
if err := runCmd(ctx, tempDir, "go", "mod", "init", "temp"); err != nil {
return "", err
}
// 3. Run go get <repo>
// Try different version strategies: original -> @latest -> @master
moduleName := repo
if gstr.Contains(repo, "@") {
moduleName = gstr.Split(repo, "@")[0]
}
var downloadErrs []string
versionsToTry := []string{repo}
if !gstr.Contains(repo, "@") {
versionsToTry = append(versionsToTry, repo+"@latest", repo+"@master")
}
var successRepo string
for _, tryRepo := range versionsToTry {
mlog.Printf("Downloading template %s...", tryRepo)
if err := runCmd(ctx, tempDir, "go", "get", tryRepo); err == nil {
successRepo = tryRepo
break
} else {
downloadErrs = append(downloadErrs, fmt.Sprintf("%s: %v", tryRepo, err))
mlog.Debugf("Failed to download %s, trying next...", tryRepo)
}
}
if successRepo == "" {
errMsg := "all download attempts failed"
if len(downloadErrs) > 0 {
errMsg = strings.Join(downloadErrs, "; ")
}
return "", fmt.Errorf("failed to download repo %s: %s", repo, errMsg)
}
// 4. Find the local path using go list -m -json <repo>
listCmd := exec.CommandContext(ctx, "go", "list", "-m", "-json", moduleName)
listCmd.Dir = tempDir
output, err := listCmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return "", fmt.Errorf("go list failed: %s", string(exitErr.Stderr))
}
return "", fmt.Errorf("failed to locate module path: %w", err)
}
var modInfo struct {
Dir string `json:"Dir"`
}
if err := json.Unmarshal(output, &modInfo); err != nil {
return "", fmt.Errorf("failed to parse go list output: %w", err)
}
if modInfo.Dir == "" {
return "", fmt.Errorf("module directory not found for %s", repo)
}
return modInfo.Dir, nil
}
func runCmd(ctx context.Context, dir string, name string, args ...string) error {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

View File

@ -1,90 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// GoEnv represents Go environment variables
type GoEnv struct {
GOVERSION string `json:"GOVERSION"`
GOROOT string `json:"GOROOT"`
GOPATH string `json:"GOPATH"`
GOMODCACHE string `json:"GOMODCACHE"`
GOPROXY string `json:"GOPROXY"`
GO111MODULE string `json:"GO111MODULE"`
}
// CheckGoEnv verifies Go is installed and properly configured
func CheckGoEnv(ctx context.Context) (*GoEnv, error) {
// 1. Check if go binary exists
goPath, err := exec.LookPath("go")
if err != nil {
return nil, fmt.Errorf("go is not installed or not in PATH: %w", err)
}
mlog.Debugf("Found go binary at: %s", goPath)
// 2. Get go env as JSON
cmd := exec.CommandContext(ctx, "go", "env", "-json")
output, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("go env failed: %s", string(exitErr.Stderr))
}
return nil, fmt.Errorf("failed to run go env: %w", err)
}
// 3. Parse JSON output
var env GoEnv
if err := json.Unmarshal(output, &env); err != nil {
return nil, fmt.Errorf("failed to parse go env output: %w", err)
}
// 4. Validate critical environment variables
if env.GOROOT == "" {
return nil, fmt.Errorf("GOROOT is not set")
}
if env.GOMODCACHE == "" && env.GOPATH == "" {
return nil, fmt.Errorf("neither GOMODCACHE nor GOPATH is set")
}
mlog.Debugf("Go Version: %s", env.GOVERSION)
mlog.Debugf("GOROOT: %s", env.GOROOT)
mlog.Debugf("GOMODCACHE: %s", env.GOMODCACHE)
mlog.Debugf("GOPROXY: %s", env.GOPROXY)
return &env, nil
}
// CheckGitEnv verifies Git is installed and returns its version
func CheckGitEnv(ctx context.Context) (string, error) {
// 1. Check if git binary exists
gitPath, err := exec.LookPath("git")
if err != nil {
return "", fmt.Errorf("git is not installed or not in PATH: %w", err)
}
mlog.Debugf("Found git binary at: %s", gitPath)
// 2. Get git version
cmd := exec.CommandContext(ctx, "git", "--version")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get git version: %w", err)
}
version := strings.TrimSpace(string(output))
mlog.Debugf("Git version: %s", version)
return version, nil
}

View File

@ -1,146 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"fmt"
"go/format"
"path/filepath"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// generateProject copies the template to the destination and performs cleanup
// oldModule: original module path from template
// newModule: target module path for go.mod (can be different from project name)
func generateProject(ctx context.Context, srcPath, name, oldModule, newModule string) error {
pwd := gfile.Pwd()
dstPath := filepath.Join(pwd, name)
if name == "." {
dstPath = pwd
}
if gfile.Exists(dstPath) && !gfile.IsEmpty(dstPath) {
return fmt.Errorf("target directory %s is not empty", dstPath)
}
mlog.Printf("Generating project in %s...", dstPath)
// 1. Copy files
if err := gfile.Copy(srcPath, dstPath); err != nil {
return err
}
// 2. Clean up .git directory
gitDir := filepath.Join(dstPath, ".git")
if gfile.Exists(gitDir) {
if err := gfile.Remove(gitDir); err != nil {
mlog.Debugf("Failed to remove .git directory: %v", err)
}
}
// 3. Clean up go.work and go.work.sum (workspace files should not be in generated project)
for _, workFile := range []string{"go.work", "go.work.sum"} {
workPath := filepath.Join(dstPath, workFile)
if gfile.Exists(workPath) {
if err := gfile.Remove(workPath); err != nil {
mlog.Printf("Failed to remove %s: %v", workFile, err)
} else {
mlog.Debugf("Removed %s", workFile)
}
}
}
// 4. Update go.mod module name
goModPath := filepath.Join(dstPath, "go.mod")
if gfile.Exists(goModPath) {
content := gfile.GetContents(goModPath)
lines := gstr.Split(content, "\n")
if len(lines) > 0 && gstr.HasPrefix(lines[0], "module ") {
lines[0] = "module " + newModule
newContent := gstr.Join(lines, "\n")
if err := gfile.PutContents(goModPath, newContent); err != nil {
mlog.Printf("Failed to update go.mod: %v", err)
}
}
}
// 5. Use AST to replace import paths in all Go files
if oldModule != "" && oldModule != newModule {
replacer := NewASTReplacer(oldModule, newModule)
if err := replacer.ReplaceInDir(ctx, dstPath); err != nil {
return fmt.Errorf("failed to replace imports: %w", err)
}
}
// 6. Format the generated Go files using go/format (not imports.Process)
// Note: We use formatGoFiles instead of utils.GoFmt because imports.Process
// may incorrectly "fix" local import paths by replacing them with cached module paths.
formatGoFiles(dstPath)
mlog.Print("Project generated successfully!")
return nil
}
// tidyDependencies runs go mod tidy in the project directory
func tidyDependencies(ctx context.Context, projectDir string) error {
mlog.Print("Tidying dependencies (go mod tidy)...")
if err := runCmd(ctx, projectDir, "go", "mod", "tidy"); err != nil {
return fmt.Errorf("go mod tidy failed: %w", err)
}
mlog.Print("Dependencies tidied successfully!")
return nil
}
// upgradeDependencies runs go get -u ./... to upgrade all dependencies to latest
func upgradeDependencies(ctx context.Context, projectDir string) error {
mlog.Print("Upgrading dependencies to latest (go get -u ./...)...")
if err := runCmd(ctx, projectDir, "go", "get", "-u", "./..."); err != nil {
return fmt.Errorf("go get -u failed: %w", err)
}
// Run tidy again after upgrade
if err := runCmd(ctx, projectDir, "go", "mod", "tidy"); err != nil {
return fmt.Errorf("go mod tidy after upgrade failed: %w", err)
}
mlog.Print("Dependencies upgraded successfully!")
return nil
}
// formatGoFiles formats all Go files in the directory using go/format.
// Unlike imports.Process, this only formats code without modifying imports,
// which prevents incorrect "fixing" of local import paths.
func formatGoFiles(dir string) {
files, err := findGoFiles(dir)
if err != nil {
mlog.Printf("Failed to find Go files for formatting: %v", err)
return
}
for _, file := range files {
content := gfile.GetContents(file)
if content == "" {
continue
}
formatted, err := format.Source([]byte(content))
if err != nil {
mlog.Debugf("Failed to format %s: %v", file, err)
continue
}
if string(formatted) != content {
if err := gfile.PutContents(file, string(formatted)); err != nil {
mlog.Debugf("Failed to write formatted file %s: %v", file, err)
}
}
}
}

View File

@ -1,241 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// GitRepoInfo holds parsed git repository information
type GitRepoInfo struct {
Host string // e.g., github.com
Owner string // e.g., gogf
Repo string // e.g., examples
Branch string // e.g., main (default: main)
SubPath string // e.g., httpserver/jwt
CloneURL string // e.g., https://github.com/gogf/examples.git
}
// ParseGitURL parses a git URL and extracts repository info
// Supports formats:
// - github.com/owner/repo
// - github.com/owner/repo/subdir/path
// - github.com/owner/repo/tree/branch/subdir/path (from GitHub web URL)
func ParseGitURL(url string) (*GitRepoInfo, error) {
// Remove protocol prefix if present
url = strings.TrimPrefix(url, "https://")
url = strings.TrimPrefix(url, "http://")
url = strings.TrimSuffix(url, ".git")
// Remove version suffix like @v1.0.0
if idx := strings.Index(url, "@"); idx != -1 {
url = url[:idx]
}
parts := strings.Split(url, "/")
if len(parts) < 3 {
return nil, fmt.Errorf("invalid git URL: %s", url)
}
info := &GitRepoInfo{
Host: parts[0],
Owner: parts[1],
Repo: parts[2],
Branch: "main", // default branch
}
// Check for /tree/branch/ pattern (GitHub web URL)
if len(parts) > 4 && parts[3] == "tree" {
info.Branch = parts[4]
if len(parts) > 5 {
info.SubPath = strings.Join(parts[5:], "/")
}
} else if len(parts) > 3 {
// Direct subpath: github.com/owner/repo/subdir/path
info.SubPath = strings.Join(parts[3:], "/")
}
info.CloneURL = fmt.Sprintf("https://%s/%s/%s.git", info.Host, info.Owner, info.Repo)
return info, nil
}
// IsSubdirRepo checks if the URL points to a subdirectory of a repository
// Returns false for Go module paths (which may have /vN suffix or nested module paths)
// Note: This uses heuristics that may have false positives/negatives in edge cases
func IsSubdirRepo(url string) bool {
info, err := ParseGitURL(url)
if err != nil {
return false
}
if info.SubPath == "" {
return false
}
// Check if this looks like a Go module path rather than a git subdirectory
// Go modules can have nested paths like github.com/owner/repo/cmd/tool/v2
// We should try to resolve it as a Go module first
// If the URL can be resolved as a Go module, it's not a subdir repo
// We use a heuristic: check if the full path looks like a valid Go module
// by checking if it ends with /vN (major version) or contains common module patterns
// Remove version suffix for checking
cleanURL := url
if before, _, ok := strings.Cut(url, "@"); ok {
cleanURL = before
}
// Check if the path ends with /vN (Go module major version)
parts := strings.Split(cleanURL, "/")
if len(parts) > 0 {
lastPart := parts[len(parts)-1]
if len(lastPart) >= 2 && lastPart[0] == 'v' {
// Check if it's v2, v3, etc.
if _, err := fmt.Sscanf(lastPart, "v%d", new(int)); err == nil {
// This looks like a Go module with major version suffix
// It could be either a versioned module or a subdir ending in vN
// We'll treat it as a Go module and let go get handle it
mlog.Debugf("URL %s detected as Go module (ends with /vN)", url)
return false
}
}
}
// For GitHub URLs, check if the subpath could be a nested Go module
// Common patterns: cmd/*, internal/*, pkg/*, contrib/*
subPathParts := strings.Split(info.SubPath, "/")
if len(subPathParts) > 0 {
firstPart := subPathParts[0]
// These are common Go module nesting patterns
if firstPart == "cmd" || firstPart == "contrib" || firstPart == "tools" {
// This might be a nested Go module, not a simple subdirectory
// Let go get try first
mlog.Debugf("URL %s detected as Go module (starts with common pattern)", url)
return false
}
}
mlog.Debugf("URL %s detected as git subdirectory", url)
return true
}
// downloadGitSubdir downloads a subdirectory from a git repository using sparse checkout
func downloadGitSubdir(ctx context.Context, repoURL string) (string, *GitRepoInfo, error) {
info, err := ParseGitURL(repoURL)
if err != nil {
return "", nil, err
}
if info.SubPath == "" {
return "", nil, fmt.Errorf("not a subdirectory URL: %s", repoURL)
}
// Create temp directory for clone
tempDir := gfile.Temp("gf-init-git")
if tempDir == "" {
return "", nil, fmt.Errorf("failed to create temporary directory")
}
if err := gfile.Mkdir(tempDir); err != nil {
return "", nil, err
}
cloneDir := filepath.Join(tempDir, info.Repo)
mlog.Debugf("Using git temp workspace: %s", tempDir)
mlog.Printf("Cloning %s (sparse checkout: %s)...", info.CloneURL, info.SubPath)
// 1. Clone with no checkout, filter, and sparse
if err := runCmd(ctx, tempDir, "git", "clone", "--filter=blob:none", "--no-checkout", "--sparse", info.CloneURL); err != nil {
// Fallback: try without filter for older git versions
mlog.Debugf("Sparse clone failed, trying full clone...")
if err := gfile.Remove(cloneDir); err != nil {
mlog.Debugf("Failed to remove clone directory: %v", err)
}
if err := runCmd(ctx, tempDir, "git", "clone", "--no-checkout", info.CloneURL); err != nil {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
return "", nil, fmt.Errorf("git clone failed: %w", err)
}
}
// 2. Set sparse-checkout to the subpath
if err := runCmd(ctx, cloneDir, "git", "sparse-checkout", "set", info.SubPath); err != nil {
// Fallback for older git: use sparse-checkout init + set
mlog.Debugf("sparse-checkout set failed, trying legacy method...")
if err := runCmd(ctx, cloneDir, "git", "sparse-checkout", "init", "--cone"); err != nil {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
return "", nil, fmt.Errorf("git sparse-checkout init (legacy) failed: %w", err)
}
if err := runCmd(ctx, cloneDir, "git", "sparse-checkout", "set", info.SubPath); err != nil {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
return "", nil, fmt.Errorf("git sparse-checkout set (legacy) failed: %w", err)
}
}
// 3. Checkout the branch
if err := runCmd(ctx, cloneDir, "git", "checkout", info.Branch); err != nil {
// Try master if main fails
if info.Branch == "main" {
mlog.Debugf("Branch 'main' not found, trying 'master'...")
info.Branch = "master"
if err := runCmd(ctx, cloneDir, "git", "checkout", "master"); err != nil {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
return "", nil, fmt.Errorf("git checkout failed: %w", err)
}
} else {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
return "", nil, fmt.Errorf("git checkout failed: %w", err)
}
}
// Return the path to the subdirectory
subDirPath := filepath.Join(cloneDir, info.SubPath)
if !gfile.Exists(subDirPath) {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
return "", nil, fmt.Errorf("subdirectory not found: %s", info.SubPath)
}
mlog.Debugf("Subdirectory located at: %s", subDirPath)
return subDirPath, info, nil
}
// GetModuleNameFromGoMod reads module name from go.mod file
func GetModuleNameFromGoMod(dir string) string {
goModPath := filepath.Join(dir, "go.mod")
if !gfile.Exists(goModPath) {
return ""
}
content := gfile.GetContents(goModPath)
lines := gstr.Split(content, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if after, ok := strings.CutPrefix(line, "module "); ok {
return strings.TrimSpace(after)
}
}
return ""
}

View File

@ -1,99 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"bufio"
"context"
"fmt"
"os"
"strconv"
"strings"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// SelectVersion prompts user to select a version interactively
func SelectVersion(ctx context.Context, versions []string, modulePath string) (string, error) {
if len(versions) == 0 {
return "", fmt.Errorf("no versions available for selection")
}
if len(versions) == 1 {
mlog.Printf("Only one version available: %s", versions[0])
return versions[0], nil
}
// Display available versions
fmt.Printf("\nAvailable versions for %s:\n", modulePath)
fmt.Println(strings.Repeat("-", 40))
// Show versions with index (newest first)
maxDisplay := 20 // Limit display to avoid overwhelming output
displayCount := len(versions)
if displayCount > maxDisplay {
displayCount = maxDisplay
}
for i := 0; i < displayCount; i++ {
marker := ""
if i == 0 {
marker = " (latest)"
}
fmt.Printf(" [%2d] %s%s\n", i+1, versions[i], marker)
}
if len(versions) > maxDisplay {
fmt.Printf(" ... and %d more versions\n", len(versions)-maxDisplay)
}
fmt.Println(strings.Repeat("-", 40))
// Prompt for selection
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("Select version [1-%d] or enter version string (default: 1 for latest): ", displayCount)
input, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("failed to read input: %w", err)
}
input = strings.TrimSpace(input)
// Default to latest
if input == "" {
fmt.Printf("Selected: %s (latest)\n", versions[0])
return versions[0], nil
}
// Try parsing as number first
idx, err := strconv.Atoi(input)
if err == nil {
// Valid number - check if in range
if idx >= 1 && idx <= len(versions) {
// Allow selection from all versions, not just displayed ones
selected := versions[idx-1]
fmt.Printf("Selected: %s\n", selected)
return selected, nil
} else if idx < 1 || idx > displayCount {
fmt.Printf("Invalid selection. Please enter a number between 1 and %d, or type a version string.\n", displayCount)
continue
}
} else {
// Try matching the input as a version string (e.g., "v1.2.3")
for _, v := range versions {
if v == input || strings.Contains(v, input) {
fmt.Printf("Selected: %s\n", v)
return v, nil
}
}
fmt.Printf("Version '%s' not found. Please select by number or type a valid version string.\n", input)
continue
}
}
}

View File

@ -1,138 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"encoding/json"
"fmt"
"os/exec"
"sort"
"strings"
"golang.org/x/mod/semver"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)
// VersionInfo contains module version information
type VersionInfo struct {
Module string `json:"module"`
Versions []string `json:"versions"`
Latest string `json:"latest"`
}
// GetModuleVersions fetches available versions for a Go module
func GetModuleVersions(ctx context.Context, modulePath string) (*VersionInfo, error) {
// Create a temporary directory for go list
tempDir := gfile.Temp("gf-init-version")
if tempDir == "" {
return nil, fmt.Errorf("failed to create temporary directory for go list")
}
if err := gfile.Mkdir(tempDir); err != nil {
return nil, err
}
defer func() {
if err := gfile.Remove(tempDir); err != nil {
mlog.Debugf("Failed to remove temp directory: %v", err)
}
}()
// Initialize a temp go module
if err := runCmd(ctx, tempDir, "go", "mod", "init", "temp"); err != nil {
return nil, fmt.Errorf("failed to init temp module: %w", err)
}
// Get versions using go list -m -versions
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-versions", modulePath)
cmd.Dir = tempDir
output, err := cmd.Output()
if err != nil {
// Try with @latest to see if module exists
mlog.Debugf("go list -versions failed, trying @latest: %v", err)
return getLatestOnly(ctx, tempDir, modulePath)
}
// Parse output: "module/path v1.0.0 v1.1.0 v2.0.0"
parts := strings.Fields(strings.TrimSpace(string(output)))
if len(parts) < 1 {
return nil, fmt.Errorf("no version information found for %s", modulePath)
}
info := &VersionInfo{
Module: parts[0],
Versions: []string{},
}
if len(parts) > 1 {
info.Versions = parts[1:]
// Sort versions in descending order (newest first)
sort.Slice(info.Versions, func(i, j int) bool {
return semver.Compare(info.Versions[i], info.Versions[j]) > 0
})
info.Latest = info.Versions[0]
}
// If no tagged versions, try to get latest
if len(info.Versions) == 0 {
latestInfo, err := getLatestOnly(ctx, tempDir, modulePath)
if err != nil {
return nil, err
}
info.Latest = latestInfo.Latest
if latestInfo.Latest != "" {
info.Versions = []string{latestInfo.Latest}
}
}
return info, nil
}
// getLatestOnly gets only the latest version when go list -versions fails
func getLatestOnly(ctx context.Context, tempDir, modulePath string) (*VersionInfo, error) {
// Try go list -m modulePath@latest
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json", modulePath+"@latest")
cmd.Dir = tempDir
output, err := cmd.Output()
if err != nil {
// Try without @latest
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", modulePath)
cmd.Dir = tempDir
output, err = cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to get module info for %s: %w", modulePath, err)
}
}
var modInfo struct {
Path string `json:"Path"`
Version string `json:"Version"`
}
if err := json.Unmarshal(output, &modInfo); err != nil {
return nil, fmt.Errorf("failed to parse module info: %w", err)
}
return &VersionInfo{
Module: modInfo.Path,
Versions: []string{modInfo.Version},
Latest: modInfo.Version,
}, nil
}
// GetLatestVersion returns the latest version of a module
func GetLatestVersion(ctx context.Context, modulePath string) (string, error) {
info, err := GetModuleVersions(ctx, modulePath)
if err != nil {
return "", err
}
if info.Latest == "" {
return "", fmt.Errorf("no version found for %s", modulePath)
}
return info.Latest, nil
}

View File

@ -1,359 +0,0 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package geninit
import (
"context"
"path/filepath"
"testing"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)
func Test_ParseGitURL_Basic(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test basic github URL
info, err := ParseGitURL("github.com/gogf/gf")
t.AssertNil(err)
t.Assert(info.Host, "github.com")
t.Assert(info.Owner, "gogf")
t.Assert(info.Repo, "gf")
t.Assert(info.SubPath, "")
t.Assert(info.Branch, "main")
t.Assert(info.CloneURL, "https://github.com/gogf/gf.git")
})
}
func Test_ParseGitURL_WithHTTPS(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test URL with https prefix
info, err := ParseGitURL("https://github.com/gogf/gf")
t.AssertNil(err)
t.Assert(info.Host, "github.com")
t.Assert(info.Owner, "gogf")
t.Assert(info.Repo, "gf")
})
}
func Test_ParseGitURL_WithGitSuffix(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test URL with .git suffix
info, err := ParseGitURL("github.com/gogf/gf.git")
t.AssertNil(err)
t.Assert(info.Host, "github.com")
t.Assert(info.Owner, "gogf")
t.Assert(info.Repo, "gf")
})
}
func Test_ParseGitURL_WithSubPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test URL with subdirectory
info, err := ParseGitURL("github.com/gogf/examples/httpserver/jwt")
t.AssertNil(err)
t.Assert(info.Host, "github.com")
t.Assert(info.Owner, "gogf")
t.Assert(info.Repo, "examples")
t.Assert(info.SubPath, "httpserver/jwt")
t.Assert(info.CloneURL, "https://github.com/gogf/examples.git")
})
}
func Test_ParseGitURL_WithTreeBranch(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test GitHub web URL with /tree/branch/
info, err := ParseGitURL("github.com/gogf/examples/tree/develop/httpserver/jwt")
t.AssertNil(err)
t.Assert(info.Host, "github.com")
t.Assert(info.Owner, "gogf")
t.Assert(info.Repo, "examples")
t.Assert(info.Branch, "develop")
t.Assert(info.SubPath, "httpserver/jwt")
})
}
func Test_ParseGitURL_WithVersion(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test URL with version suffix
info, err := ParseGitURL("github.com/gogf/gf/cmd/gf/v2@v2.9.7")
t.AssertNil(err)
t.Assert(info.Host, "github.com")
t.Assert(info.Owner, "gogf")
t.Assert(info.Repo, "gf")
t.Assert(info.SubPath, "cmd/gf/v2")
})
}
func Test_ParseGitURL_Invalid(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Test invalid URL (too short)
_, err := ParseGitURL("github.com/gogf")
t.AssertNE(err, nil)
})
}
func Test_IsSubdirRepo_NotSubdir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Standard Go module paths should not be detected as subdirectory
t.Assert(IsSubdirRepo("github.com/gogf/gf"), false)
t.Assert(IsSubdirRepo("github.com/gogf/gf/v2"), false)
})
}
func Test_IsSubdirRepo_GoModuleWithCmd(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Go module paths with common patterns should not be detected as subdirectory
t.Assert(IsSubdirRepo("github.com/gogf/gf/cmd/gf/v2"), false)
t.Assert(IsSubdirRepo("github.com/gogf/gf/contrib/drivers/mysql/v2"), false)
})
}
func Test_IsSubdirRepo_ActualSubdir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Actual subdirectories should be detected
t.Assert(IsSubdirRepo("github.com/gogf/examples/httpserver/jwt"), true)
t.Assert(IsSubdirRepo("github.com/gogf/examples/grpc/basic"), true)
})
}
func Test_GetModuleNameFromGoMod_Valid(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create temp directory with go.mod
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Write go.mod file
goModContent := `module github.com/test/myproject
go 1.21
require (
github.com/gogf/gf/v2 v2.9.0
)
`
err = gfile.PutContents(filepath.Join(tempDir, "go.mod"), goModContent)
t.AssertNil(err)
// Test extraction
moduleName := GetModuleNameFromGoMod(tempDir)
t.Assert(moduleName, "github.com/test/myproject")
})
}
func Test_GetModuleNameFromGoMod_NoFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create temp directory without go.mod
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Test extraction - should return empty
moduleName := GetModuleNameFromGoMod(tempDir)
t.Assert(moduleName, "")
})
}
func Test_GetModuleNameFromGoMod_SimpleModule(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create temp directory with simple go.mod
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Write simple go.mod file
goModContent := `module main
go 1.21
`
err = gfile.PutContents(filepath.Join(tempDir, "go.mod"), goModContent)
t.AssertNil(err)
// Test extraction
moduleName := GetModuleNameFromGoMod(tempDir)
t.Assert(moduleName, "main")
})
}
func Test_ASTReplacer_ReplaceInFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create temp directory
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Create a Go file with imports
goFileContent := `package main
import (
"fmt"
"github.com/old/module/internal/service"
"github.com/old/module/pkg/utils"
"github.com/other/package"
)
func main() {
fmt.Println("Hello")
}
`
goFilePath := filepath.Join(tempDir, "main.go")
err = gfile.PutContents(goFilePath, goFileContent)
t.AssertNil(err)
// Replace imports
replacer := NewASTReplacer("github.com/old/module", "github.com/new/project")
err = replacer.ReplaceInFile(context.Background(), goFilePath)
t.AssertNil(err)
// Verify replacement
content := gfile.GetContents(goFilePath)
t.Assert(gfile.Exists(goFilePath), true)
// Check that old imports are replaced
t.AssertNE(content, "")
t.Assert(contains(content, `"github.com/new/project/internal/service"`), true)
t.Assert(contains(content, `"github.com/new/project/pkg/utils"`), true)
// Check that other imports are not affected
t.Assert(contains(content, `"github.com/other/package"`), true)
t.Assert(contains(content, `"fmt"`), true)
})
}
func Test_ASTReplacer_ReplaceInDir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create temp directory structure
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Create subdirectory
subDir := filepath.Join(tempDir, "sub")
err = gfile.Mkdir(subDir)
t.AssertNil(err)
// Create main.go
mainContent := `package main
import "github.com/old/module/sub"
func main() {
sub.Hello()
}
`
err = gfile.PutContents(filepath.Join(tempDir, "main.go"), mainContent)
t.AssertNil(err)
// Create sub/sub.go
subContent := `package sub
import "github.com/old/module/pkg"
func Hello() {
pkg.Do()
}
`
err = gfile.PutContents(filepath.Join(subDir, "sub.go"), subContent)
t.AssertNil(err)
// Replace imports in directory
replacer := NewASTReplacer("github.com/old/module", "github.com/new/project")
err = replacer.ReplaceInDir(context.Background(), tempDir)
t.AssertNil(err)
// Verify main.go replacement
mainResult := gfile.GetContents(filepath.Join(tempDir, "main.go"))
t.Assert(contains(mainResult, `"github.com/new/project/sub"`), true)
// Verify sub/sub.go replacement
subResult := gfile.GetContents(filepath.Join(subDir, "sub.go"))
t.Assert(contains(subResult, `"github.com/new/project/pkg"`), true)
})
}
func Test_findGoFiles(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create temp directory structure
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Create subdirectories
subDir := filepath.Join(tempDir, "sub")
err = gfile.Mkdir(subDir)
t.AssertNil(err)
// Create various files
err = gfile.PutContents(filepath.Join(tempDir, "main.go"), "package main")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(tempDir, "readme.md"), "# README")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(subDir, "sub.go"), "package sub")
t.AssertNil(err)
err = gfile.PutContents(filepath.Join(subDir, "data.json"), "{}")
t.AssertNil(err)
// Find Go files
files, err := findGoFiles(tempDir)
t.AssertNil(err)
// Should find exactly 2 Go files
t.Assert(len(files), 2)
// Verify file names
hasMain := false
hasSub := false
for _, f := range files {
if filepath.Base(f) == "main.go" {
hasMain = true
}
if filepath.Base(f) == "sub.go" {
hasSub = true
}
}
t.Assert(hasMain, true)
t.Assert(hasSub, true)
})
}
func Test_findGoFiles_EmptyDir(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
// Create empty temp directory
tempDir := gfile.Temp(guid.S())
err := gfile.Mkdir(tempDir)
t.AssertNil(err)
defer gfile.Remove(tempDir)
// Find Go files
files, err := findGoFiles(tempDir)
t.AssertNil(err)
t.Assert(len(files), 0)
})
}
// Helper function to check if string contains substring
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsAt(s, substr))
}
func containsAt(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}

View File

@ -12,6 +12,7 @@ import (
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gregex"
"github.com/gogf/gf/v2/text/gstr"
@ -36,14 +37,21 @@ func (c CGenService) calculateImportedItems(
}
for _, item := range pkgItems {
// Skip anonymous imports
if item.Alias == "_" {
alias := item.Alias
// If the alias is _, it means that the package is not generated.
if alias == "_" {
mlog.Debugf(`ignore anonymous package: %s`, item.RawImport)
continue
}
// Keep all imports, let gofmt clean up unused ones.
// We cannot accurately infer package name from import path
// (e.g., path "minio-go" but package name is "minio").
// If the alias is empty, it will use the package name as the alias.
if alias == "" {
alias = gfile.Basename(gstr.Trim(item.Path, `"`))
}
if !gstr.Contains(allFuncParamType.String(), alias) {
mlog.Debugf(`ignore unused package: %s`, item.RawImport)
continue
}
srcImportedPackages.Add(item.RawImport)
}
return nil

View File

@ -4,7 +4,7 @@ go 1.23.0
toolchain go1.24.6
require github.com/gogf/gf/v2 v2.9.8
require github.com/gogf/gf/v2 v2.9.6
require (
go.opentelemetry.io/otel v1.38.0 // indirect

View File

@ -1,10 +1,13 @@
package testdata
import (
"fmt"
"testing"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
)

View File

@ -7,7 +7,7 @@ package dict
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_ctrl/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_ctrl/api/dict/v1"
)
type IDictV1 interface {

View File

@ -7,7 +7,7 @@ package dict
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_ctrl/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_ctrl/api/dict/v1"
)
type IDictV1 interface {

View File

@ -5,7 +5,7 @@
package dict
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_ctrl/api/dict"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_ctrl/api/dict"
)
type ControllerV1 struct{}

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_ctrl/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_ctrl/api/dict/v1"
)
func (c *ControllerV1) DictTypeAddPage(ctx context.Context, req *v1.DictTypeAddPageReq) (res *v1.DictTypeAddPageRes, err error) {

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_ctrl/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_ctrl/api/dict/v1"
)
func (c *ControllerV1) DictTypeAddPage(ctx context.Context, req *v1.DictTypeAddPageReq) (res *v1.DictTypeAddPageRes, err error) {

View File

@ -7,7 +7,7 @@ package dict
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_ctrl/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_ctrl/api/dict/v1"
)
type IDictV1 interface {

View File

@ -7,7 +7,7 @@ package dict
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_file/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_file/api/dict/v1"
)
type IDictV1 interface {

View File

@ -5,7 +5,7 @@
package dict
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_file/api/dict"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_file/api/dict"
)
type ControllerV1 struct{}

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_file/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_file/api/dict/v1"
)
func (c *ControllerV1) DictTypeAddPage(ctx context.Context, req *v1.DictTypeAddPageReq) (res *v1.DictTypeAddPageRes, err error) {

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/merge/add_new_file/api/dict/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl-merge/add_new_file/api/dict/v1"
)
func (c *ControllerV1) DictTypeAdd(ctx context.Context, req *v1.DictTypeAddReq) (res *v1.DictTypeAddRes, err error) {

View File

@ -7,8 +7,8 @@ package article
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v2"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v2"
)
type IArticleV1 interface {

View File

@ -5,7 +5,7 @@
package article
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article"
)
type ControllerV1 struct{}

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
)
// Create add title.

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
)
func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
)
func (c *ControllerV1) GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error) {

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
)
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v2"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v2"
)
func (c *ControllerV2) Create(ctx context.Context, req *v2.CreateReq) (res *v2.CreateRes, err error) {

View File

@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v2"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v2"
)
func (c *ControllerV2) Update(ctx context.Context, req *v2.UpdateReq) (res *v2.UpdateRes, err error) {

View File

@ -1,15 +0,0 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package article
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/admin/article/v1"
)
type IArticleV1 interface {
Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
}

View File

@ -1,19 +0,0 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package v1
import "github.com/gogf/gf/v2/frame/g"
type (
// CreateReq add title.
CreateReq struct {
g.Meta `path:"/article/create" method:"post" tags:"ArticleService"`
Title string `v:"required"`
}
CreateRes struct{}
)

View File

@ -1,15 +0,0 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package user
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/admin/user/v1"
)
type IUserV1 interface {
Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
}

View File

@ -1,19 +0,0 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package v1
import "github.com/gogf/gf/v2/frame/g"
type (
// CreateReq add title.
CreateReq struct {
g.Meta `path:"/article/create" method:"post" tags:"ArticleService"`
Title string `v:"required"`
}
CreateRes struct{}
)

View File

@ -1,16 +0,0 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package user
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/v1"
)
type IUserV1 interface {
Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
}

View File

@ -1,16 +0,0 @@
// =================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package user_ext
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/user_ext/v1"
)
type IUserExtV1 interface {
Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
}

View File

@ -1,28 +0,0 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package v1
import "github.com/gogf/gf/v2/frame/g"
type (
// CreateReq add title.
CreateReq struct {
g.Meta `path:"/article/create" method:"post" tags:"ArticleService"`
Title string `v:"required"`
}
CreateRes struct{}
)
type (
UpdateReq struct {
g.Meta `path:"/article/update" method:"post" tags:"ArticleService"`
Title string `v:"required"`
}
UpdateRes struct{}
)

View File

@ -1,28 +0,0 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package v1
import "github.com/gogf/gf/v2/frame/g"
type (
// CreateReq add title.
CreateReq struct {
g.Meta `path:"/article/create" method:"post" tags:"ArticleService"`
Title string `v:"required"`
}
CreateRes struct{}
)
type (
UpdateReq struct {
g.Meta `path:"/article/update" method:"post" tags:"ArticleService"`
Title string `v:"required"`
}
UpdateRes struct{}
)

View File

@ -1,5 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package article

View File

@ -1,15 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package article
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/admin/article"
)
type ControllerV1 struct{}
func NewV1() article.IArticleV1 {
return &ControllerV1{}
}

View File

@ -1,15 +0,0 @@
package article
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/admin/article/v1"
)
// Create add title.
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

View File

@ -1,5 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user

View File

@ -1,15 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/admin/user"
)
type ControllerV1 struct{}
func NewV1() user.IUserV1 {
return &ControllerV1{}
}

View File

@ -1,15 +0,0 @@
package user
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/admin/user/v1"
)
// Create add title.
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

View File

@ -1,5 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user

View File

@ -1,5 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user_ext

View File

@ -1,15 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user_ext
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/user_ext"
)
type ControllerV1 struct{}
func NewV1() user_ext.IUserExtV1 {
return &ControllerV1{}
}

View File

@ -1,15 +0,0 @@
package user_ext
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/user_ext/v1"
)
// Create add title.
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

View File

@ -1,14 +0,0 @@
package user_ext
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/user_ext/v1"
)
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

View File

@ -1,15 +0,0 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================
package user
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user"
)
type ControllerV1 struct{}
func NewV1() user.IUserV1 {
return &ControllerV1{}
}

View File

@ -1,15 +0,0 @@
package user
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/v1"
)
// Create add title.
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

View File

@ -1,14 +0,0 @@
package user
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/multi/api/app/user/v1"
)
func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
return nil, gerror.NewCode(gcode.CodeNotImplemented)
}

View File

@ -1,47 +0,0 @@
-- Test case for issue #4603: overlapping sharding patterns
-- https://github.com/gogf/gf/issues/4603
--
-- Patterns: "a_?", "a_b_?", "a_c_?"
-- Expected: a_1/a_2 -> "a", a_b_1/a_b_2 -> "a_b", a_c_1/a_c_2 -> "a_c"
CREATE TABLE `a_1`
(
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `a_2`
(
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `a_b_1`
(
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `a_b_2`
(
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `a_c_1`
(
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `a_c_2`
(
`id` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

View File

@ -1,30 +0,0 @@
-- Test case for issue #4629: tables pattern matching
-- https://github.com/gogf/gf/issues/4629
-- Standard SQL syntax compatible with MySQL and PostgreSQL
--
-- Tables: trade_order, trade_item, user_info, user_log, config
CREATE TABLE trade_order (
id INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL
);
CREATE TABLE trade_item (
id INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL
);
CREATE TABLE user_info (
id INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL
);
CREATE TABLE user_log (
id INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL
);
CREATE TABLE config (
id INTEGER PRIMARY KEY,
name VARCHAR(45) NOT NULL
);

View File

@ -1,22 +0,0 @@
syntax = "proto3";
package genpb;
option go_package = "genpb/v1";
message UserReq {
// v:required
// v:#Id > 0
int64 Id = 1;
// User name for login
string Name = 2;
// v:required
// v:email
string Email = 3; // User email address
}
message UserResp {
int64 Id = 1;
string Name = 2;
string Email = 3;
}

View File

@ -1,21 +0,0 @@
syntax = "proto3";
package genpb;
option go_package = "genpb/v1";
message Order {
// v:required
int64 OrderId = 1;
// Order details
OrderDetail Detail = 2;
}
message OrderDetail {
// v:required
string ProductName = 1;
// v:min:1
int32 Quantity = 2;
// v:min:0.01
double Price = 3;
}

View File

@ -1,34 +0,0 @@
package issue4242
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
"github.com/gogf/gf/contrib/drivers/mysql/v2"
)
func init() {
service.RegisterIssue4242(New())
}
type sIssue4242 struct {
}
func New() *sIssue4242 {
return &sIssue4242{}
}
// GetDriver tests versioned import path is preserved.
func (s *sIssue4242) GetDriver(ctx context.Context) (d mysql.Driver, err error) {
return mysql.Driver{}, nil
}
// GetRequest tests another versioned import.
func (s *sIssue4242) GetRequest(ctx context.Context) (*ghttp.Request, error) {
g.Log().Info(ctx, "getting request")
return nil, nil
}

View File

@ -1,37 +0,0 @@
package issue4242alias
import (
"context"
// Anonymous import (should be skipped)
_ "github.com/gogf/gf/v2/os/gres"
// Versioned import without alias
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/service"
// Explicit alias import
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
)
func init() {
service.RegisterIssue4242Alias(New())
}
type sIssue4242Alias struct {
}
func New() *sIssue4242Alias {
return &sIssue4242Alias{}
}
// GetDriver tests explicit alias import.
func (s *sIssue4242Alias) GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error) {
return mysqlDriver.Driver{}, nil
}
// GetRequest tests versioned import.
func (s *sIssue4242Alias) GetRequest(ctx context.Context) (*ghttp.Request, error) {
return nil, nil
}

View File

@ -1,10 +0,0 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package logic
import (
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/logic/issue4242"
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/issue/4242/logic/issue4242alias"
)

View File

@ -1,37 +0,0 @@
// ================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// You can delete these comments if you wish manually maintain this interface file.
// ================================================================================
package service
import (
"context"
"github.com/gogf/gf/contrib/drivers/mysql/v2"
"github.com/gogf/gf/v2/net/ghttp"
)
type (
IIssue4242 interface {
// GetDriver tests versioned import path is preserved.
GetDriver(ctx context.Context) (d mysql.Driver, err error)
// GetRequest tests another versioned import.
GetRequest(ctx context.Context) (*ghttp.Request, error)
}
)
var (
localIssue4242 IIssue4242
)
func Issue4242() IIssue4242 {
if localIssue4242 == nil {
panic("implement not found for interface IIssue4242, forgot register?")
}
return localIssue4242
}
func RegisterIssue4242(i IIssue4242) {
localIssue4242 = i
}

View File

@ -1,37 +0,0 @@
// ================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// You can delete these comments if you wish manually maintain this interface file.
// ================================================================================
package service
import (
"context"
mysqlDriver "github.com/gogf/gf/contrib/drivers/mysql/v2"
"github.com/gogf/gf/v2/net/ghttp"
)
type (
IIssue4242Alias interface {
// GetDriver tests explicit alias import.
GetDriver(ctx context.Context) (d mysqlDriver.Driver, err error)
// GetRequest tests versioned import.
GetRequest(ctx context.Context) (*ghttp.Request, error)
}
)
var (
localIssue4242Alias IIssue4242Alias
)
func Issue4242Alias() IIssue4242Alias {
if localIssue4242Alias == nil {
panic("implement not found for interface IIssue4242Alias, forgot register?")
}
return localIssue4242Alias
}
func RegisterIssue4242Alias(i IIssue4242Alias) {
localIssue4242Alias = i
}

Some files were not shown because too many files have changed in this diff Show More