浏览代码

Run go fmt ./...

Vadim Markovtsev 7 年之前
父节点
当前提交
2a2394b065
共有 6 个文件被更改,包括 172 次插入174 次删除
  1. 1 1
      cmd/hercules-generate-plugin/embed.go
  2. 99 99
      cmd/hercules-generate-plugin/main.go
  3. 18 19
      cmd/hercules/main.go
  4. 38 38
      pb/utils.go
  5. 14 14
      toposort/toposort.go
  6. 2 3
      toposort/toposort_test.go

+ 1 - 1
cmd/hercules-generate-plugin/embed.go

@@ -9,7 +9,7 @@ import (
 )
 
 func main() {
-  contents, err := ioutil.ReadFile("plugin.template")
+	contents, err := ioutil.ReadFile("plugin.template")
 	if err != nil {
 		panic(err)
 	}

+ 99 - 99
cmd/hercules-generate-plugin/main.go

@@ -1,84 +1,84 @@
 package main
 
 import (
-  "bytes"
-  "flag"
-  "fmt"
-  "io/ioutil"
-  "os"
-  "os/exec"
-  "path"
-  "runtime"
-  "strings"
-  "text/template"
+	"bytes"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"os/exec"
+	"path"
+	"runtime"
+	"strings"
+	"text/template"
 
-  "gopkg.in/src-d/hercules.v3"
-  "github.com/fatih/camelcase"
+	"github.com/fatih/camelcase"
+	"gopkg.in/src-d/hercules.v3"
 )
 
 //go:generate go run embed.go
 
-var SHLIB_EXT = map[string]string {
-  "window": "dll",
-  "linux": "so",
-  "darwin": "dylib",
-  "freebsd": "dylib",
+var SHLIB_EXT = map[string]string{
+	"window":  "dll",
+	"linux":   "so",
+	"darwin":  "dylib",
+	"freebsd": "dylib",
 }
 
 func main() {
-  var outputDir, name, varname, _flag, pkg string
-  var printVersion, disableMakefile bool
-  flag.StringVar(&name, "n", "", "Name of the plugin, CamelCase. Required.")
-  flag.StringVar(&outputDir, "o", ".", "Output directory for the generated plugin files.")
-  flag.StringVar(&varname, "varname", "", "Name of the plugin instance variable, If not " +
-      "specified, inferred from -n.")
-  flag.StringVar(&_flag, "flag", "", "Name of the plugin activation cmdline flag, If not " +
-      "specified, inferred from -varname.")
-  flag.BoolVar(&printVersion, "version", false, "Print version information and exit.")
-  flag.BoolVar(&disableMakefile, "no-makefile", false, "Do not generate the Makefile.")
-  flag.StringVar(&pkg, "package", "main", "Name of the package.")
-  flag.Parse()
-  if printVersion {
+	var outputDir, name, varname, _flag, pkg string
+	var printVersion, disableMakefile bool
+	flag.StringVar(&name, "n", "", "Name of the plugin, CamelCase. Required.")
+	flag.StringVar(&outputDir, "o", ".", "Output directory for the generated plugin files.")
+	flag.StringVar(&varname, "varname", "", "Name of the plugin instance variable, If not "+
+		"specified, inferred from -n.")
+	flag.StringVar(&_flag, "flag", "", "Name of the plugin activation cmdline flag, If not "+
+		"specified, inferred from -varname.")
+	flag.BoolVar(&printVersion, "version", false, "Print version information and exit.")
+	flag.BoolVar(&disableMakefile, "no-makefile", false, "Do not generate the Makefile.")
+	flag.StringVar(&pkg, "package", "main", "Name of the package.")
+	flag.Parse()
+	if printVersion {
 		fmt.Printf("Version: 3\nGit:     %s\n", hercules.GIT_HASH)
 		return
 	}
-  if name == "" {
-    fmt.Fprintln(os.Stderr, "-n must be specified")
-    flag.PrintDefaults()
-    os.Exit(1)
-  }
-  splitted := camelcase.Split(name)
-  err := os.MkdirAll(outputDir, os.ModePerm)
-  if err != nil {
-    panic(err)
-  }
-  outputPath := path.Join(outputDir, strings.ToLower(strings.Join(splitted, "_")) + ".go")
-  gen := template.Must(template.New("plugin").Parse(PLUGIN_TEMPLATE_SOURCE))
-  outFile, err := os.Create(outputPath)
-  if err != nil {
-    panic(err)
-  }
-  defer outFile.Close()
-  if varname == "" {
-    varname = strings.ToLower(splitted[0])
-  }
-  if _flag == "" {
-    _flag = strings.ToLower(strings.Join(splitted, "-"))
-  }
-  outputBase := path.Base(outputPath)
-  shlib := outputBase[:len(outputBase)-2] + SHLIB_EXT[runtime.GOOS]
-  protoBuf := outputPath[:len(outputPath)-3] + ".proto"
-  pbGo := outputPath[:len(outputPath)-3] + ".pb.go"
-  dict := map[string]string{
-    "name": name, "varname": varname, "flag": _flag, "package": pkg,
-    "output": outputPath, "shlib": shlib, "proto": protoBuf, "protogo": pbGo,
-    "outdir": outputDir}
-  err = gen.Execute(outFile, dict)
-  if err != nil {
-    panic(err)
-  }
-  // write pb file
-  ioutil.WriteFile(protoBuf, []byte(fmt.Sprintf(`syntax = "proto3";
+	if name == "" {
+		fmt.Fprintln(os.Stderr, "-n must be specified")
+		flag.PrintDefaults()
+		os.Exit(1)
+	}
+	splitted := camelcase.Split(name)
+	err := os.MkdirAll(outputDir, os.ModePerm)
+	if err != nil {
+		panic(err)
+	}
+	outputPath := path.Join(outputDir, strings.ToLower(strings.Join(splitted, "_"))+".go")
+	gen := template.Must(template.New("plugin").Parse(PLUGIN_TEMPLATE_SOURCE))
+	outFile, err := os.Create(outputPath)
+	if err != nil {
+		panic(err)
+	}
+	defer outFile.Close()
+	if varname == "" {
+		varname = strings.ToLower(splitted[0])
+	}
+	if _flag == "" {
+		_flag = strings.ToLower(strings.Join(splitted, "-"))
+	}
+	outputBase := path.Base(outputPath)
+	shlib := outputBase[:len(outputBase)-2] + SHLIB_EXT[runtime.GOOS]
+	protoBuf := outputPath[:len(outputPath)-3] + ".proto"
+	pbGo := outputPath[:len(outputPath)-3] + ".pb.go"
+	dict := map[string]string{
+		"name": name, "varname": varname, "flag": _flag, "package": pkg,
+		"output": outputPath, "shlib": shlib, "proto": protoBuf, "protogo": pbGo,
+		"outdir": outputDir}
+	err = gen.Execute(outFile, dict)
+	if err != nil {
+		panic(err)
+	}
+	// write pb file
+	ioutil.WriteFile(protoBuf, []byte(fmt.Sprintf(`syntax = "proto3";
 option go_package = "%s";
 
 message %sResultMessage {
@@ -87,28 +87,28 @@ message %sResultMessage {
   // example: pb/pb.proto https://github.com/src-d/hercules/blob/master/pb/pb.proto
 }
 `, pkg, name)), 0666)
-  // generate the pb Go file
-  protoc, err := exec.LookPath("protoc")
-  args := [...]string{
-    protoc,
-    "--gogo_out=" + outputDir,
-    "--proto_path=" + outputDir,
-    protoBuf,
-  }
-  env := os.Environ()
-  env = append(env, fmt.Sprintf(
-    "PATH=%s:%s", os.Getenv("PATH"), path.Join(os.Getenv("GOPATH"), "bin")))
-  if err != nil {
-    panic("protoc was not found at " + env[len(env)-1])
-  }
-  cmd := exec.Cmd{Path: protoc, Args: args[:], Env: env, Stdout: os.Stdout, Stderr: os.Stderr}
-  err = cmd.Run()
-  if err != nil {
-    panic(err)
-  }
-  if !disableMakefile {
-    makefile := path.Join(outputDir, "Makefile")
-    gen = template.Must(template.New("plugin").Parse(`all: {{.shlib}}
+	// generate the pb Go file
+	protoc, err := exec.LookPath("protoc")
+	args := [...]string{
+		protoc,
+		"--gogo_out=" + outputDir,
+		"--proto_path=" + outputDir,
+		protoBuf,
+	}
+	env := os.Environ()
+	env = append(env, fmt.Sprintf(
+		"PATH=%s:%s", os.Getenv("PATH"), path.Join(os.Getenv("GOPATH"), "bin")))
+	if err != nil {
+		panic("protoc was not found at " + env[len(env)-1])
+	}
+	cmd := exec.Cmd{Path: protoc, Args: args[:], Env: env, Stdout: os.Stdout, Stderr: os.Stderr}
+	err = cmd.Run()
+	if err != nil {
+		panic(err)
+	}
+	if !disableMakefile {
+		makefile := path.Join(outputDir, "Makefile")
+		gen = template.Must(template.New("plugin").Parse(`all: {{.shlib}}
 
 {{.shlib}}: {{.output}} {{.protogo}}
 ` + "\t" + `go build -buildmode=plugin {{.output}} {{.protogo}}
@@ -116,14 +116,14 @@ message %sResultMessage {
 {{.protogo}}: {{.proto}}
 ` + "\t" + `PATH=$$PATH:$$GOPATH/bin protoc --gogo_out=. --proto_path=. {{.proto}}
 `))
-    buffer := new(bytes.Buffer)
-    mkrelative := func(name string) {
-      dict[name] = path.Base(dict[name])
-    }
-    mkrelative("output")
-    mkrelative("protogo")
-    mkrelative("proto")
-    gen.Execute(buffer, dict)
-    ioutil.WriteFile(makefile, buffer.Bytes(), 0666)
-  }
+		buffer := new(bytes.Buffer)
+		mkrelative := func(name string) {
+			dict[name] = path.Base(dict[name])
+		}
+		mkrelative("output")
+		mkrelative("protogo")
+		mkrelative("proto")
+		gen.Execute(buffer, dict)
+		ioutil.WriteFile(makefile, buffer.Bytes(), 0666)
+	}
 }

+ 18 - 19
cmd/hercules/main.go

@@ -41,6 +41,10 @@ import (
 	"strconv"
 	"strings"
 
+	"github.com/gogo/protobuf/proto"
+	"github.com/vbauerster/mpb"
+	"github.com/vbauerster/mpb/decor"
+	"golang.org/x/crypto/ssh/terminal"
 	"gopkg.in/src-d/go-billy.v3/osfs"
 	"gopkg.in/src-d/go-git.v4"
 	"gopkg.in/src-d/go-git.v4/plumbing/object"
@@ -49,10 +53,6 @@ import (
 	"gopkg.in/src-d/go-git.v4/storage/memory"
 	"gopkg.in/src-d/hercules.v3"
 	"gopkg.in/src-d/hercules.v3/pb"
-	"github.com/vbauerster/mpb"
-	"github.com/vbauerster/mpb/decor"
-	"github.com/gogo/protobuf/proto"
-	"golang.org/x/crypto/ssh/terminal"
 )
 
 type OneLineWriter struct {
@@ -60,9 +60,9 @@ type OneLineWriter struct {
 }
 
 func (writer OneLineWriter) Write(p []byte) (n int, err error) {
-	if p[len(p) - 1] == '\n' {
-		p = p[:len(p) - 1]
-		if len(p) > 5 && bytes.Compare(p[len(p) - 5:], []byte("done.")) == 0 {
+	if p[len(p)-1] == '\n' {
+		p = p[:len(p)-1]
+		if len(p) > 5 && bytes.Compare(p[len(p)-5:], []byte("done.")) == 0 {
 			p = []byte("cloning...")
 		}
 		p = append(p, '\r')
@@ -111,7 +111,6 @@ func loadRepository(uri string, disableStatus bool) *git.Repository {
 	return repository
 }
 
-
 type arrayPluginFlags map[string]bool
 
 func (apf *arrayPluginFlags) String() string {
@@ -133,7 +132,7 @@ func loadPlugins() {
 	fs.SetOutput(ioutil.Discard)
 	pluginFlagName := "plugin"
 	pluginDesc := "Load the specified plugin by the full or relative path. " +
-			"Can be specified multiple times."
+		"Can be specified multiple times."
 	fs.Var(&pluginFlags, pluginFlagName, pluginDesc)
 	flag.Var(&pluginFlags, pluginFlagName, pluginDesc)
 	fs.Parse(os.Args[1:])
@@ -281,17 +280,17 @@ func protobufResults(
 	uri string, begin, end int64, commitsCount int, deployed []hercules.PipelineItem,
 	results map[hercules.PipelineItem]interface{}) {
 
-  header := pb.Metadata{
-	  Version: 1,
-	  Hash: hercules.GIT_HASH,
-	  Repository: uri,
-    BeginUnixTime: begin,
-	  EndUnixTime: end,
-	  Commits: int32(commitsCount),
-  }
+	header := pb.Metadata{
+		Version:       1,
+		Hash:          hercules.GIT_HASH,
+		Repository:    uri,
+		BeginUnixTime: begin,
+		EndUnixTime:   end,
+		Commits:       int32(commitsCount),
+	}
 
 	message := pb.AnalysisResults{
-		Header: &header,
+		Header:   &header,
 		Contents: map[string][]byte{},
 	}
 
@@ -309,5 +308,5 @@ func protobufResults(
 	if err != nil {
 		panic(err)
 	}
-  os.Stdout.Write(serialized)
+	os.Stdout.Write(serialized)
 }

+ 38 - 38
pb/utils.go

@@ -3,44 +3,44 @@ package pb
 import "sort"
 
 func ToBurndownSparseMatrix(matrix [][]int64, name string) *BurndownSparseMatrix {
-  r := BurndownSparseMatrix{
-	  Name: name,
-	  NumberOfRows: int32(len(matrix)),
-	  NumberOfColumns: int32(len(matrix[len(matrix)-1])),
-	  Rows: make([]*BurndownSparseMatrixRow, len(matrix)),
-  }
-  for i, status := range matrix {
-	  nnz := make([]uint32, 0, len(status))
-	  changed := false
-	  for j := range status {
-		  v := status[len(status) - 1 - j]
-		  if v < 0 {
-			  v = 0
-		  }
-		  if !changed {
-			  changed = v != 0
-		  }
-		  if changed {
-			  nnz = append(nnz, uint32(v))
-		  }
-	  }
-	  r.Rows[i] = &BurndownSparseMatrixRow{
-		  Columns: make([]uint32, len(nnz)),
-	  }
-	  for j := range nnz {
-		  r.Rows[i].Columns[j] = nnz[len(nnz) - 1 - j]
-	  }
+	r := BurndownSparseMatrix{
+		Name:            name,
+		NumberOfRows:    int32(len(matrix)),
+		NumberOfColumns: int32(len(matrix[len(matrix)-1])),
+		Rows:            make([]*BurndownSparseMatrixRow, len(matrix)),
+	}
+	for i, status := range matrix {
+		nnz := make([]uint32, 0, len(status))
+		changed := false
+		for j := range status {
+			v := status[len(status)-1-j]
+			if v < 0 {
+				v = 0
+			}
+			if !changed {
+				changed = v != 0
+			}
+			if changed {
+				nnz = append(nnz, uint32(v))
+			}
+		}
+		r.Rows[i] = &BurndownSparseMatrixRow{
+			Columns: make([]uint32, len(nnz)),
+		}
+		for j := range nnz {
+			r.Rows[i].Columns[j] = nnz[len(nnz)-1-j]
+		}
 	}
 	return &r
 }
 
 func DenseToCompressedSparseRowMatrix(matrix [][]int64) *CompressedSparseRowMatrix {
 	r := CompressedSparseRowMatrix{
-		NumberOfRows: int32(len(matrix)),
+		NumberOfRows:    int32(len(matrix)),
 		NumberOfColumns: int32(len(matrix[0])),
-		Data: make([]int64, 0),
-		Indices: make([]int32, 0),
-		Indptr: make([]int64, 1),
+		Data:            make([]int64, 0),
+		Indices:         make([]int32, 0),
+		Indptr:          make([]int64, 1),
 	}
 	r.Indptr[0] = 0
 	for _, row := range matrix {
@@ -52,18 +52,18 @@ func DenseToCompressedSparseRowMatrix(matrix [][]int64) *CompressedSparseRowMatr
 				nnz += 1
 			}
 		}
-		r.Indptr = append(r.Indptr, r.Indptr[len(r.Indptr) - 1] + int64(nnz))
+		r.Indptr = append(r.Indptr, r.Indptr[len(r.Indptr)-1]+int64(nnz))
 	}
 	return &r
 }
 
 func MapToCompressedSparseRowMatrix(matrix []map[int]int64) *CompressedSparseRowMatrix {
 	r := CompressedSparseRowMatrix{
-		NumberOfRows: int32(len(matrix)),
+		NumberOfRows:    int32(len(matrix)),
 		NumberOfColumns: int32(len(matrix)),
-		Data: make([]int64, 0),
-		Indices: make([]int32, 0),
-		Indptr: make([]int64, 1),
+		Data:            make([]int64, 0),
+		Indices:         make([]int32, 0),
+		Indptr:          make([]int64, 1),
 	}
 	r.Indptr[0] = 0
 	for _, row := range matrix {
@@ -79,7 +79,7 @@ func MapToCompressedSparseRowMatrix(matrix []map[int]int64) *CompressedSparseRow
 			r.Data = append(r.Data, val)
 			r.Indices = append(r.Indices, int32(col))
 		}
-		r.Indptr = append(r.Indptr, r.Indptr[len(r.Indptr) - 1] + int64(len(row)))
+		r.Indptr = append(r.Indptr, r.Indptr[len(r.Indptr)-1]+int64(len(row)))
 	}
 	return &r
-}
+}

+ 14 - 14
toposort/toposort.go

@@ -13,7 +13,7 @@ type Graph struct {
 	// Outgoing connections for every node.
 	outputs map[string]map[string]int
 	// How many parents each node has.
-	inputs  map[string]int
+	inputs map[string]int
 }
 
 // NewGraph initializes a new Graph.
@@ -178,7 +178,7 @@ func (g *Graph) BreadthSort() []string {
 // FindCycle returns the cycle in the graph which contains "seed" node.
 func (g *Graph) FindCycle(seed string) []string {
 	type edge struct {
-		node string
+		node   string
 		parent string
 	}
 	S := make([]edge, 0, len(g.outputs))
@@ -245,17 +245,17 @@ func (g *Graph) Serialize(sorted []string) string {
 		nodesFrom = append(nodesFrom, nodeFrom)
 	}
 	sort.Strings(nodesFrom)
-  for _, nodeFrom := range nodesFrom {
-	  links := []string{}
-	  for nodeTo := range g.outputs[nodeFrom] {
-		  links = append(links, nodeTo)
-	  }
-	  sort.Strings(links)
-	  for _, nodeTo := range links {
-		  buffer.WriteString(fmt.Sprintf("  \"%d %s\" -> \"%d %s\"\n",
-			  node2index[nodeFrom], nodeFrom, node2index[nodeTo], nodeTo))
-	  }
-  }
+	for _, nodeFrom := range nodesFrom {
+		links := []string{}
+		for nodeTo := range g.outputs[nodeFrom] {
+			links = append(links, nodeTo)
+		}
+		sort.Strings(links)
+		for _, nodeTo := range links {
+			buffer.WriteString(fmt.Sprintf("  \"%d %s\" -> \"%d %s\"\n",
+				node2index[nodeFrom], nodeFrom, node2index[nodeTo], nodeTo))
+		}
+	}
 	buffer.WriteString("}")
 	return buffer.String()
 }
@@ -275,7 +275,7 @@ func (g *Graph) DebugDump() string {
 	for key, val1 := range g.outputs {
 		val2 := make([]string, len(val1))
 		for name, idx := range val1 {
-			val2[idx - 1] = name
+			val2[idx-1] = name
 		}
 		keys = append(keys, key)
 		vals[key] = val2

+ 2 - 3
toposort/toposort_test.go

@@ -1,8 +1,8 @@
 package toposort
 
 import (
-	"testing"
 	"github.com/stretchr/testify/assert"
+	"testing"
 )
 
 func index(s []string, v string) int {
@@ -205,9 +205,8 @@ func TestToposortFindChildren(t *testing.T) {
 	assert.Equal(t, [2]bool{true, true}, checks)
 }
 
-
 func TestToposortSerialize(t *testing.T) {
-  graph := NewGraph()
+	graph := NewGraph()
 	graph.AddNodes("1", "2", "3", "4", "5")
 
 	graph.AddEdge("1", "2")