#development #golang #tools

James is your butler and helps you to create, build, debug, test and run your Go projects.

When you often create new apps using Go, it quickly becomes annoying when you realize all the steps it takes to configure the basics. You need to manually create the source files, version info requires more steps to be injected into the executable, using Visual Studio Code requires you to manually setup the tasks you want to run…

Using the go-james tool, you can automate and streamline this process. The tool will take care of initializing your project, running your project, debugging it, building it and running the tests.



Requirements

Installation

You can run the following command to install go-james:

1go get -u github.com/pieterclaerhout/go-james/cmd/go-james

This will create the go-james command in your $GOPATH/bin folder.

The tool is self-contained and doesn't have any external dependencies.

To install it manually, download the go-james executable from the releases and place it in $GOPATH/bin.

Updating

Simply run:

1go-james update

Starting a new project

To start a new project, you can use the new subcommand as follows:

1go-james new --path=<target path> \
2             --package=<package> \
3             --name=<name of your project> \
4             --description=<description of your project> \
5             --copyright=<copyright of your project> \
6             [--create-git-repo] \
7             [--overwrite]

When you run it, you'll get the following output:

 1➜ go-james new --path go-example --package github.com/pieterclaerhout/go-example
 2Creating: go-example/go-james.json
 3Creating: go-example/.vscode/launch.json
 4Creating: go-example/.vscode/tasks.json
 5Creating: go-example/LICENSE
 6Creating: go-example/.gitignore
 7Creating: go-example/README.md
 8Creating: go-example/library.go
 9Creating: go-example/cmd/go-example/main.go
10Creating: go-example/versioninfo/versioninfo.go
11go: creating new go.mod: module github.com/pieterclaerhout/go-example

It will automatically create the following folder and file structure:

 1go-example
 2├── .git
 3├── .gitignore
 4├── .vscode
 5│   ├── launch.json
 6│   └── tasks.json
 7├── LICENSE
 8├── README.md
 9├── cmd
10│   └── go-example
11│       ├── main.go
12│       └── main_test.go
13├── go-james.json
14├── go.mod
15├── library.go
16├── library_test.go
17├── scripts
18│   ├── post_build
19│   │   └── post_build.example.go
20│   └── pre_build
21│       └── pre_build.example.go
22└── versioninfo
23    └── versioninfo.go

An important file which is generated and can be used to further customize the project and it's settings is the go-james.json file which sits next to the go.mod file.

You can specify the following options:

  • --path: the path where the new project should be created, e.g. /home/username/go-example (if not specified, it will create a directory with the name of the prject in the current path)
  • --package: the main package for the new project, e.g. github.com/pieterclaerhout/go-example (defaults to the project name if specified)
  • --name: the name of the project, if not specified, the last part of the path is used
  • --description: the description of the project, used for the readme
  • --copyright: the copyright of the project, used for the readme
  • --create-git-repo: if specified, a local Git repository will be created for the project and the source files will automatically be committed.
  • --overwrite: if the destination path already exists, overwrite it (be careful, the original folder will be replaced)

Initializing an existing project

When you already have an existing folder structure, you can run the init command to add the missing pieces.

1go-james init

This command is supposed to run from the project's directory and doesn't take any arguments.

Building a project

From within the project root, run the build command to build the executable:

1go-james build [-v] [--output=<path>] [--goos=<os>] [--goarch=<arch>]

By default, the output is put in the build subdirectory but can be customized in the configuration file.

You can specify the following options:

  • -v: the packages which are built will be listed.
  • --output: you can override the default output path as specified in the configuration file.
  • --goos: you can override the GOOS environment variable which indicates for which OS you are compiling.
  • --goarch: you can override the GOARCH environment variable which indicates for which architecture you are compiling.

You can read more about the GOOS and GOARCH environment variables here.

As part of the build process, the versioninfo package will be filled with the following details:

With every build, these variables are automatically updated.

Pre-build and post-build hooks

Just before the build, if a file called <project_root>/scripts/pre_build/pre_build.go is present, it will be executed and will get a lot of info about the build injected. It's a plain Go file, so use whatever trick or tool you know. A sample pre-build script looks as follows:

 1package main
 2
 3import (
 4    "github.com/pieterclaerhout/go-james"
 5    "github.com/pieterclaerhout/go-log"
 6)
 7
 8func main() {
 9
10    args, err := james.ParseBuildArgs()
11    log.CheckError(err)
12
13    log.InfoDump(args, "pre_build arguments")
14
15}

You can also execute a script after the build. To do so, create a file <project_root>/scripts/post_build/post_build.go with contents similar to:

 1package main
 2
 3import (
 4    "github.com/pieterclaerhout/go-james"
 5    "github.com/pieterclaerhout/go-log"
 6)
 7
 8func main() {
 9
10    args, err := james.ParseBuildArgs()
11    log.CheckError(err)
12
13    log.InfoDump(args, "post_build arguments")
14
15}

To parse the arguments, you can use james.ParseBuildArgs().

The parameters it gets are are struct of the type james.BuildArgs:

 1james.BuildArgs{
 2  ProjectPath: "/Users/pclaerhout/Downloads/JonoFotografie/go-james",
 3  OutputPath: "build/go-james",
 4  GOOS: "darwin",
 5  GOARCH: "amd64",
 6  ProjectName: "go-james",
 7  ProjectDescription: "James is your butler and helps you to create, build, test and run your Go projects",
 8  ProjectCopyright: "© 2019 Copyright Pieter Claerhout",
 9  Version: "0.7.0",
10  Revision: "2065b13",
11  Branch: "master",
12  RawBuildCommand: []string{
13    "go",
14    "build",
15    "-o",
16    "build/go-james",
17    "-ldflags",
18    "-s -w -X github.com/pieterclaerhout/go-james/versioninfo.ProjectName=go-james -X 'github.com/pieterclaerhout/go-james/versioninfo.ProjectDescription=James is your butler and helps you to create, build, test and run your Go projects' -X 'github.com/pieterclaerhout/go-james/versioninfo.ProjectCopyright=© 2019 Copyright Pieter Claerhout' -X github.com/pieterclaerhout/go-james/versioninfo.Version=0.7.0 -X github.com/pieterclaerhout/go-james/versioninfo.Revision=2065b13 -X github.com/pieterclaerhout/go-james/versioninfo.Branch=master",
19    "-trimpath",
20    "github.com/pieterclaerhout/go-james/cmd/go-james",
21  },
22}

Packaging a project

From within the project root, run the package command to build the executable for windows / darwin / linux in the 386 and amd64 variants and compresses the result as a .zip (windows) or .tgz (linux / mac):

1go-james package [-v] [--concurrency=4]

By default, the output is put in the build subdirectory but can be customized in the configuration file.

The filenames which are constructed use the following convention:

1build/<project.name>_<goos>-<goarch>_v<project.version>.[zip,tgz]

The executable will be compressed and, if present in the project, the project's README.md file as well.

You can specify the following options:

  • -v: the packages which are built will be listed.
  • --concurrency: how many package processes should run in parallel, defaults to the number of CPUs.

As part of the build process, the versioninfo package will be filled with the following details:

  • versioninfo.ProjectName: the name of the project from the configuration file
  • versioninfo.ProjectDescription: the description of the project from the configuration file
  • versioninfo.Version: the version of the project from the configuration file
  • versioninfo.Revision: the current Git commit hash
  • versioninfo.Branch: the current Git branch name

With every build, these variables are automatically updated.

Debugging a project

From within the project root, run:

1go-james debug

This will build the project and run it's main target through the Delve debugger. If the dlv command is not yet present in your $GOPATH/bin folder, it will automaticall be installed the first time you run it.

When creating a new project or performing init on an existing project, it also configures debugging from within Visual Studio Code. It's a simple as setting one or more breakpoints and choose "Start" > "Debug" from the menu. It creates a launch configuration called Debug.

Running a project

From within the project root, run:

1go-james run <args>

This will build the project and run it's main target passing the <args> to the command.

Testing a project

From within the project root, run:

1go-james test

This will run all the tests defined in the package.

Installing the executable

To install the main executable of your project in $GOPATH/bin, simply run the install command.

This will build the project and install it in the $GOPATH/bin folder. The name of the executable is the basename of build output path (as specified in the configuration file.

1go-james uninstall

Uninstalling the executable

Similar to the install command, there is also an uninstall command which removes the executable from $GOPATH/bin.

1go-james uninstall

The config file go-james.json

When you create a new project or init an existing one, a go-james.json file will be created in the root of your project. This file can be used to configure the project. The full config file is as follows:

 1{
 2    "project": {
 3        "name": "go-example",
 4        "version": "1.0.0",
 5        "description": "",
 6        "copyright": "",
 7        "package": "github.com/pieterclaerhout/go-example",
 8        "main_package": "github.com/pieterclaerhout/go-example/cmd/go-example"
 9    },
10    "build": {
11        "output_path": "build/go-example",
12        "ld_flags": [
13            "-s",
14            "-w"
15        ],
16        "extra_args": [
17            "-trimpath"
18        ]
19    },
20    "test": {
21        "extra_args": []
22    }
23}

Project Config

  • name: the name of your project (will be availabme under <package>/versioninfo.ProjectName)
  • version: the version of your project (will be availabme under <package>/versioninfo.Version)
  • description: the description of your project (will be availabme under <package>/versioninfo.ProjectDescription)
  • copyright: the description of your project (will be availabme under <package>/versioninfo.ProjectCopyright)
  • package: the root package of your project
  • main_package: the full path to the main package of your app, defaults to <package>/cmd/<project-name>

Build Config

  • output_path: the path where the built executable should be placed. Defaults to build/<project-name>
  • ld_flags: the linker flags you want to use for building. You can find more info about these flags here.
  • extra_args: contains any extra command-line parameters you want to add to the go build command when you run go-james build.

Test Config

  • extra_args: contains any extra command-line parameters you want to add to the go test command when you run go-james test.

Updating go-james

Just run:

1go-james update

Bootstrapping go-james

If you want to build go-james from scratch, you can use the following command (or use the "bootstrap" build task in Visual Studio Code):

1go build -v -o build/go-james github.com/pieterclaerhout/go-james/cmd/go-james

If you have a version of go-james installed, you can use it to build itself.

Roadmap

To get an idea on what's coming, you can check the GitHub Milestones.