Skip to content

haru-256/gcectl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

gcectl: Google Cloud Compute Engine Management CLI

Go Rust License: MIT

A powerful and elegant CLI tool for managing Google Cloud Compute Engine instances with clean architecture design.

✨ Features

  • πŸš€ VM Operations: Start, stop, and monitor GCE instances
    • Support for multiple VMs in parallel
    • Fail-fast behavior for safety
  • πŸ“Š Status Monitoring: View VM status with intelligent uptime tracking
    • Supports days, hours, minutes, and seconds
    • Automatic format selection: 7d12h45m, 2h30m, 5m30s, 45s
  • βš™οΈ Machine Type Management: Change VM configurations on the fly
  • πŸ“… Schedule Policies: Automate VM start/stop schedules
  • 🎨 Beautiful Output: Styled terminal output with tables and emojis
  • ⚑ Parallel Execution: Fast operations with concurrent API calls
  • πŸ—οΈ Clean Architecture: Well-structured codebase following best practices
  • βœ… Comprehensive Tests: 80+ test cases with race detection and integration tests

πŸ“¦ Installation

From Binary Release

curl -sSL "https://raw.githubusercontent.com/haru-256/gcectl/main/scripts/install.sh" | sh

From Source

Prerequisites

  • Go 1.21 or higher
git clone https://github.com/haru-256/gcectl.git
cd gcectl/go
make build
# Binary will be available at bin/main

Completion

You can enable shell completion for bash, zsh, or fish. Please refer to the following commands result.

gcectl completion bash --help # for bash
gcectl completion zsh --help  # for zsh
gcectl completion fish --help # for fish

For example, in fish, you can enable completion by running:

gcectl completion fish > "${HOME}/.config/fish/completions/gcectl.fish"

πŸš€ Quick Start

Configuration

Create a configuration file at ~/.config/gcectl/config.yaml:

default-project: your-gcp-project
default-zone: us-central1-a
vm:
  - name: my-vm
    project: your-gcp-project
    zone: us-central1-a
  - name: dev-vm
    project: your-gcp-project
    zone: asia-northeast1-a

Basic Commands

# List all VMs with status and uptime
gcectl list

# View detailed information about a VM
gcectl describe my-vm

# Start one or more VMs
gcectl on my-vm
gcectl on vm1 vm2 vm3

# Stop one or more VMs
gcectl off my-vm
gcectl off vm1 vm2

# Change machine type (VM must be stopped)
gcectl set machine-type my-vm e2-medium

# Set schedule policy
gcectl set schedule-policy my-vm my-schedule-policy

# Unset schedule policy
gcectl set schedule-policy my-vm my-schedule-policy --un

πŸ“– Usage Examples

List VMs

gcectl list

Output:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Name   β”‚  Project   β”‚     Zone     β”‚ Machine-Type β”‚   Status    β”‚ Schedule β”‚ Uptime  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ my-vm    β”‚ my-project β”‚ us-central1-aβ”‚ e2-medium    β”‚ 🟒 RUNNING  β”‚ policy-1 β”‚ 2h30m   β”‚
β”‚ dev-vm   β”‚ my-project β”‚ us-west1-a   β”‚ n1-standard-1β”‚ οΏ½ RUNNING  β”‚          β”‚ 7d12h45mβ”‚
β”‚ test-vm  β”‚ my-project β”‚ asia-east1-a β”‚ e2-small     β”‚ 🟒 RUNNING  β”‚          β”‚ 5m30s   β”‚
β”‚ old-vm   β”‚ my-project β”‚ us-east1-b   β”‚ e2-micro     β”‚ οΏ½πŸ”΄ STOPPED  β”‚          β”‚ N/A     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Uptime Format:

  • Days: 7d12h45m (days, hours, minutes)
  • Hours: 2h30m (hours, minutes)
  • Minutes: 5m30s (minutes, seconds)
  • Seconds: 45s (seconds only)

Describe a VM

gcectl describe my-vm

Output:

β€’ Name          : my-vm
β€’ Project       : my-project
β€’ Zone          : us-central1-a
β€’ MachineType   : e2-medium
β€’ Status        : 🟒 RUNNING
β€’ Uptime        : 2h30m
β€’ SchedulePolicy: my-schedule-policy

Start a VM

# Start a single VM
$ gcectl on my-vm
Starting VM my-vm...
[SUCCESS] | VM my-vm started successfully

# Start multiple VMs in parallel
$ gcectl on vm1 vm2 vm3
Starting 3 VMs...
[SUCCESS] | All VMs started successfully

Stop VMs

# Stop a single VM
$ gcectl off my-vm
Stopping VM my-vm...
[SUCCESS] | VM my-vm stopped successfully

# Stop multiple VMs in parallel
$ gcectl off vm1 vm2
Stopping 2 VMs...
[SUCCESS] | All VMs stopped successfully

Change Machine Type

$ gcectl set machine-type my-vm e2-standard-2
Updating machine type for VM my-vm...
[SUCCESS] | Set machine-type to e2-standard-2

πŸ—οΈ Architecture

This project follows Clean Architecture principles with strict layer separation:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Interface Layer                        β”‚
β”‚           (cmd/, internal/interface/presenter/)          β”‚
β”‚   β€’ CLI Commands (Cobra)                                 β”‚
β”‚   β€’ Console Presentation (lipgloss)                      β”‚
β”‚   β€’ Progress Indicators                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Use Case Layer                          β”‚
β”‚                 (usecase/)                               β”‚
β”‚   β€’ Business Logic Orchestration                         β”‚
β”‚   β€’ VM Operations (Start, Stop, Update)                  β”‚
β”‚   β€’ Parallel Execution with errgroup                     β”‚
β”‚   β€’ Data Retrieval (List, Describe)                      β”‚
β”‚   β€’ Shared Utilities (Uptime Calculation)                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   Domain Layer                           β”‚
β”‚         (domain/model/, domain/repository/)              β”‚
β”‚   β€’ Core Entities (VM, Status)                           β”‚
β”‚   β€’ Business Rules (CanStart, CanStop)                   β”‚
β”‚   β€’ Repository Interfaces                                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Infrastructure Layer                        β”‚
β”‚      (infrastructure/gcp/, infrastructure/config/)       β”‚
β”‚   β€’ GCP Compute Engine API Client                        β”‚
β”‚   β€’ Configuration Management (YAML)                      β”‚
β”‚   β€’ Logging & Error Handling                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Principles

  • Dependency Rule: Dependencies point inward only
  • Layer Independence: Inner layers have no knowledge of outer layers
  • Progress Control: Progress display managed in presentation layer (cmd)
  • Repository Pattern: Abstract external API interactions
  • Parallel Execution: Multiple VM operations using errgroup
  • YAGNI: Use cases applied only where business logic exists

For detailed architecture documentation, see go/README.md.

πŸ§ͺ Testing

The project maintains high test coverage with comprehensive test suites:

cd go

# Run all tests
make test

# Run tests with verbose output
go test ./... -v

# Run tests with coverage
go test ./... -cover

# Run specific test package
go test ./internal/usecase/... -v

Test Coverage:

  • βœ… 80+ test cases
  • βœ… Domain layer: Business rule tests
  • βœ… Use case layer: Mock-based integration tests
  • βœ… Infrastructure layer: Integration tests for GCP operations
  • βœ… Presenter layer: Output validation tests
  • βœ… Race detection enabled
  • βœ… Table-driven test patterns

πŸ› οΈ Development

Build

cd go
make build
# Output: bin/main

Lint

cd go
make lint
# Uses golangci-lint with strict configuration

Project Structure

gcectl/
β”œβ”€β”€ go/                              # Go implementation
β”‚   β”œβ”€β”€ cmd/                         # CLI commands
β”‚   β”‚   β”œβ”€β”€ describe.go              # Describe VM command
β”‚   β”‚   β”œβ”€β”€ list.go                  # List VMs command
β”‚   β”‚   β”œβ”€β”€ on.go                    # Start VM command
β”‚   β”‚   β”œβ”€β”€ off.go                   # Stop VM command
β”‚   β”‚   β”œβ”€β”€ root.go                  # Root command
β”‚   β”‚   └── set/                     # Set command group
β”‚   β”‚       β”œβ”€β”€ machine_type.go      # Set machine type
β”‚   β”‚       └── schedule.go          # Set/unset schedule
β”‚   β”œβ”€β”€ internal/
β”‚   β”‚   β”œβ”€β”€ domain/                  # Domain layer
β”‚   β”‚   β”‚   β”œβ”€β”€ model/               # Entities (VM)
β”‚   β”‚   β”‚   └── repository/          # Repository interfaces
β”‚   β”‚   β”œβ”€β”€ usecase/                 # Use case layer
β”‚   β”‚   β”‚   β”œβ”€β”€ describe_vm.go       # Describe VM use case
β”‚   β”‚   β”‚   β”œβ”€β”€ list_vms.go          # List VMs use case
β”‚   β”‚   β”‚   β”œβ”€β”€ vm_uptime.go         # Shared uptime logic
β”‚   β”‚   β”‚   β”œβ”€β”€ start_vm.go          # Start VM use case
β”‚   β”‚   β”‚   β”œβ”€β”€ stop_vm.go           # Stop VM use case
β”‚   β”‚   β”‚   └── update_machine_type.go
β”‚   β”‚   β”œβ”€β”€ infrastructure/          # Infrastructure layer
β”‚   β”‚   β”‚   β”œβ”€β”€ gcp/                 # GCP API client
β”‚   β”‚   β”‚   β”œβ”€β”€ config/              # Configuration
β”‚   β”‚   β”‚   └── log/                 # Logging
β”‚   β”‚   └── interface/               # Interface layer
β”‚   β”‚       └── presenter/           # Console presenter
β”‚   β”œβ”€β”€ main.go                      # Application entry
β”‚   β”œβ”€β”€ config.yaml                  # Example config
β”‚   └── Makefile                     # Build automation
β”‚
β”œβ”€β”€ terraform/                       # Infrastructure as Code
β”‚   β”œβ”€β”€ environments/dev/            # Dev environment
β”‚   └── modules/                     # Reusable modules
β”‚       β”œβ”€β”€ gce/                     # GCE instance module
β”‚       └── tfstate_gcs_bucket/      # State bucket module
β”‚
└── rust/                            # Rust implementation (WIP)

🌟 Status Indicators

The CLI uses emoji indicators for quick status recognition:

  • 🟒 RUNNING - VM is running
  • πŸ”΄ STOPPED - VM is stopped
  • 🟑 STAGING - VM is being staged
  • 🟠 PROVISIONING - VM is provisioning
  • πŸ”΅ STOPPING - VM is stopping
  • ⚫ TERMINATED - VM is terminated
  • βšͺ SUSPENDING - VM is suspending
  • 🟀 SUSPENDED - VM is suspended
  • πŸ”„ REPAIRING - VM is being repaired

πŸ“š Additional Resources

🀝 Contributing

Contributions are welcome! Please follow these guidelines:

  1. Follow Clean Architecture: Respect layer boundaries
  2. Add Tests: Aim for >80% coverage for new code
  3. Update Documentation: Keep README and docstrings current
  4. Run Quality Checks: Ensure make test and make lint pass
  5. Keep Use Cases Lean: Add use case layer only when business logic exists

See CONTRIBUTING.md for more details (if available).

πŸ“‹ Roadmap

Completed βœ…

  • Start/Stop VM operations (single and multiple VMs)
  • List VMs with status and uptime
  • Describe VM details
  • Set machine type
  • Set/unset schedule policies
  • Clean Architecture implementation
  • Progress indicators with ExecuteWithProgress helper
  • Parallel execution for multiple VMs (errgroup)
  • Comprehensive test coverage (80+ tests)
  • Integration tests for GCP operations
  • Styled console output
  • Intelligent uptime formatting (days/hours/minutes/seconds)
  • Success logging for each operation

Planned πŸ”œ

  • Interactive TUI mode (bubbletea)
  • List available machine types
  • VM cost estimation
  • Configuration validation command
  • Export VM details (JSON/YAML)
  • GoReleaser for multi-platform releases
  • Homebrew formula
  • Docker image

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

haru-256

πŸ™ Acknowledgments


Made with ❀️ and Clean Architecture

About

Go CLI tool for managing Google Cloud Compute Engine instances

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •