JimWen / gotouch

Customizable Project Creator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gotouch

go report GitHub go.mod Go version GitHub issues

gotouch easy way to create your projects.

Install, run on CLI, make your selections and start development.

Asciiname

Installation

Go install

go install github.com/denizgursoy/gotouch/cmd/gotouch@latest

Installation on Unix/macOS

The following will install gotouch into /usr/local/bin/:

curl -fsSL https://raw.githubusercontent.com/denizgursoy/gotouch/main/scripts/install.sh | sudo bash

Usage

Execute

gotouch

Gotouch will use default properties yaml if -f/--file argument is not provided. If you have custom properties yaml, execute

gotouch -f path-to-properties-yaml

Run with docker

Execute following command to run in a docker container. Change $(pwd) if you want to create project in another folder. -f argument can only be URL.

docker run -it -v $(pwd):/out --rm ghcr.io/denizgursoy/gotouch:latest

How Gotouch works

  1. Asks for user to select a project structure in properties yaml
  2. If the selected project's language is go, it will check whether go command is installed on the OS
  3. Asks for module name
  4. Asks for a choice of every question under the selected project structure in order and saves the choices
  5. Asks for changing the values of the selected project structure if any
  6. Add default to values
  7. Creates a new directory with module name's last part after last /
  8. Uncompress/checkout the template project of the selected project structure into the created directory
  9. If the selected project's language is go,updates the module's name in the go.mod with the value user entered, if there is no go.mod file, it creates the go.mod file
  10. Creates files, and adds dependencies of all selected choices
  11. Merges values under the selected project structure with the values of all selected choices and default values
  12. Walks through the newly created directory's content and templates every file with the merged values,

How to customize

Follow the steps below to create your properties yaml. The suggested way to share files, your template project, and the properties yaml is to host them in a git repository and use the raw URL of the files in the properties yaml.

  1. Create your template project
  2. Write your properties yaml file
  3. Share your properties yaml with the others

Create your template project

Template is a zip file that has your directories and files inside.Template can be created with package command. Files inside a template can have actions which will be templated with the values. As an example, If you have Port key in your values, {{ .Port }} will be replaced with the corresponding value.

package main

import (
	"io"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", getRoot)
	log.Fatalln(http.ListenAndServe(":{{ .Port }}", nil))
}

func getRoot(w http.ResponseWriter, r *http.Request) {
	io.WriteString(w, "Server got the request\n")
}

You can also use other go template library's capabilities such as conditions, iterating array values, etc. For more information see go template library.

Write your properties yaml file

Properties yaml is a list of what we call Project Structure as it can be seen below:

- name: Empty Project Layout #mandatory - used for displaying project in listing
  reference: https://go.dev/ #optional - is appended to name while prompting project name
  url: https://github.com/denizgursoy/go-touch-projects/raw/main/compressed/empty.zip #mandatory - url of template project
  language: go # go, golang 
  values: # optional
    Port: 8080
  questions: #optional
    - direction: Do you want Dockerfile? #mandatory
      canSkip: true #if true, there must be at least one choice. 
      choices:
        - choice: Yes
          files:
            - url: https://raw.githubusercontent.com/denizgursoy/go-touch-projects/main/Dockerfile
              pathFromRoot: Dockerfile
          values:
            isDocker: true

A Project Structure has name, reference, url, list of question and values.

After creating your template project, it should be hosted in a http server and url must be address of your template project. Gotouch downloads the template from the url and uncompress it.

Question

Question allows your users to customize their projects. A question must have a direction and choices. Gotouch prompts user to make a choice for every question in the selected Project Structure.

If a question has only one choice and canSkip is true, it is evaluated as Yes/No question

Example Yes/No question:

questions: #optional
  - direction: Do you want Dockerfile? #mandatory
    canSkip: true #if true, there must be at least one choice. 
    choices: #mandatory
      - choice: Yes
        files:
          - url: https://raw.githubusercontent.com/denizgursoy/go-touch-projects/main/Dockerfile
            pathFromRoot: Dockerfile

Yes/No will be displayed like:

Yes/No Question

If there are choices more than one, Gotouch will prompt user to select form list of choices. If can skip is true, user will have None of above choice as well.

Example select question:

questions:
  - direction: Which http library do you want to use?
    choices:
      - choice: Echo
        dependencies:
          - github.com/labstack/echo/v4
      - choice: Gorilla Mux
        dependencies:
          - github.com/gorilla/mux
      - choice: Gin
        dependencies:
          - github.com/gin-gonic/gin

Select question will be displayed like:

Select Question

Choice

If selected, a choice can create files, add dependencies and introduce new values.

Dependencies are list of string. If version of a dependency is not written Gotouch will append @latest and execute go get.

A choice can create files with address of source file, or content. A file entry must have pathFromRoot value is the location of the file inside project.

Creator of this yaml might want to customize project if a specific choice is selected, so values written under a choice will be appended to general value. Values of choice cannot be changed by the user.

A choice can be written like:

- choice: Yes
    dependencies:
      - github.com/labstack/echo/v4
    files:
      - url: https://raw.githubusercontent.com/denizgursoy/go-touch-projects/main/Dockerfile
        pathFromRoot: Dockerfile
      - content: "My input"
        pathFromRoot: input.txt
    values:
      httpLibrary: echo

Values

If you want some part of the source code not to be hardcoded, you can define custom values under the Project Strcuture. The most common cases can be port numbers, service addresses, and some configuration values, etc. Gotouch will ask user to change the values if he/she wants.

Edit Values

Gotouch uses editor of survey. It launches your default editor for YAML. If you want to change your editor, set $VISUAL or $EDITOR environment variables.

Vim editor

When you exit your the editor, gotouch will save the values and continue creating the project.

Values under Project Structure will be merged with all selected choices' values.

Apart from these values, you can use following predefined values :

ModuleName: Module name user typed
ProjectName: Project directory name
WorkingDirectory: location where Gotouch command is executed
ProjectFullPath: Projects full path which is actually WorkingDirectory / ProjectName
Dependencies: Array of dependencies of all selected choices

You can also use values in your directories. If you, for example, have a directory {{ .ProjectName }}, It will be replaced with the corresponding values.

Share your properties yaml with others

If you share your properties yaml with us, we can add it to list so that other people can use it.

Commands

gotouch command

gotouch --file path-to-yaml

gotouch command uses properties yaml file for prompting user to enter name and select project structure. If file flag value is not provided, it is going to use default properties yaml. Firstly,The command asks for project name. Project name is written to go module and used for directory name.

package subcommand

gotouch package --source path-to-source --target path-to-target

Package command compresses the source directory with the .tar.gz extension and moves the zip file to target directory. source and target flags are optional. Default values for source and target are ./, ../ respectively.

Package command ignores following files/directories:

  1. __MACOS
  2. .DS_Store
  3. .idea
  4. .vscode
  5. .git

validate subcommand

gotouch validate --file path-to-yaml

Validate checks if your yaml is valid or not.

About

Customizable Project Creator

License:MIT License


Languages

Language:Go 98.6%Language:Shell 0.7%Language:Makefile 0.6%Language:Dockerfile 0.1%