From c8c82b323616fab39319e6e789bc76beb4edbb22 Mon Sep 17 00:00:00 2001
From: Tomasz Maczukin <tomasz@maczukin.pl>
Date: Tue, 12 Jul 2016 22:51:03 +0200
Subject: [PATCH] Add "info" command

---
 commands/builds_helper.go |  4 ++++
 commands/info.go          | 50 +++++++++++++++++++++++++++++++++++++++
 commands/multi.go         |  2 +-
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 commands/info.go

diff --git a/commands/builds_helper.go b/commands/builds_helper.go
index 876578726..e5b9cdf0c 100644
--- a/commands/builds_helper.go
+++ b/commands/builds_helper.go
@@ -91,3 +91,7 @@ func (b *buildsHelper) removeBuild(deleteBuild *common.Build) bool {
 	}
 	return false
 }
+
+func (b *buildsHelper) buildsCount() int {
+	return len(b.builds)
+}
diff --git a/commands/info.go b/commands/info.go
new file mode 100644
index 000000000..58f45c0b5
--- /dev/null
+++ b/commands/info.go
@@ -0,0 +1,50 @@
+package commands
+
+import (
+	"encoding/json"
+	"fmt"
+
+	log "github.com/Sirupsen/logrus"
+	"github.com/codegangsta/cli"
+	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/common"
+	"gitlab.com/gitlab-org/gitlab-ci-multi-runner/helpers"
+)
+
+type InfoData struct {
+	BuildsCount int `json:"builds_count"`
+
+	RunnersBuildsCounts map[string]int        `json:"runners_builds_counts"`
+	VersionInfo         common.AppVersionInfo `json:"version_info"`
+}
+
+type InfoCommand struct {
+	buildsHelper buildsHelper
+	data         InfoData
+}
+
+func (c *InfoCommand) prepare() {
+	c.data.BuildsCount = c.buildsHelper.buildsCount()
+	c.data.VersionInfo = common.AppVersion
+
+	runnersBuildsCounts := map[string]int{}
+	for token, count := range c.buildsHelper.counts {
+		runnersBuildsCounts[helpers.ShortenToken(token)] = count
+	}
+	c.data.RunnersBuildsCounts = runnersBuildsCounts
+}
+
+func (c *InfoCommand) Execute(context *cli.Context) {
+	c.prepare()
+
+	bytes, err := json.Marshal(c.data)
+	if err != nil {
+		log.WithError(err).Errorln("Error with InfoData marshalling to JSON")
+		return
+	}
+
+	fmt.Print(string(bytes))
+}
+
+func init() {
+	common.RegisterCommand2("info", "show statistic and debuging data", &InfoCommand{})
+}
diff --git a/commands/multi.go b/commands/multi.go
index e67483221..d3bea8c58 100644
--- a/commands/multi.go
+++ b/commands/multi.go
@@ -56,7 +56,7 @@ type RunCommand struct {
 }
 
 func (mr *RunCommand) log() *log.Entry {
-	return log.WithField("builds", len(mr.buildsHelper.builds))
+	return log.WithField("builds", mr.buildsHelper.buildsCount())
 }
 
 func (mr *RunCommand) feedRunner(runner *common.RunnerConfig, runners chan *common.RunnerConfig) {
-- 
GitLab