Skip to content

Commit

Permalink
Switch dep manager from govendor to go modules (#20)
Browse files Browse the repository at this point in the history
- CircleCi config is update to enable go mod support for go in ci env.
- Go modules enables project to built outside go workspace. Hence,
  build/setEnv.sh that was used for creating fake workspace is removed.
- Functionality to check if build scripts are invoked from project root
  directory is moved from build/setEnv.sh to build/ci.go
- Build scripts (build/ci.go), Readme and .gitignore are updated.

- Also, as a a result of migrating from govendor to go modules,
    - Vendor directory is no more required and hence removed.
    - go.mod and go.sum files are added to track dependencies.

Resolves #9

Signed-off-by: Manoranjith <ponraj.manoranjitha@in.bosch.com>
  • Loading branch information
manoranjith committed Jan 21, 2020
1 parent 741765d commit d3a917d
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 1,028 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ jobs:
checkout_deps:
docker:
- image: circleci/golang:1.12.7-stretch
environment:
GO111MODULE: "on"
working_directory: /go/src/github.com/direct-state-transfer/dst-go
steps:
- checkout
Expand All @@ -20,6 +22,7 @@ jobs:
docker:
- image: circleci/golang:1.12.7-stretch
environment:
GO111MODULE: "on"
TEST_RESULTS: /tmp/test-results
working_directory: /go/src/github.com/direct-state-transfer/dst-go
steps:
Expand All @@ -41,6 +44,8 @@ jobs:
walkthrough_simulated:
docker:
- image: circleci/golang:1.12.7-stretch
environment:
GO111MODULE: "on"
working_directory: /go/src/github.com/direct-state-transfer/dst-go
steps:
- restore_cache:
Expand Down
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
#Ignore these files
vendor/*
build/workspace_

#But not these
!vendor/vendor.json


3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ run:
# include test files or not, default is true
tests: true

skip-dirs:
- workspace_

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ help: ## display this help text
# $(O) is meant as a shortcut for $(BUILDOPTS).
$(__TARGETS): Makefile
go build -o build/build ./build
build/setEnv.sh build/build $@ $(BUILDOPTS) $(0)
build/build $@ $(BUILDOPTS) $(0)
rm build/build
@echo "Done building."
41 changes: 15 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,30 @@ This below document explains how to start using direct state transfer software.

### Prerequisites

1. Install Go (v1.10 or later)
1. Install Go (v1.11 or later)
2. Install Geth (v1.8.20-stable) (Optional; required only for running walkthrough)

### Build from source

The following commands can be used to build and install dst-go from its source.
Once the GOPATH and GOBIN are properly set in the local environment, following commands can be executed.

```bash
cd $GOPATH
cd src

#create directory
mkdir -p github.com/direct-state-transfer
cd github.com/direct-state-transfer

#clone dst-go repo
git clone https://github.com/direct-state-transfer/dst-go.git
cd dst-go

#install govendor tool to sync vendored dependencies
go get -u -v github.com/kardianos/govendor

#sync dependencies
#should be run inside the cloned root of the dst-go repo (~/PATH/dst-go)
govendor sync -v
#should be run inside the cloned root of the dst-go repo
go mod tidy

#Now navigate to dst-go module inside the repo
cd dst-go
#install dst-go
go install -v
```

dst-go is installed in the local machine and the binary will be available at the set GOBIN path.
dst-go is installed in the local machine and the binary will be available at `$GOBIN`.
If `$GOBIN` environement variable is not set, then it can be found at `~/go/bin`

### Build using Make

Expand All @@ -80,7 +70,6 @@ git clone https://github.com/direct-state-transfer/dst-go.git

The available make file can be used to build dst-go from source.
The following commands can be executed from the root repository of the project.
(~/LOCAL_WORKSPACE/dst-go/)

```bash
#To get a list of available build targets
Expand All @@ -89,7 +78,7 @@ make help
make install
```

On successful run, the binary will be available at ~/LOCAL_WORKSPACE/dst-go/build/workspace_/bin
On successful run, the binary will be available at `$GOPATH/bin`. If `$GOPATH` environement variable is not set, then it can be found at `~/go/bin`

### Build and run walkthrough

Expand All @@ -106,7 +95,7 @@ This can be run with a geth node (real backend) or with the simulated backend (f

1. The geth node should be configured to use port number 8546 for
websocket connection or the geth nodes websocket port number should
be updated in `~/LOCAL_WORKSPACE/dst-go/testdata/test_addresses.json`
be updated in `<project-root-directory>/testdata/test_addresses.json`
in the place of ethereum node url.

The following shows the default test_addresses.json with some sample keys already present in the keystore which is already present in the project. Similarly the ports and ethereum addresses of alice and bob can be updated.
Expand All @@ -130,27 +119,27 @@ This can be run with a geth node (real backend) or with the simulated backend (f
}
```

2. The key files of the keys mentioned in the test_addresses.json for alice and bob should be present in testdata/test-keystore and the geth's keystore.
3. Sample configuration is available at testdata with default keys and key files. To use these, simply add the key files from testdata/test-keystore directory to geth's keystore.
2. The key files of the keys mentioned in the `test_addresses.json` for alice and bob should be present in `testdata/test-keystore` directory and the loaded in geth's keystore.
3. Sample configuration is available at `testdata` with default keys and key files. To use these, simply add the key files from `testdata/test-keystore` directory to geth's keystore.
4. Both Alice's and Bob's account should have minimum of 10 Ethers each to run this walkthrough. (It is currently not yet tested on mainnet.)
5. If a local geth node is used to run the walkthrough, the mining should be activated to execute transactions.
6. Make sure that the vendored dependencies using govendor tool are synchronized.
6. Make sure that the dependencies are synchronized using `go mod tidy` command.

#### Build and run from source

Use the below commands to build and run the walkthrough from source.

```bash
cd $GOPATH/src/github.com/direct-state-transfer/dst-go/walkthrough
cd dst-go/walkthrough
#build walkthrough
go build -v

#Run walkthrough
#Initialize bob's node (should be started first) and let it run..
#Initialize bob's node (should be started first) and let it run.
./walkthrough --real_backend_bob

#Open a new terminal and go to walkthrough dir
cd $GOPATH/src/github.com/direct-state-transfer/dst-go/walkthrough
cd dst-go/walkthrough

#Initialize alice's node and commence walkthrough
./walkthrough --real_backend_alice
Expand Down Expand Up @@ -183,8 +172,8 @@ In test mode short, all the unit tests will be performed using the simulated bac
```bash
# To run all the tests
make test
# All options supported by go test command can be passed via testOpts
# option as shown below.
# All options supported by go test command can be passed via BUILDOPTS
# as shown below.
# Some flags will be enabled by default (-cover and cache=1). Output is
# non verbose by default.
make test BUILDOPTS="-v -short"
Expand Down
110 changes: 46 additions & 64 deletions build/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,56 @@ package main

import (
"fmt"
"go/build"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
)

var tempGoPath, tempProjectRoot string
var goPath, goBin, projectRootDir string

var (
requiredGoVersionMajor = uint8(1) //Go1.x Required
minimumGoVersionMinor = uint8(10) //Go1.10 Minimum
minimumGoVersionMinor = uint8(11) //Go1.11 Minimum
)

func main() {

var err error
var workingDir string

_, programFilePath, _, ok := runtime.Caller(0)
if !ok {
fmt.Printf("Error in fetching caller information : %s\n", err)
os.Exit(1)
}

projectRootDir = path.Dir(path.Dir(programFilePath))

workingDir, err = os.Getwd()
if err != nil {
fmt.Printf("Error in fetching current working directory : %s\n", err)
os.Exit(1)
}

if projectRootDir != workingDir {
fmt.Printf("Error - Program can only be invoked from project root directory\n")
os.Exit(1)
}

if len(os.Args) < 1 {
fmt.Println("No commands to execute")
}

tempGoPath = os.Getenv("GOPATH")
tempProjectRoot = os.Getenv("GOPATH") + os.Getenv("DST_GO_PATH")
//the variable from build package gives the proper gopath
//from environment variables if set, else default value (~/go)
goPath = build.Default.GOPATH
goBin = filepath.Join(goPath, "bin")

err := checkGoEnv(requiredGoVersionMajor, minimumGoVersionMinor)
err = checkGoEnv(requiredGoVersionMajor, minimumGoVersionMinor)
if err != nil {
fmt.Printf("Error in go environment : %s\n", err)
os.Exit(1)
Expand All @@ -59,7 +85,6 @@ func main() {
os.Exit(1)
}

// documentation of commands in doc.go
switch os.Args[1] {
case "fetchDependencies":
fetchVendoredPackages()
Expand Down Expand Up @@ -88,8 +113,8 @@ func main() {
runWalkthrough(os.Args[2:])

case "lint":
fetchVendoredPackages()
performLint()
// fetchVendoredPackages()
performLint(os.Args[2:])
}
}

Expand All @@ -99,22 +124,13 @@ func installDependencies() (err error) {
fmt.Println(" Installing Dependencies ")
fmt.Println("----------------------------------------------------------")

fmt.Printf("\ngovendor...\t")
cmd := exec.Command("go", "get", "-u", "-v", "github.com/kardianos/govendor")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed. Below errors are reported\n\n%s", output)
}
fmt.Printf("Successful")

fmt.Printf("\ngolangci-lint v1.21.0...\t")

tempGoBin := filepath.Join(tempGoPath, "bin")
outputByteArr, err := pipe(
// #nosec G204. To suppress gosec linter warning. The below command is safe to use.
exec.Command("curl", "-sfL", "https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh"),
// #nosec G204. To suppress gosec linter warning. The below command is safe to use.
exec.Command("sh", "-s", "--", "-b", tempGoBin, "v1.21.0"),
exec.Command("sh", "-s", "--", "-b", goBin, "v1.21.0"),
)
if err != nil {
return fmt.Errorf("failed. Below errors are reported\n\n%s", outputByteArr.String())
Expand All @@ -140,9 +156,6 @@ func checkGoEnv(requiredGoVersionMajor, minimumGoVersionMinor uint8) (err error)
if goVersionMinor < minimumGoVersionMinor {
return fmt.Errorf("detected go version - %s. Minor version atleast %d required", goVersion, minimumGoVersionMinor)
}
if tempGoPath == "" {
return fmt.Errorf("gopath not found")
}

return nil
}
Expand All @@ -151,50 +164,16 @@ func fetchVendoredPackages() {
fmt.Printf("\n----------------------------------------------------------\n")
fmt.Println(" Fetching vendored libraries ")
fmt.Printf("---------------------------------------------------------- \n\n")
var filesList []string
var err error

//Delete all the existing vendored files
root := filepath.Join(tempProjectRoot, "vendor")
var vendorFile os.FileInfo
if vendorFile, err = os.Stat(root); os.IsNotExist(err) || !vendorFile.IsDir() {
fmt.Println("Vendor directory missing or it is a file")
os.Exit(1)
}
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.Name() != "vendor.json" && info.Name() != "vendor" {
filesList = append(filesList, path)
}
return nil
})

cmd := exec.Command("rm", "-rf")
cmd.Args = append(cmd.Args, filesList...)
err = cmd.Run()
if err != nil {
fmt.Println("Delete existing vendored packages failed - ", err)
os.Exit(1)
}

//Fetch vendored libraries using tool
//Add missing and remove unnecessary dependencies.
// #nosec G204. To suppress gosec linter warning. The below command is safe to use.
cmd = exec.Command(filepath.Join(tempGoPath, "/bin/govendor"), "sync", "-v")
cmd := exec.Command("go", "mod", "tidy", "-v")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println("Govendor sync failed - ", err)
os.Exit(1)
}

//Print the status of vendored libraries
// #nosec G204. To suppress gosec linter warning. The below command is safe to use.
cmd = exec.Command(filepath.Join(tempGoPath, "/bin/govendor"), "list")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
fmt.Println("Govendor list failed - ", err)
fmt.Println("go mod tidy failed with error - ", err)
os.Exit(1)
}
}
Expand Down Expand Up @@ -227,7 +206,7 @@ func install() {
fmt.Println("Build failed - ", err)
os.Exit(1)
}
fmt.Println("Installed successfully at", (filepath.Join(tempGoPath, "/bin/dst-go")))
fmt.Println("Installed successfully at", (filepath.Join(goPath, "/bin/dst-go")))
}

//run unit tests for all packages
Expand Down Expand Up @@ -269,7 +248,8 @@ func buildWalkthrough() {
fmt.Printf("\n----------------------------------------------------------\n")
fmt.Println(" Building Walkthrough ")
fmt.Printf("---------------------------------------------------------- \n\n")
cmd := exec.Command("go", "build", "-o", "./walkthrough/walkthrough", "-v", "./walkthrough")
cmd := exec.Command("go", "build", "-v")
cmd.Dir = filepath.Join(projectRootDir, "walkthrough")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
Expand All @@ -290,7 +270,7 @@ func runWalkthrough(flags []string) {

cmd := exec.Command("./walkthrough")
cmd.Args = append(cmd.Args, flags...)
cmd.Dir = tempProjectRoot + "walkthrough"
cmd.Dir = filepath.Join(projectRootDir, "walkthrough")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand All @@ -302,7 +282,7 @@ func runWalkthrough(flags []string) {
}

//perform Lint
func performLint() {
func performLint(flags []string) {

fmt.Println("----------------------------------------------------------")
fmt.Println(" Linting the code ")
Expand All @@ -311,13 +291,15 @@ func performLint() {

// Configuration is fetched from the file .golangci.yml in project root directory.
// #nosec G204. To suppress gosec linter warning. The below command is safe to use.
cmd := exec.Command(filepath.Join(tempGoPath, "/bin/golangci-lint"), "run", "./...")
cmd.Dir = tempProjectRoot
cmd := exec.Command(filepath.Join(goBin, "golangci-lint"), "run", "./...")
cmd.Args = append(cmd.Args, flags...)
cmd.Dir = projectRootDir
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Failed. Below warnings and errors are reported\n\n%s\n", output)
os.Exit(1)
} else {
fmt.Printf("\n%s\n", output)
fmt.Printf("Successful. No errors detected\n")
}
}
Loading

0 comments on commit d3a917d

Please sign in to comment.