Skip to content

Commit

Permalink
API and documentation enhancements (#1)
Browse files Browse the repository at this point in the history
Comprehensive update to the buildinfo package

* Added LoadAsync function so that build information can be loaded
  from a separate goroutine.

* Documentation improvements.

* Github action integration.

* Create README.md

* Added code example.
  • Loading branch information
qqiao committed Mar 28, 2022
1 parent 81807e2 commit e8556a0
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 4 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '26 2 * * 4'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: test

on: [push]

jobs:
test:
runs-on: ubuntu-latest
container:
image: qqiao/dev-env:latest
options: --user root

steps:
- uses: actions/checkout@v2
- run: go test -v ./...
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# buildinfo

Buildinfo is a library that provides an easy solution to embed build information into any applications.

### Installation

The `buildinfo` package is a go module, so simply using:

go get github.com/qqiao/buildinfo

in your application's go module should make it available.

### Usage
Please refer to [source code documentation](https://pkg.go.dev/github.com/qqiao/buildinfo) for usage examples.
34 changes: 30 additions & 4 deletions buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,39 @@ type BuildInfo struct {
Revision string `json:"revision"`
}

// Load loads the build info from the given path.
func Load(path string) (BuildInfo, error) {
// Load loads the build information from the given path.
func Load(path string) (*BuildInfo, error) {
var buildInfo BuildInfo
file, err := ioutil.ReadFile(path)
if err != nil {
return buildInfo, err
return nil, err
}

return buildInfo, json.Unmarshal(file, &buildInfo)
if err := json.Unmarshal(file, &buildInfo); err != nil {
return nil, err
}

return &buildInfo, nil
}

// LoadAsync loads the build information from the given path.
//
// This function internally invokes the Load funtion in a separate goroutine.
// This allows the slow file I/O and JSON unmarshalling to be parallelized.
func LoadAsync(path string) (<-chan *BuildInfo, <-chan error) {
out := make(chan *BuildInfo)
errs := make(chan error)

go func() {
defer close(out)
defer close(errs)

if buildInfo, err := Load(path); err != nil {
errs <- err
} else {
out <- buildInfo
}
}()

return out, errs
}
73 changes: 73 additions & 0 deletions buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2017 Qian Qiao
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buildinfo_test

import (
"fmt"
"reflect"
"testing"

"github.com/qqiao/buildinfo"
)

var expected = &buildinfo.BuildInfo{
BuildTime: 1648396360413459000,
Revision: "e594b8e",
}

func ExampleLoad() {
buildInfo, err := buildinfo.Load("./test_data.json")
if err != nil {
// Handle the loading error
} else {
// Use/store the buildInfo
fmt.Println(buildInfo.Revision)
}
}

func ExampleLoadAsync() {
buildInfoCh, errCh := buildinfo.LoadAsync("./test_data.json")

select {
case <-errCh:
// Handle the loading error
case buildInfo := <-buildInfoCh:
// Use/store the buildInfo
fmt.Println(buildInfo.Revision)
}
}

func TestLoad(t *testing.T) {
if got, err := buildinfo.Load("./test_data.json"); err != nil {
t.Errorf("Error loading test data: %v", err)
} else {
if !reflect.DeepEqual(expected, got) {
t.Errorf("Expected: %v\nGot: %v", expected, got)
}
}
}

func TestLoadAsync(t *testing.T) {
gotCh, errCh := buildinfo.LoadAsync("./test_data.json")

select {
case err := <-errCh:
t.Errorf("Error loading test data: %v", err)
case got := <-gotCh:
if !reflect.DeepEqual(expected, got) {
t.Errorf("Expected: %v\nGot: %v", expected, got)
}
}
}
21 changes: 21 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 Qian Qiao
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
Package buildinfo contains helper functions to help embed build information
into an application.
*/
package buildinfo
4 changes: 4 additions & 0 deletions test_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"buildTime" : 1648396360413459000,
"revision" : "e594b8e"
}

0 comments on commit e8556a0

Please sign in to comment.