Skip to content

Commit 6213d1e

Browse files
authored
Initial basic client with CircleCI, MD lint, license, etc. (#1)
1 parent b021770 commit 6213d1e

File tree

8 files changed

+306
-2
lines changed

8 files changed

+306
-2
lines changed

.circleci/config.yml

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
version: 2
2+
3+
defaults: &defaults
4+
working_directory: /src
5+
docker:
6+
- image: golang:1.12
7+
8+
jobs:
9+
build:
10+
<<: *defaults
11+
steps:
12+
- checkout
13+
- run:
14+
name: Build Source
15+
command: go build ./...
16+
- run:
17+
name: Check Formatting
18+
command: test -z $(go fmt ./...)
19+
- run:
20+
name: Vet Source
21+
command: go vet -all ./...
22+
- run:
23+
name: Check for Lint
24+
command: |
25+
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.15.0
26+
golangci-lint run ./...
27+
- run:
28+
name: Run Tests
29+
command: go test -cover -race -timeout 60s ./...
30+
31+
get_source:
32+
<<: *defaults
33+
steps:
34+
- checkout
35+
- restore_cache:
36+
keys:
37+
- go-mod-{{ checksum "go.sum" }}
38+
- run:
39+
name: Populate Go Mod Cache
40+
command: |
41+
if [ ! -d /go/pkg/mod ]; then
42+
go mod download
43+
fi
44+
- save_cache:
45+
key: go-mod-{{ checksum "go.sum" }}
46+
paths:
47+
- '/go/pkg/mod'
48+
- persist_to_workspace:
49+
root: /
50+
paths:
51+
- go/pkg/mod/*
52+
- src/*
53+
54+
build_source:
55+
<<: *defaults
56+
steps:
57+
- attach_workspace:
58+
at: /
59+
- run:
60+
name: Build Source
61+
command: go build ./...
62+
63+
check_formatting:
64+
<<: *defaults
65+
steps:
66+
- attach_workspace:
67+
at: /
68+
- run:
69+
name: Check Formatting
70+
command: test -z $(go fmt ./...)
71+
72+
vet_source:
73+
<<: *defaults
74+
steps:
75+
- attach_workspace:
76+
at: /
77+
- run:
78+
name: Vet Source
79+
command: go vet -all ./...
80+
81+
lint_source:
82+
<<: *defaults
83+
steps:
84+
- attach_workspace:
85+
at: /
86+
- run:
87+
name: Install golangci-lint
88+
command: curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.15.0
89+
- run:
90+
name: Check for Lint
91+
command: golangci-lint run ./...
92+
93+
lint_markdown:
94+
docker:
95+
- image: circleci/ruby:2.4.1-node
96+
steps:
97+
- attach_workspace:
98+
at: .
99+
- run:
100+
name: Install markdownlint
101+
command: sudo npm install -g markdownlint-cli
102+
- run:
103+
name: Check for Lint
104+
command: markdownlint --config src/.markdownlint.json src/
105+
106+
unit_test:
107+
<<: *defaults
108+
steps:
109+
- attach_workspace:
110+
at: /
111+
- run:
112+
name: Run Tests
113+
command: go test -cover -race -timeout 60s ./...
114+
115+
workflows:
116+
version: 2
117+
118+
build_and_test:
119+
jobs:
120+
- get_source
121+
- build_source:
122+
requires:
123+
- get_source
124+
- check_formatting:
125+
requires:
126+
- get_source
127+
- vet_source:
128+
requires:
129+
- get_source
130+
- lint_source:
131+
requires:
132+
- get_source
133+
- lint_markdown:
134+
requires:
135+
- get_source
136+
- unit_test:
137+
requires:
138+
- get_source

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*~

.markdownlint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"default": true,
3+
"MD013": false
4+
}

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
# scs-remote-build-client
2-
Golang client for the Singularity Container Services (SCS) Remote Build Service
1+
# SCS Remote-Build Client
2+
3+
[![GoDoc](https://godoc.org/github.com/sylabs/scs-remote-build-client?status.svg)](https://godoc.org/github.com/sylabs/scs-remote-build-client)
4+
[![Build Status](https://circleci.com/gh/sylabs/scs-remote-build-client.svg?style=shield)](https://circleci.com/gh/sylabs/workflows/scs-remote-build-client)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/sylabs/scs-remote-build-client)](https://goreportcard.com/report/github.com/sylabs/scs-remote-build-client)
6+
7+
This project provides a Go client for the Singularity Container Services (SCS) Remote-Build Service.
8+
9+
## Quick Start
10+
11+
Install the [CircleCI Local CLI](https://circleci.com/docs/2.0/local-cli/). See the [Continuous Integration](#continuous-integration) section below for more detail.
12+
13+
To build and test:
14+
15+
```sh
16+
circleci build
17+
```
18+
19+
## Continuous Integration
20+
21+
This package uses [CircleCI](https://circleci.com) for Continuous Integration (CI). It runs automatically on commits and pull requests involving a protected branch. All CI checks must pass before a merge to a proected branch can be performed.
22+
23+
The CI checks are typically run in the cloud without user intervention. If desired, the CI checks can also be run locally using the [CircleCI Local CLI](https://circleci.com/docs/2.0/local-cli/).

client/client.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) 2019, Sylabs Inc. All rights reserved.
2+
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
3+
// distributed with the sources of this project regarding your rights to use or distribute this
4+
// software.
5+
6+
package client
7+
8+
import (
9+
"fmt"
10+
"io"
11+
"net/http"
12+
"net/url"
13+
)
14+
15+
// Config contains the client configuration.
16+
type Config struct {
17+
// Base URL of the service (https://keys.sylabs.io is used if not supplied).
18+
BaseURL string
19+
// Auth token to include in the Authorization header of each request (if supplied).
20+
AuthToken string
21+
// User agent to include in each request (if supplied).
22+
UserAgent string
23+
// HTTPClient to use to make HTTP requests (if supplied).
24+
HTTPClient *http.Client
25+
}
26+
27+
// DefaultConfig is a configuration that uses default values.
28+
var DefaultConfig = &Config{}
29+
30+
// Client describes the client details.
31+
type Client struct {
32+
// Base URL of the service.
33+
BaseURL *url.URL
34+
// Auth token to include in the Authorization header of each request (if supplied).
35+
AuthToken string
36+
// User agent to include in each request (if supplied).
37+
UserAgent string
38+
// HTTPClient to use to make HTTP requests.
39+
HTTPClient *http.Client
40+
}
41+
42+
// NewClient sets up a new Remote-Build Service client with the specified base URL and auth token.
43+
func NewClient(cfg *Config) (c *Client, err error) {
44+
if cfg == nil {
45+
cfg = DefaultConfig
46+
}
47+
48+
// Determine base URL
49+
bu := "https://build.sylabs.io"
50+
if cfg.BaseURL != "" {
51+
bu = cfg.BaseURL
52+
}
53+
baseURL, err := url.Parse(bu)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
c = &Client{
59+
BaseURL: baseURL,
60+
AuthToken: cfg.AuthToken,
61+
UserAgent: cfg.UserAgent,
62+
}
63+
64+
// Set HTTP client
65+
if cfg.HTTPClient != nil {
66+
c.HTTPClient = cfg.HTTPClient
67+
} else {
68+
c.HTTPClient = http.DefaultClient
69+
}
70+
71+
return c, nil
72+
}
73+
74+
// newRequest returns a new Request given a method, path, query, and optional body.
75+
func (c *Client) newRequest(method, path, rawQuery string, body io.Reader) (r *http.Request, err error) {
76+
u := c.BaseURL.ResolveReference(&url.URL{
77+
Path: path,
78+
RawQuery: rawQuery,
79+
})
80+
81+
r, err = http.NewRequest(method, u.String(), body)
82+
if err != nil {
83+
return nil, err
84+
}
85+
if v := c.AuthToken; v != "" {
86+
r.Header.Set("Authorization", fmt.Sprintf("BEARER %s", v))
87+
}
88+
if v := c.UserAgent; v != "" {
89+
r.Header.Set("User-Agent", v)
90+
}
91+
92+
return r, nil
93+
}

client/version.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2019, Sylabs Inc. All rights reserved.
2+
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
3+
// distributed with the sources of this project regarding your rights to use or distribute this
4+
// software.
5+
6+
package client
7+
8+
import (
9+
"context"
10+
"net/http"
11+
12+
jsonresp "github.com/sylabs/json-resp"
13+
)
14+
15+
const pathVersion = "/version"
16+
17+
// VersionInfo contains version information.
18+
type VersionInfo struct {
19+
Version string `json:"version"`
20+
}
21+
22+
// GetVersion gets version information from the Remote-Build Service. The context controls the lifetime of
23+
// the request.
24+
func (c *Client) GetVersion(ctx context.Context) (vi VersionInfo, err error) {
25+
req, err := c.newRequest(http.MethodGet, pathVersion, "", nil)
26+
if err != nil {
27+
return VersionInfo{}, err
28+
}
29+
30+
res, err := c.HTTPClient.Do(req.WithContext(ctx))
31+
if err != nil {
32+
return VersionInfo{}, err
33+
}
34+
defer res.Body.Close()
35+
36+
if err := jsonresp.ReadResponse(res.Body, &vi); err != nil {
37+
return VersionInfo{}, err
38+
}
39+
return vi, nil
40+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/sylabs/scs-remote-build-client
2+
3+
go 1.12
4+
5+
require github.com/sylabs/json-resp v0.5.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/sylabs/json-resp v0.5.0 h1:AWdKu6aS0WrkkltX1M0ex0lENrIcx5TISox902s2L2M=
2+
github.com/sylabs/json-resp v0.5.0/go.mod h1:anCzED2SGHHZQDubMuoVtwMuJZdpqQ+7iso8yDFm/nQ=

0 commit comments

Comments
 (0)