Skip to content

Go project wire frame building

suntong edited this page Jun 6, 2021 · 19 revisions

wire-frame building

github-create-repo - Create Repository in Github

To create repository in Github:

 # Create an organization Github Repository 
$ easygen github-create-repo.tmpl wireframe_proj.yaml
curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/orgs/repos -d '{"name":"wireframe", "description": "wire-frame construction to get the project quickly into shape", "auto_init": true, "license_template": "mit", "gitignore_template": "Go"}'

 # Create a normal user Github Repository 
sed 's/^  Vendor: go-easygen/  User: suntong/' wireframe_proj.yaml > /tmp/wireframe_proj.yaml

$ easygen github-create-repo.tmpl /tmp/wireframe_proj.yaml
curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user/repos -d '{"name":"wireframe", "description": "wire-frame construction to get the project quickly into shape", "auto_init": true, "license_template": "mit", "gitignore_template": "Go"}'

Then copy the generated curl command and do it on the command line (in the terminal).

Notes,

  • The easygen is the universal code/text generator, an very important component of this Go wireframe project, and it is available here.
  • The GitHub Token is for accessing GitHub API to create repository or deploy the artefacts to GitHub etc. You can create one here.

gitlab-repo-create - Create Repository in Gitlab

To create repository in Gitlab:

# set everything
ghrn=wireframe
ghrd='wire-frame construction to get the project quickly into shape'
ghun=go-easygen
GITLAB_TOKEN=xxxx

# get namespace_id
namespace_id=`curl -s --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v3/namespaces" | jq --arg name $ghun '.[] | select(.name==$name) | .id'`

# then do it
curl -H "Content-Type:application/json" https://gitlab.com/api/v3/projects?private_token=$GITLAB_TOKEN -d "{ \"name\": \"$ghrn\", \"description\": \"$ghrd\", \"namespace_id\": $namespace_id"',"only_allow_merge_if_build_succeeds":true,"only_allow_merge_if_all_discussions_are_resolved":true}'

Do the above on the command line (in the terminal).

Data type definition

Defining Go Data types from a data dictionary is an important step in wire-frame building too. There are many ready build tools available. Choose one that suits you. Or, you can use the simple JSON to struct from jsonfiddle:

Expand to check out -- Example of Data type definition
# suppose our data dictionary looks like this:
$ cat wireframe_full.yaml
Wireframe:
  Proj: wireframe
  Desc: wire-frame construction to get the project quickly into shape
  Lang: Go
  User: <empty>
  Vendor: go-easygen
  Author: Tong Sun <suntong at cpan.org>
  License: MIT

# then generate Go code from above data dictionary using jsonfiddle j2s
$ jsonfiddle j2s -f yaml -i wireframe_full.yaml --name WireframeT | sed '/Wireframe\b/d; s/ `yaml:.*$//' | gofmt | tee WireframeT.go
package main

type WireframeT struct {
        Author  string
        Desc    string
        Lang    string
        License string
        Proj    string
        User    string
        Vendor  string
}

The jsonfiddle is the JSON Fiddling tool that makes it easy to look at the JSON data from different aspects, which is available here.

Command line flag handling code auto-generation

Refer to

Command line flag handling code auto-generation

github-create-release - Create Release in Github

GITHUB_TOKEN=...

GITHUB_TAG=1.0.0
GITHUB_RELEASE_TEXT="Release v$GITHUB_TAG"

git push

$ easygen ../../go-easygen/wireframe/github-create-release.tmpl wireframe_proj.yaml
curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/go-easygen/wireframe/releases -d '{"tag_name":"'$GITHUB_TAG'", "name": "wireframe-'$GITHUB_TAG'", "body": "'"$GITHUB_RELEASE_TEXT"'"}'

Then copy and do the curl command for wireframe on the command line. E.g.:

curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/go-easygen/wireframe/releases -d '{"tag_name":"'$GITHUB_TAG'", "name": "wireframe-'$GITHUB_TAG'", "body": "'"$GITHUB_RELEASE_TEXT"'"}'

which can yield:

Expand to check out execution result
{
  "url": "https://api.github.com/repos/go-easygen/wireframe/releases/14826407",
  "assets_url": "https://api.github.com/repos/go-easygen/wireframe/releases/14826407/assets",
  "upload_url": "https://uploads.github.com/repos/go-easygen/wireframe/releases/14826407/assets{?name,label}",
  "html_url": "https://github.com/go-easygen/wireframe/releases/tag/1.0.0",
  "id": 14826407,
  "node_id": "MDc6UmVsZWFzZTE0ODI2NDA3",
  "tag_name": "1.0.0",
  "target_commitish": "master",
  "name": "wireframe-1.0.0",
  "draft": false,
  "author": {
    "login": "suntong",
    "id": 422244,
    "node_id": "MDQ6VXNlcjQyMjI0NA==",
    "avatar_url": "https://avatars1.githubusercontent.com/u/422244?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/suntong",
    "html_url": "https://github.com/suntong",
    "followers_url": "https://api.github.com/users/suntong/followers",
    "following_url": "https://api.github.com/users/suntong/following{/other_user}",
    "gists_url": "https://api.github.com/users/suntong/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/suntong/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/suntong/subscriptions",
    "organizations_url": "https://api.github.com/users/suntong/orgs",
    "repos_url": "https://api.github.com/users/suntong/repos",
    "events_url": "https://api.github.com/users/suntong/events{/privacy}",
    "received_events_url": "https://api.github.com/users/suntong/received_events",
    "type": "User",
    "site_admin": false
  },
  "prerelease": false,
  "created_at": "2019-01-07T03:51:46Z",
  "published_at": "2019-01-07T04:15:30Z",
  "assets": [

  ],
  "tarball_url": "https://api.github.com/repos/go-easygen/wireframe/tarball/1.0.0",
  "zipball_url": "https://api.github.com/repos/go-easygen/wireframe/zipball/1.0.0",
  "body": "Release v1.0.0"
}

For ffcvt:

$ easygen ../../go-easygen/wireframe/github-create-release.tmpl ffcvt_proj.yaml 
curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/suntong/ffcvt/releases -d '{"tag_name":"'$GITHUB_TAG'", "name": "ffcvt-'$GITHUB_TAG'", "body": "'"$GITHUB_RELEASE_TEXT"'"}'

GITHUB_TAG=1.3.2
GITHUB_RELEASE_TEXT="Add subtitle streams copy support"
curl -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/suntong/ffcvt/releases -d '{"tag_name":"'$GITHUB_TAG'", "name": "ffcvt-'$GITHUB_TAG'", "body": "'"$GITHUB_RELEASE_TEXT"'"}'

will get:

Expand to check out execution result
{
  "url": "https://api.github.com/repos/suntong/ffcvt/releases/14826435",
  "assets_url": "https://api.github.com/repos/suntong/ffcvt/releases/14826435/assets",
  "upload_url": "https://uploads.github.com/repos/suntong/ffcvt/releases/14826435/assets{?name,label}",
  "html_url": "https://github.com/suntong/ffcvt/releases/tag/1.3.2",
  "id": 14826435,
  "node_id": "MDc6UmVsZWFzZTE0ODI2NDM1",
  "tag_name": "1.3.2",
  "target_commitish": "master",
  "name": "ffcvt-1.3.2",
  "draft": false,
  "author": {
    "login": "suntong",
    "id": 422244,
    "node_id": "MDQ6VXNlcjQyMjI0NA==",
    "avatar_url": "https://avatars1.githubusercontent.com/u/422244?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/suntong",
    "html_url": "https://github.com/suntong",
    "followers_url": "https://api.github.com/users/suntong/followers",
    "following_url": "https://api.github.com/users/suntong/following{/other_user}",
    "gists_url": "https://api.github.com/users/suntong/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/suntong/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/suntong/subscriptions",
    "organizations_url": "https://api.github.com/users/suntong/orgs",
    "repos_url": "https://api.github.com/users/suntong/repos",
    "events_url": "https://api.github.com/users/suntong/events{/privacy}",
    "received_events_url": "https://api.github.com/users/suntong/received_events",
    "type": "User",
    "site_admin": false
  },
  "prerelease": false,
  "created_at": "2019-01-07T02:56:43Z",
  "published_at": "2019-01-07T04:20:21Z",
  "assets": [

  ],
  "tarball_url": "https://api.github.com/repos/suntong/ffcvt/tarball/1.3.2",
  "zipball_url": "https://api.github.com/repos/suntong/ffcvt/zipball/1.3.2",
  "body": "Add subtitle streams copy support"
}

Binary releases

Binary releases are done via GitHub Actions with the help of goreleaser.
This Go wireframe project make it easier to generate the .goreleaser.yml automatically, from the github-goreleaser.tmpl file.

easygen ../../go-easygen/wireframe/github-goreleaser.tmpl *_proj.yaml | tee .goreleaser.yml

which will produce:

Expand to check out execution result
project_name: wireframe

archives:
  - format: tar.gz
    wrap_in_directory: true
    format_overrides:
      - goos: windows
        format: zip
    # remove README and LICENSE
    files:
      - none*

builds:
  - env: [CGO_ENABLED=0]
    goos:
      - linux
      - windows
      - darwin
    goarch:
      - amd64
      - arm64

nfpms:
- maintainer: Tong Sun <suntong at cpan.org>
  description: wire-frame construction to get the project quickly into shape
  homepage: https://github.com/go-easygen/wireframe
  license: MIT
  formats:
  - deb
  - rpm
  - apk

Note, by reading from the _proj.yaml file, the output is dynamic, and targets the project being worked on well. For e.g., for ffcvt, the last part is:

Expand to check out details
project_name: ffcvt

. . . 

nfpms:
- maintainer: Tong Sun <suntong at cpan.org>
  description: ffmpeg convert wrapper tool
  homepage: https://github.com/suntong/ffcvt
  license: MIT
  formats:
  - deb
  - rpm
  - apk

Then,

  • inspect the generated file, and.
  • copy .github/workflows/go-release-build.yml from this repo to yours,
  • edit both files to your likes, then
  • try to do a git push and manually fix any remaining issues.

That's it. Most of the cookie-cutting chores should have been taken care of by this Go wireframe project.