This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ci_dump_meta.go
113 lines (91 loc) · 2.58 KB
/
ci_dump_meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright © 2016-present Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package commands
import (
"bytes"
"encoding/json"
"flag"
"os"
"strings"
"github.com/mitchellh/cli"
helper "github.com/rande/gitlab-ci-helper"
)
type MetaBuild struct {
Id string `json:"id"`
Ref string `json:"ref"`
RefName string `json:"ref_name"`
Tag string `json:"tag"`
Stage string `json:"stage"`
JobName string `json:"job_name"`
}
type MetaProject struct {
Id string `json:"id"`
Dir string `json:"dir"`
}
type MetaServer struct {
Name string `json:"name"`
Revision string `json:"revision"`
Version string `json:"version"`
}
type Meta struct {
Build *MetaBuild `json:"build"`
Project *MetaProject `json:"project"`
Server *MetaServer `json:"server"`
}
type CiDumpMetaCommand struct {
Ui cli.Ui
Verbose bool
MetaFile string
}
func (c *CiDumpMetaCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("ci:meta", flag.ContinueOnError)
cmdFlags.Usage = func() {
c.Ui.Output(c.Help())
}
cmdFlags.BoolVar(&c.Verbose, "verbose", false, "")
cmdFlags.StringVar(&c.MetaFile, "file", "ci.json", "")
if err := cmdFlags.Parse(args); err != nil {
return 1
}
meta := &Meta{
Build: &MetaBuild{
Id: helper.GetEnv("CI_JOB_ID", os.Getenv("CI_BUILD_ID")),
Ref: helper.GetEnv("CI_COMMIT_SHA", os.Getenv("CI_BUILD_REF")),
RefName: helper.GetEnv("CI_COMMIT_REF_NAME", os.Getenv("CI_BUILD_REF_NAME")),
Tag: helper.GetEnv("CI_COMMIT_TAG", os.Getenv("CI_BUILD_TAG")),
Stage: helper.GetEnv("CI_JOB_STAGE", os.Getenv("CI_BUILD_STAGE")),
JobName: helper.GetEnv("CI_JOB_NAME", os.Getenv("CI_BUILD_NAME")),
},
Project: &MetaProject{
Id: os.Getenv("CI_PROJECT_ID"),
Dir: os.Getenv("CI_PROJECT_DIR"),
},
Server: &MetaServer{
Name: os.Getenv("CI_SERVER_NAME"),
Revision: os.Getenv("CI_SERVER_REVISION"),
Version: os.Getenv("CI_SERVER_VERSION"),
},
}
fp, _ := os.Create(c.MetaFile)
defer fp.Close()
b, _ := json.Marshal(meta)
var out bytes.Buffer
json.Indent(&out, b, "", " ")
out.WriteTo(fp)
return 0
}
func (c *CiDumpMetaCommand) Synopsis() string {
return "Dump a json file with build information."
}
func (c *CiDumpMetaCommand) Help() string {
helpText := `
Usage: gitlab-ci-helper ci:meta [options]
Dump meta information about ci into a ci.json file
Options:
-file Target file (default: ci.json)
-verbose Add verbose information to the output
`
return strings.TrimSpace(helpText)
}