Skip to content

gone-io/gonectl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

English  |  中文

gonectl

Command-line tool for Gone framework, simplifying project creation, module management, and code generation

Introduction

gonectl is the official command-line tool for the Gone framework, designed to streamline the development process of Gone projects. It provides a series of convenient commands to help developers quickly create projects, manage modules, generate code, and build applications. Whether you're new to Gone or an experienced developer, gonectl can significantly improve your development efficiency.

Installation

Method 1: Using go install (Recommended)

Run the following command to install gonectl:

go install github.com/gone-io/gonectl@latest

After installation, gonectl will be located in the $GOPATH/bin directory. Make sure this directory is added to your system's $PATH environment variable for global access to the gonectl command.

Tip: If you're unsure about the location of $GOPATH, you can check it by running go env GOPATH.

Method 2: Direct Binary Download

You can also visit the gonectl/releases page to download the latest version binary for your operating system, then:

  1. Extract the downloaded file
  2. Copy the extracted gonectl executable to a directory in your system PATH
  3. Ensure the file has execution permissions (on Linux/macOS, you may need to run chmod +x gonectl)

Feature Overview

gonectl provides the following core features:

  • Project Creation: Quickly scaffold Gone project architecture from templates
  • Module Installation: Integrate Gone modules and automatically generate loading code
  • Code Generation: Automatically generate necessary Gone framework integration code
  • Mock Generation: Create Mock implementations for interfaces, facilitating unit testing
  • Build and Run: Simplify project building and running processes

Detailed Usage Guide

1. create Command: Create Gone Projects from Templates

The create command helps you quickly create Gone projects based on preset or custom templates.

View Help:

gonectl create -h

Basic Usage: Create Project

gonectl create demo-project

This will create a basic Gone project named demo-project in the current directory.

Create Project with Specific Template

gonectl create demo-project -t template-name

List All Available Templates

gonectl create -ls

This command lists all built-in project templates with their brief descriptions.

Create Project with Module Name

gonectl create demo-project -t template-name -m github.com/gone-io/my-module

This is particularly useful when creating projects that will be published as public packages.

Create Project from Remote Git Repository Template

gonectl create demo-project -t https://github.com/gone-io/template-v2-web-mysql

You can directly use any Git repository that follows the Gone template specification as a project template.

2. install Command: Install Gone Modules and Generate module.load.go

The install command integrates Gone modules into your project and automatically generates the necessary loading code.

Gone Module Best Practice: We recommend each Gone module to provide one or more gone.LoadFunc functions, such as:

func Load(gone.Loader) error {
    // Load related Goners
    return nil
}

View Help:

gonectl install -h

Basic Usage: Install Module

gonectl install demo-module

This adds demo-module to your project and generates the corresponding loading code.

Specify LoadFunc

# Specify LoadA and LoadB functions for generating loading code
gonectl install module LoadA,LoadB

Real Example

gonectl install github.com/gone-io/goner/nacos RegistryLoad

This installs the nacos module and uses its RegistryLoad function for initialization.

Uninstall/Modify Module

When executing gonectl install module command:

  • If the module is not installed, it will be installed
  • If already installed, an interactive selection list will be displayed where you can uncheck unwanted LoadFunc to remove them from module.load.go

gone-io Official Modules, Supporting Short Names

gonectl install goner/nacos

Note: For unofficial modules, you need to use the complete Golang module name.

3. generate Command: Generate *.gone.go Files for Gone Projects

The generate command scans project directories and automatically generates integration code files needed by the Gone framework.

Functionality

This command will:

  1. Scan all packages in specified directories

  2. Create init.gone.go file for packages containing Goner or LoadFunc, generating automatic loading code:

    func init() {
        gone.
            Loads(Load).  // Load LoadFunc
            Load(&MyGoner{})  // Load Goner
            // ... Load more Goners
    }

    Note: If a package defines LoadFunc, it will only load LoadFunc and not directly load Goners, indicating that the user has chosen to manually manage Goners.

  3. Create import.gone.go file in the main package directory to import all discovered Goner packages:

    package main
    
    import (
        _ "test"
        _ "test/modules/a"
        _ "test/modules/b"
    )

Important: Do not manually modify *.gone.go files, as they will be automatically overwritten by gonectl.

Specify Scan Directory

# Can specify multiple directories simultaneously
gonectl generate -s ./test -s ./test2

Specify Main Function Directory

gonectl generate -m cmd/server

Advanced Usage: Generate import.gone.go for Non-main Package

gonectl generate -m for_import --main-package-name for_import

Advanced Usage: Support Multiple Gone Instances

When using multiple Gone instances in the same program, you can use --preparer-code and --preparer-package parameters:

# Goners in gone1 directory use instance-1 instance
gonectl generate -s gone1 --preparer-code 'g.App("instance-1")' --preparer-package 'github.com/gone-io/goner/g'

# Goners in gone2 directory use instance-2 instance
gonectl generate -s gone2 --preparer-code 'g.App("instance-2")' --preparer-package 'github.com/gone-io/goner/g'

Use with go generate

Create a generate.go file in the project root directory and add the following code:

//go:generate gonectl generate -m main-package-dir

Then execute go generate ./... to automatically run the gonectl command.

4. mock Command: Generate Mock Code

The mock command generates Mock implementations for interfaces and registers them as Goners, facilitating integration into the Gone framework for testing.

Prerequisites: This feature depends on the uber mockgen tool, please install it first:

go install go.uber.org/mock/mockgen@latest

View Help:

gonectl mock -h

Basic Usage

# Generate Mock implementation for UserService interface in service package
gonectl mock -package service -interfaces UserService

More Options

# Generate Mock implementations for multiple interfaces and specify output directory
gonectl mock -package service -interfaces "UserService,OrderService" -output ./mocks

5. build Command: Build Gone Projects

The build command is an enhanced wrapper around the standard go build, specifically designed for Gone projects.

Features

  • Automatically executes go generate ./... before compilation to ensure all auxiliary code is updated
  • Supports all standard go build parameters and options

View Help:

gonectl build -h

Basic Usage

# Build Gone project in current directory
gonectl build

# Specify output filename
gonectl build -o myapp

# Use other go build parameters
gonectl build -v -ldflags="-s -w"

6. run Command: Run Gone Projects

The run command is similar to build, serving as an enhanced wrapper around go run.

Features

  • Automatically runs go generate ./... before execution to update all auxiliary code
  • Supports all standard go run parameters and options

View Help:

gonectl run -h

Basic Usage

# Run Gone project in current directory
gonectl run

# Run specific file
gonectl run main.go

# Run with parameters
gonectl run . -config=dev.yaml

FAQ

Q: What is the relationship between gonectl and standard Go tools?

A: gonectl is a complement to standard Go tools, specifically designed for the Gone framework. It simplifies Gone-specific code generation and project management processes but still internally calls standard Go commands.

Q: How to upgrade gonectl to the latest version?

A: Execute go install github.com/gone-io/gonectl@latest to update to the latest version.

Q: Should *.gone.go files be included in version control?

A: It's recommended to include these files in version control as they are part of the project structure. However, they can also be dynamically generated in CI/CD pipelines.

More Resources

About

gonectl is a tool for gone project generating code, building code, running code

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages