π§ Go Wizard
This README.md is also available in Spanish.
Is a command line interface that provides you with useful commands to help you build your application with the Hexagonal
Architecture. It allows you to generate the initial structure of a project and to generate every layer of a new package.
π― Features
- Create the initial structure of a project β
- Create a package on the different layers β
- Add one field on the desired layers (database, model, storage, etc.) WIP π
- Remove one field on the desired layers (database, model, storage, etc.) WIP π
- Override a package WIP π
β Installation
This command will install the CLI on $GOPATH/bin
so make sure that you have that on your $PATH
.
go install github.com/edteamlat/go-wizard@latest
If you want a different version, just replace the @latest
with the version @v1.0.0
π» Usage
init
command
To create the initial structure of a new project you just need to run the next command:
go-wizard init -m github.com/edteamlat/my-app
This will create the project on the current directory with the layers to work with the hexagonal architecture, and
it'll automatically init git and the go modules.
.
βββ my-app
βββ cmd
β βββ certificates
β βββ logs
β βββ config.go
β βββ configuration.json.example
β βββ database.go
β βββ echo.go
β βββ logger.go
β βββ main.go
β βββ remoteconfig.go
βββ domain
βββ infrastructure
β βββ handler
β β βββ request
β β β βββ fields.go
β β β βββ parameter.go
β β β βββ token.go
β β βββ response
β β β βββ message.go
β β β βββ response.go
β β βββ router.go
β βββ postgres
βββ model
β βββ config.go
β βββ error.go
β βββ logger.go
β βββ messagehandler.go
β βββ model.go
β βββ model_test.go
β βββ remoteconfig.go
β βββ router.go
βββ sqlmigration
βββ go.mod
βββ README.md
βββ wizard-config.yaml
Once the installation is done, you can open your project folder:
cd my-app
After we init the project the go mod tidy
command will be executed to download the dependencies.
If you want to get more information about this command, run:
go-wizard help init
add package
command
With this command you can create the CRUD on the available layers that will read from a config yaml file that is
generated when you run the init command:
# if you don't specify this field, the wizard will automatically use the path of working directory (pwd)
# here you'll have to change the username and set the correct path to your project
project_path: /home/username/Documents/code/
# the module_name is used to create the imports
module_name: github.com/edteamlat/go-wizzard
# Is Use for the name of the structs of the different layers
model: UserRole
# Will be use for the table name and constraints
# also, it'll be converted to UpperCamelCase to be use
# for the Slice model
table: user_roles
# table_comment will be use as the comment for the new table
table_comment: Write your comment here
# choose the type of the dates (valid options: time.Time and int64 for unix)
time_type: time.Time
# choose the type of the primary key (valid options: uint, uuid.UUID)
id_type: uint
# a list of objects where you specify the name, type and if it allows nulls
# by default, the id, created_at and updated_at fields, will be added
fields:
- name: user_id
type: uint
is_null: false
- name: description
type: string
is_null: true
field_size: 255 # only for string type, if not specified, it'll be 255, if you put -1, it'll be of type TEXT instead of VARCHAR
- name: is_active
type: bool
is_null: false
- name: begins_at
type: time.Time
is_null: false
- name: priority
type: float64
numeric_precision: 10 # only for float type, if not specify, it'll be 10, must be a positive number
numeric_scale: 2 # only for float type, if not specified, it'll be 2, can be either positive or negative
# The available layers that we can generate
# if you don't want to use one, just remove it
layers:
- domain
- handler_echo # we only support the echo framework for now
- storage_postgres # we don't support other db system for now
- model
- sqlmigration_postgres # here we'll save the sql files to modify our db, it only supports postgres syntax for now
This config file is added on the root of your project with the name wizard-config.yaml
when you exec the init command.
To create a new package just fill the config file and exec the next command:
go-wizard add package
With the config file of above this command will generate the following:
βββ cmd
βββ domain
β βββ userrole
β βββ usecase.go
β βββ userrole.go
βββ infrastructure
β βββ handler
β β βββ request
β β βββ response
β β βββ userrole
β β β βββ handler.go
β β β βββ route.go
β βββ postgres
β βββ userrole
β βββ userrole.go
βββ model
β βββ userrole.go
βββ sqlmigration
β βββ 20210908_063849_create_user_roles_table.sql
Now you can enter into every file that was generated to see what's inside.
You can also pass the flag -c
to indicate the path of the config file:
go-wizard add package -c /home/username/Documents/code/my-app/wizard-config.yaml
If you want to get more information about this command, run:
go-wizard help add
π· Available types
Go Type |
Postgres type |
string |
VARCHAR(SIZE) and TEXT |
int |
INTEGER |
int8 |
INTEGER |
int16 |
INTEGER |
int32 |
INTEGER |
int64 |
INTEGER |
uint |
INTEGER |
uint8 |
INTEGER |
uint16 |
INTEGER |
uint32 |
INTEGER |
uint64 |
INTEGER |
float32 |
NUMERIC() |
float64 |
NUMERIC() |
bool |
BOOLEAN |
time.Time |
TIMESTAMP and INTEGER for unix |
uuid.UUID |
UUID |
json.RawMessage |
JSON |
If other types are used, the wizard will return set INVALID_TYPE as the type of the field, so if you want to use other
types, you'll have to open an issue or make a PR.