-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd-init-features.go
81 lines (62 loc) · 2.23 KB
/
cmd-init-features.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
package main
import (
"fmt"
"os"
"path"
"github.com/forj-oss/forjj-modules/trace"
core "jplugins/coremgt"
"jplugins/utils"
"github.com/alecthomas/kingpin"
)
type cmdInitFeatures struct {
cmd *kingpin.CmdClause
jenkinsHomePath *string
pluginsFeaturePath *string
pluginsFeatureFile *string
replace *bool
}
func (c *cmdInitFeatures) init(parent *kingpin.CmdClause) {
c.cmd = parent.Command("features", "Initialize the features file from Jenkins home.")
c.jenkinsHomePath = c.cmd.Flag("jenkins-home", "Path to the Jenkins home.").Default(defaultJenkinsHome).String()
c.pluginsFeatureFile = c.cmd.Flag("feature-file", "Full path to a feature file. The path must exist.").Default(featureFileName).String()
c.pluginsFeaturePath = c.cmd.Flag("feature-path", "Feature file name to create.").Default(".").String()
c.replace = c.cmd.Flag("force", "force to re-create a feature file which already exist.").Bool()
}
func (c *cmdInitFeatures) DoInitFeatures() {
App.setJenkinsHome(*c.jenkinsHomePath)
var elements *core.ElementsType
if App.checkJenkinsHome() {
if e, err := App.readFromJenkins() ; err != nil {
gotrace.Error("%s", err)
os.Exit(1)
} else {
elements = e
}
}
if !utils.CheckPath(*c.pluginsFeaturePath) {
gotrace.Error("'%s' is not a valid/accessible features path", *c.pluginsFeaturePath)
os.Exit(1)
}
err := c.saveFeatures(elements)
if err != nil {
gotrace.Error("Unable to create the feature file. %s", err)
os.Exit(1)
}
}
// saveFeatures
func (c *cmdInitFeatures) saveFeatures(elements *core.ElementsType) (err error) {
if utils.CheckFile(*c.pluginsFeaturePath, *c.pluginsFeatureFile) {
if !*c.replace {
err = fmt.Errorf("'%s/%s' already exist. Use --force to replace it", *c.pluginsFeaturePath, *c.pluginsFeatureFile)
return
}
gotrace.Info("Replacing '%s/%s'", *c.pluginsFeaturePath, *c.pluginsFeatureFile)
}
identified := elements.ExtractTopElements()
gotrace.Info("%d/%d plugin features detected. ", len(identified.GetElements("plugin")), len(elements.GetElements("plugin")))
if err = identified.WriteSimple(path.Join(*c.pluginsFeaturePath, *c.pluginsFeatureFile), 2); err != nil {
return
}
gotrace.Info("%s/%s saved.", *c.pluginsFeaturePath, *c.pluginsFeatureFile)
return
}