forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep_nexus.go
135 lines (122 loc) · 3.47 KB
/
step_nexus.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cmd
import (
"io"
"github.com/spf13/cobra"
"fmt"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/util"
"io/ioutil"
"path/filepath"
"strings"
)
const (
statingRepositoryIdPrefix = "stagingRepository.id="
statingRepositoryProperties = "target/nexus-staging/staging/*.properties"
)
// StepNexusOptions contains the command line flags
type StepNexusOptions struct {
StepOptions
}
// NewCmdStepNexus Steps a command object for the "step" command
func NewCmdStepNexus(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &StepNexusOptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "nexus",
Short: "nexus [command]",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
cmdutil.CheckErr(err)
},
}
cmd.AddCommand(NewCmdStepNexusDrop(f, out, errOut))
cmd.AddCommand(NewCmdStepNexusRelease(f, out, errOut))
return cmd
}
// Run implements this command
func (o *StepNexusOptions) Run() error {
return o.Cmd.Help()
}
func (o *StepNexusOptions) findStagingRepoIds() ([]string, error) {
answer := []string{}
files, err := filepath.Glob(statingRepositoryProperties)
if err != nil {
return answer, err
}
for _, f := range files {
data, err := ioutil.ReadFile(f)
if err != nil {
return answer, fmt.Errorf("Failed to load file %s: %s", f, err)
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, statingRepositoryIdPrefix) {
id := strings.TrimSpace(strings.TrimPrefix(line, statingRepositoryIdPrefix))
if id != "" {
answer = append(answer, id)
}
}
}
}
return answer, nil
}
func (o *StepNexusOptions) dropRepositories(repoIds []string, message string) error {
var answer error
for _, repoId := range repoIds {
err := o.dropRepository(repoId, message)
if err != nil {
o.warnf("Failed to drop repository %s: %s\n", util.ColorInfo(repoIds), util.ColorError(err))
if answer == nil {
answer = err
}
}
}
return answer
}
func (o *StepNexusOptions) dropRepository(repoId string, message string) error {
if repoId == "" {
return nil
}
o.Printf("Dropping nexus release repository %s\n", util.ColorInfo(repoId))
err := o.runCommand("mvn",
"org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-drop",
"-DserverId=oss-sonatype-staging",
"-DnexusUrl=https://oss.sonatype.org",
"-DstagingRepositoryId="+repoId,
"-Ddescription=\""+message+"\" -DstagingProgressTimeoutMinutes=60")
if err != nil {
o.warnf("Failed to drop repository %s due to: %s\n", repoId, err)
} else {
o.Printf("Dropped repository %s\n", util.ColorInfo(repoId))
}
return err
}
func (o *StepNexusOptions) releaseRepository(repoId string) error {
if repoId == "" {
return nil
}
o.Printf("Releasing nexus release repository %s\n", util.ColorInfo(repoId))
options := o
err := options.runCommand("mvn",
"org.sonatype.plugins:nexus-staging-maven-plugin:1.6.5:rc-release",
"-DserverId=oss-sonatype-staging",
"-DnexusUrl=https://oss.sonatype.org",
"-DstagingRepositoryId="+repoId,
"-Ddescription=\"Next release is ready\" -DstagingProgressTimeoutMinutes=60")
if err != nil {
o.warnf("Failed to release repository %s due to: %s\n", repoId, err)
} else {
o.Printf("Released repository %s\n", util.ColorInfo(repoId))
}
return err
}