Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Display version, our license, and vendor attributions in version comm…
Browse files Browse the repository at this point in the history
…and (0xPolygon#407)

* Embed proper version by ldflags

* Set app version in goreleaser.yml

* Add our license in version command

* Add attribution for dependencies with BSD-2 or 3

* Remove license and attributions from version command and add license command

Move version, license, and BSD attributions into params package

Remove blank line between license and attributions on license command

* Fix lint error

* Add generate_dependency_licenses.sh

* Update modules dependencies to match go1.16

Signed-off-by: Luca Georges Francois <luca.georges-francois@epitech.eu>

* Update min. go version in CI tests

Signed-off-by: Luca Georges Francois <luca.georges-francois@epitech.eu>

* Rename BsdLicsense to BSDLicense

* Separate params package into licenses and versioning

* Fix main.go

* Remove init function from main

* Fix lint error

* Fix wrong path in Makefile

* Add archive settings in goreleaser.yml in order to ignore licenses directory to be included in zip

* Add README.md in archives in goreleaser.yml

Co-authored-by: Luca Georges Francois <luca.georges-francois@epitech.eu>
  • Loading branch information
Kourin1996 and 0xpanoramix authored Feb 21, 2022
1 parent af0029b commit cee68ff
Show file tree
Hide file tree
Showing 13 changed files with 550 additions and 37 deletions.
14 changes: 10 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ builds:
- CC=o64-clang
- CXX=o64-clang++
ldflags:
-s -w
-s -w -X 'github.com/0xPolygon/polygon-edge/versioning.Version=v{{ .Version }}'

- id: darwin-arm64
main: ./main.go
Expand All @@ -30,7 +30,7 @@ builds:
- CC=oa64-clang
- CXX=oa64-clang++
ldflags:
-s -w
-s -w -X 'github.com/0xPolygon/polygon-edge/versioning.Version=v{{ .Version }}'

- id: linux-amd64
main: ./main.go
Expand All @@ -44,7 +44,7 @@ builds:
- CXX=g++
ldflags:
# We need to build a static binary because we are building in a glibc based system and running in a musl container
-s -w -linkmode external -extldflags "-static"
-s -w -linkmode external -extldflags "-static" -X 'github.com/0xPolygon/polygon-edge/versioning.Version=v{{ .Version }}'

- id: linux-arm64
main: ./main.go
Expand All @@ -58,7 +58,13 @@ builds:
- CXX=aarch64-linux-gnu-g++
ldflags:
# We need to build a static binary because we are building in a glibc based system and running in a musl container
-s -w -linkmode external -extldflags "-static"
-s -w -linkmode external -extldflags "-static" -X 'github.com/0xPolygon/polygon-edge/versioning.Version=v{{ .Version }}'

archives:
-
files:
- LICENSE
- README.md

#nfpms:
# - vendor: 0xPolygon
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ protoc:
protoc --go_out=. --go-grpc_out=. ./txpool/proto/*.proto
protoc --go_out=. --go-grpc_out=. ./consensus/ibft/**/*.proto

.PHONY: build
build:
$(eval LATEST_VERSION = $(shell git describe --tags --abbrev=0))
$(eval COMMIT_HASH = $(shell git rev-parse --short HEAD))
go build -ldflags="-X 'github.com/0xPolygon/polygon-edge/versioning.Version=$(LATEST_VERSION)+$(COMMIT_HASH)'" main.go

.PHONY: lint
lint:
golangci-lint run -E whitespace -E wsl -E wastedassign -E unconvert -E tparallel -E thelper -E stylecheck -E prealloc \
-E predeclared -E nlreturn -E misspell -E makezero -E lll -E importas -E ifshort -E gosec -E gofmt -E goconst \
-E forcetypeassert -E dogsled -E dupl -E errname -E errorlint -E nolintlint
-E forcetypeassert -E dogsled -E dupl -E errname -E errorlint -E nolintlint

.PHONY: generate-bsd-licenses
generate-bsd-licenses:
./generate_dependency_licenses.sh BSD-3-Clause,BSD-2-Clause > ./licenses/bsd_licenses.json
82 changes: 82 additions & 0 deletions command/license/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package license

import (
"bytes"
"fmt"

"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/licenses"
)

// LicenseCommand is the command to show the version of the agent
type LicenseCommand struct {
helper.Base
}

// DefineFlags defines the command flags
func (c *LicenseCommand) DefineFlags() {
c.Base.DefineFlags()
}

// GetHelperText returns a simple description of the command
func (c *LicenseCommand) GetHelperText() string {
return "Returns Polygon Edge license and dependency attributions"
}

func (c *LicenseCommand) GetBaseCommand() string {
return "license"
}

// Help implements the cli.Command interface
func (c *LicenseCommand) Help() string {
c.DefineFlags()

return helper.GenerateHelp(c.Synopsis(), helper.GenerateUsage(c.GetBaseCommand(), c.FlagMap), c.FlagMap)
}

// Synopsis implements the cli.Command interface
func (c *LicenseCommand) Synopsis() string {
return c.GetHelperText()
}

// Run implements the cli.Command interface
func (c *LicenseCommand) Run(args []string) int {
bsdLicenses, err := licenses.GetBSDLicenses()
if err != nil {
c.UI.Error(err.Error())

return 1
}

var buffer bytes.Buffer

buffer.WriteString("\n[LICENSE]\n\n")
buffer.WriteString(licenses.License)

buffer.WriteString("\n[DEPENDENCY ATTRIBUTIONS]\n\n")

for idx, l := range bsdLicenses {
// put a blank line between attributions
if idx != 0 {
buffer.WriteString("\n")
}

name := l.Name
if l.Version != nil {
name += " " + *l.Version
}

buffer.WriteString(fmt.Sprintf(
" This product bundles %s,\n"+
" which is available under a \"%s\" license.\n"+
" For details, see %s.\n",
name,
l.Type,
l.Path,
))
}

c.UI.Info(buffer.String())

return 0
}
5 changes: 5 additions & 0 deletions command/util/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/0xPolygon/polygon-edge/command/genesis"
"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/command/ibft"
"github.com/0xPolygon/polygon-edge/command/license"
"github.com/0xPolygon/polygon-edge/command/loadbot"
"github.com/0xPolygon/polygon-edge/command/monitor"
"github.com/0xPolygon/polygon-edge/command/peers"
Expand Down Expand Up @@ -39,6 +40,7 @@ func Commands() map[string]cli.CommandFactory {
monitorCmd := monitor.MonitorCommand{Base: base, Formatter: formatter, GRPC: grpc}
statusCmd := status.StatusCommand{Base: base, Formatter: formatter, GRPC: grpc}
versionCmd := version.VersionCommand{Base: base, Formatter: formatter}
licenseCmd := license.LicenseCommand{Base: base}
backupCmd := backup.BackupCommand{Base: base, Formatter: formatter, GRPC: grpc}

ibftCmd := ibft.IbftCommand{}
Expand Down Expand Up @@ -140,6 +142,9 @@ func Commands() map[string]cli.CommandFactory {
versionCmd.GetBaseCommand(): func() (cli.Command, error) {
return &versionCmd, nil
},
licenseCmd.GetBaseCommand(): func() (cli.Command, error) {
return &licenseCmd, nil
},
backupCmd.GetBaseCommand(): func() (cli.Command, error) {
return &backupCmd, nil
},
Expand Down
18 changes: 14 additions & 4 deletions command/version/version.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package version

import (
"bytes"

"github.com/0xPolygon/polygon-edge/command/helper"
"github.com/0xPolygon/polygon-edge/version"
"github.com/0xPolygon/polygon-edge/versioning"
)

// VersionCommand is the command to show the version of the agent
Expand Down Expand Up @@ -46,15 +48,23 @@ func (c *VersionCommand) Run(args []string) int {
return 1
}

c.Formatter.OutputResult(&VersionResult{Verstion: version.Version})
c.Formatter.OutputResult(&VersionResult{
Version: versioning.Version,
})

return 0
}

type VersionResult struct {
Verstion string `json:"version"`
Version string `json:"version"`
}

func (r *VersionResult) Output() string {
return version.GetVersion()
var buffer bytes.Buffer

buffer.WriteString("\n[POLYGON EDGE VERSION]\n")
buffer.WriteString(r.Version)
buffer.WriteString("\n")

return buffer.String()
}
165 changes: 165 additions & 0 deletions generate_dependency_licenses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/bin/bash

# generate_dependency_licenses.sh
# Generate dependency package list in json
# How to use
# ./generate_dependency_licenses.sh [LICSENSE_TYPE1,LICENSE_TYPE_2,...]
# Example
# ./generate_dependency_licenses.sh BSD-3-Clause,BSD-2-Clause
#
# Please install go-license before running this script
# go get github.com/google/go-licenses

# Constants
PACKAGE="github.com/0xPolygon/polygon-edge"

# Arguments
licenses=( `echo $1 | tr -s ',' ' '`)

# Get the version of given package from go.mod or go.sum
get_package_version() {
package=$1

# Get version from go.mod first
gomod_res=`cat go.mod | grep $package`
if [ ${#gomod_res} -gt 0 ]; then
res=(`echo $gomod_res | tr -s ',' ' '`)
echo "${res[1]}"

return 0
fi

# Get version from go.sum if not found
# There may be multiple versions in go.sum and take the last
tmp_ifs=$IFS
IFS=$'\n'
gosum_res=(`cat go.sum | grep $package`)
IFS=$tmp_ifs

if [ ${#gosum_res} -gt 0 ]; then
last=${gosum_res[${#gosum_res[@]} - 1]}
arr=(`echo $last | tr -s ',' ' '`)
# Remove suffix from version
version=`echo "${arr[1]}" | sed -e 's/\/go.mod//g' | sed -e 's/\+incompatible//g'`
echo "$version"

return 0
fi
}

# Print package information in JSON
print_dependency_json() {
name=$1
license=$2
path=$3
version=$4

if [ ${#version} -ne 0 ]
then
# with version
printf '
{
"name": "%s",
"version": "%s",
"type": "%s",
"path": "%s"
}' "${name}" "${version}" "${license}" "${path}"
else
# without version
printf '
{
"name": "%s",
"type": "%s",
"path": "%s"
}' "${name}" "${license}" "${path}"
fi
}

echo -n '['

count=0
# Run go-licenses and process each line
go-licenses csv $PACKAGE | while read line; do
# Parse line
res=(`echo $line | tr -s ',' ' '`)
package=${res[0]}
fullpath=${res[1]}
license=${res[2]}

# Filter by license
if [ "$license" = "Unknown" ]; then
continue
fi
if [ ${#licenses[@]} -gt 0 ]; then
echo ${licenses[@]} | xargs -n 1 | grep -E "^${license}$" > /dev/null
if [ $? -ne 0 ]; then
# not found
continue
fi
fi

# Get version from go.mod or go.sum
version=`get_package_version $package`

# Path begins from vendor/...
path=`echo $fullpath | sed -e "s/^.*\(vendor.*\)$/\1/"`

# Remove domain of repository in package
name=`echo $package | sed -e "s/^[^\/]*\/\(.*\)$/\1/"`

# Generate JSON
if [ $count -gt 0 ]; then
echo -n ','
fi
count=`expr $count + 1`

print_dependency_json $name $license $path $version
done

# workaround, go-licenses doesn't return following packages:
# libp2p/go-netroute
# libp2p/go-sockaddr
# pkg/errors
if [ ${#licenses[@]} -gt 0 ]; then
# BSD-3
echo ${licenses[@]} | xargs -n 1 | grep -E "^BSD-3-Clause$" > /dev/null
if [ $? -eq 0 ]; then
bsd3_dep_packages=("libp2p/go-netroute" "libp2p/go-sockaddr")
bsd3_dep_paths=("vendor/github.com/libp2p/go-netroute/LICENSE" "vendor/github.com/libp2p/go-netroute/LICENSE")

i=0
for package in "${bsd3_dep_packages[@]}"
do
path=${bsd3_dep_paths[i]}
version=`get_package_version $package`
license="BSD-3-Clause"

echo -n ','
print_dependency_json $package $license $path $version

i=`expr $i + 1`
done
fi

# BSD-2
echo ${licenses[@]} | xargs -n 1 | grep -E "^BSD-2-Clause$" > /dev/null
if [ $? -eq 0 ]; then
bsd2_dep_packages=("pkg/errors")
bsd2_dep_paths=("vendor/github.com/pkg/errors/LICENSE")

i=0
for package in "${bsd2_dep_packages[@]}"
do
path=${bsd2_dep_paths[i]}
version=`get_package_version $package`
license="BSD-2-Clause"

echo -n ','
print_dependency_json $package $license $path $version

i=`expr $i + 1`
done
fi
fi

printf '\n]'
4 changes: 2 additions & 2 deletions jsonrpc/web3_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (

"github.com/0xPolygon/polygon-edge/helper/hex"
"github.com/0xPolygon/polygon-edge/helper/keccak"
"github.com/0xPolygon/polygon-edge/version"
"github.com/0xPolygon/polygon-edge/versioning"
)

// Web3 is the web3 jsonrpc endpoint
type Web3 struct{}

// ClientVersion returns the version of the web3 client (web3_clientVersion)
func (w *Web3) ClientVersion() (interface{}, error) {
return fmt.Sprintf("polygon-edge [%s]", version.GetVersionJsonrpc()), nil
return fmt.Sprintf("polygon-edge [%s]", versioning.Version), nil
}

// Sha3 returns Keccak-256 (not the standardized SHA3-256) of the given data
Expand Down
Loading

0 comments on commit cee68ff

Please sign in to comment.