Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial buildscripts, makefile #10

Merged
merged 2 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ config.toml

# build
pb

# OS Files
.DS_Store
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
linters:
disable-all: true
enable:
- typecheck
- goimports
- misspell
- govet
- revive
- ineffassign
- gomodguard
- gofmt
- unused
- gofumpt

linters-settings:
golint:
min-confidence: 0

misspell:
locale: US

gofumpt:
lang-version: "1.17"

# Choose whether or not to use the extra rules that are disabled
# by default
extra-rules: false

issues:
exclude-use-default: false
exclude:
- instead of using struct literal
- should have a package comment
- should have comment or be unexported
- time-naming
- error strings should not be capitalized or end with punctuation or a newline

service:
golangci-lint-version: 1.43.0 # use the fixed version to not introduce new linters unexpectedly
57 changes: 57 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
PWD := $(shell pwd)
GOPATH := $(shell go env GOPATH)
LDFLAGS := $(shell go run buildscripts/gen-ldflags.go)

GOARCH := $(shell go env GOARCH)
GOOS := $(shell go env GOOS)

VERSION ?= $(shell git describe --tags)
TAG ?= "parseablehq/pb:$(VERSION)"

all: build

checks:
@echo "Checking dependencies"
@(env bash $(PWD)/buildscripts/checkdeps.sh)

getdeps:
@mkdir -p ${GOPATH}/bin
@echo "Installing golangci-lint" && curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin
@echo "Installing stringer" && go install -v golang.org/x/tools/cmd/stringer@latest
@echo "Installing staticheck" && go install honnef.co/go/tools/cmd/staticcheck@latest

crosscompile:
@(env bash $(PWD)/buildscripts/cross-compile.sh)

verifiers: getdeps vet lint

docker: build
@docker build -t $(TAG) . -f Dockerfile.dev

vet:
@echo "Running $@"
@GO111MODULE=on go vet $(PWD)/...

lint:
@echo "Running $@ check"
@GO111MODULE=on ${GOPATH}/bin/golangci-lint run --timeout=5m --config ./.golangci.yml
@GO111MODULE=on ${GOPATH}/bin/staticcheck -tests=false -checks="all,-ST1000,-ST1003,-ST1016,-ST1020,-ST1021,-ST1022,-ST1023,-ST1005" ./...

# Builds pb locally.
build: checks
@echo "Building pb binary to './pb'"
@GO111MODULE=on CGO_ENABLED=0 go build -trimpath -tags kqueue --ldflags "$(LDFLAGS)" -o $(PWD)/pb

# Builds pb and installs it to $GOPATH/bin.
install: build
@echo "Installing pb binary to '$(GOPATH)/bin/pb'"
@mkdir -p $(GOPATH)/bin && cp -f $(PWD)/pb $(GOPATH)/bin/pb
@echo "Installation successful. To learn more, try \"pb --help\"."

clean:
@echo "Cleaning up all the generated files"
@find . -name '*.test' | xargs rm -fv
@find . -name '*~' | xargs rm -fv
@rm -rvf pb
@rm -rvf build
@rm -rvf release
55 changes: 55 additions & 0 deletions buildscripts/cross-compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
#
# Copyright (c) 2023 Cloudnatively Services Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

set -e

# Enable tracing if set.
[ -n "$BASH_XTRACEFD" ] && set -x

function _init() {
## All binaries are static make sure to disable CGO.
export CGO_ENABLED=0

## List of architectures and OS to test coss compilation.
SUPPORTED_OSARCH="linux/amd64 linux/arm64 darwin/arm64 darwin/amd64 windows/amd64"
}

function _build() {
local osarch=$1
IFS=/ read -r -a arr <<<"$osarch"
os="${arr[0]}"
arch="${arr[1]}"
package=$(go list -f '{{.ImportPath}}')
printf -- "--> %15s:%s\n" "${osarch}" "${package}"

# Go build to build the binary.
export GOOS=$os
export GOARCH=$arch
export GO111MODULE=on
export CGO_ENABLED=0
go build -tags kqueue -o /dev/null
}

function main() {
echo "Testing builds for OS/Arch: ${SUPPORTED_OSARCH}"
for each_osarch in ${SUPPORTED_OSARCH}; do
_build "${each_osarch}"
done
}

_init && main "$@"
118 changes: 118 additions & 0 deletions buildscripts/gen-ldflags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//go:build ignore
// +build ignore

// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)

func genLDFlags(version string) string {
releaseTag, date := releaseTag(version)
copyrightYear := fmt.Sprintf("%d", date.Year())

var ldflagsStr string
ldflagsStr = "-s -w -X github.com/parseablehq/pb/cmd.Version=" + version + " "
ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.CopyrightYear=" + copyrightYear + " "
ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.ReleaseTag=" + releaseTag + " "
ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.CommitID=" + commitID() + " "
ldflagsStr = ldflagsStr + "-X github.com/parseablehq/pb/cmd.ShortCommitID=" + commitID()[:12]
return ldflagsStr
}

// releaseTag prints release tag to the console for easy git tagging.
func releaseTag(version string) (string, time.Time) {
relPrefix := "DEVELOPMENT"
if prefix := os.Getenv("PB_RELEASE"); prefix != "" {
relPrefix = prefix
}

relSuffix := ""
if hotfix := os.Getenv("PB_HOTFIX"); hotfix != "" {
relSuffix = hotfix
}

relTag := strings.ReplaceAll(version, " ", "-")
relTag = strings.ReplaceAll(relTag, ":", "-")
t, err := time.Parse("2006-01-02T15-04-05Z", relTag)
if err != nil {
panic(err)
}

relTag = strings.ReplaceAll(relTag, ",", "")
relTag = relPrefix + "." + relTag
if relSuffix != "" {
relTag += "." + relSuffix
}

return relTag, t
}

// commitID returns the abbreviated commit-id hash of the last commit.
func commitID() string {
// git log --format="%h" -n1
var (
commit []byte
e error
)
cmdName := "git"
cmdArgs := []string{"log", "--format=%H", "-n1"}
if commit, e = exec.Command(cmdName, cmdArgs...).Output(); e != nil {
fmt.Fprintln(os.Stderr, "Error generating git commit-id: ", e)
os.Exit(1)
}

return strings.TrimSpace(string(commit))
}

func commitTime() time.Time {
// git log --format=%cD -n1
var (
commitUnix []byte
err error
)
cmdName := "git"
cmdArgs := []string{"log", "--format=%cI", "-n1"}
if commitUnix, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err)
os.Exit(1)
}

t, err := time.Parse(time.RFC3339, strings.TrimSpace(string(commitUnix)))
if err != nil {
fmt.Fprintln(os.Stderr, "Error generating git commit-time: ", err)
os.Exit(1)
}

return t.UTC()
}

func main() {
var version string
if len(os.Args) > 1 {
version = os.Args[1]
} else {
version = commitTime().Format(time.RFC3339)
}

fmt.Println(genLDFlags(version))
}
15 changes: 7 additions & 8 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
Expand All @@ -25,27 +24,27 @@ import (
"time"
)

type HttpClient struct {
type HTTPClient struct {
client http.Client
profile *config.Profile
}

func DefaultClient() HttpClient {
return HttpClient{
func DefaultClient() HTTPClient {
return HTTPClient{
client: http.Client{
Timeout: 60 * time.Second,
},
profile: &DefaultProfile,
}
}

func (client *HttpClient) baseApiUrl(path string) (x string) {
x, _ = url.JoinPath(client.profile.Url, "api/v1/", path)
func (client *HTTPClient) baseAPIURL(path string) (x string) {
x, _ = url.JoinPath(client.profile.URL, "api/v1/", path)
return
}

func (client *HttpClient) NewRequest(method string, path string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, client.baseApiUrl(path), body)
func (client *HTTPClient) NewRequest(method string, path string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, client.baseAPIURL(path), body)
if err != nil {
return
}
Expand Down
20 changes: 9 additions & 11 deletions cmd/pre.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
Expand All @@ -27,21 +26,20 @@ import (

var DefaultProfile config.Profile

// Check if a profile exists.
// PreRunDefaultProfile if a profile exists.
// This is required by mostly all commands except profile
func PreRunDefaultProfile(cmd *cobra.Command, args []string) error {
func PreRunDefaultProfile(_ *cobra.Command, _ []string) error {
conf, err := config.ReadConfigFromFile()
if err != nil {
if os.IsNotExist(err) {
return errors.New("no config found to run this command. add a profile using pb profile command")
} else {
return err
}
if os.IsNotExist(err) {
return errors.New("no config found to run this command. add a profile using pb profile command")
} else if err != nil {
return err
}
if conf.Profiles == nil || conf.Default_profile == "" {

if conf.Profiles == nil || conf.DefaultProfile == "" {
return errors.New("no profile is configured to run this command. please create one using profile command")
}

DefaultProfile = conf.Profiles[conf.Default_profile]
DefaultProfile = conf.Profiles[conf.DefaultProfile]
return nil
}
Loading