-
Notifications
You must be signed in to change notification settings - Fork 12
/
share.go
188 lines (161 loc) · 5.53 KB
/
share.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cli
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"github.com/bitrise-io/colorstring"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
)
const (
// ShareFilename ...
ShareFilename string = "share.json"
)
// ShareModel ...
type ShareModel struct {
Collection string
StepID string
StepTag string
}
// ShareBranchName ...
func (share ShareModel) ShareBranchName() string {
return share.StepID + "-" + share.StepTag
}
// DeleteShareSteplibFile ...
func DeleteShareSteplibFile() error {
return command.RemoveDir(getShareFilePath())
}
// ReadShareSteplibFromFile ...
func ReadShareSteplibFromFile() (ShareModel, error) {
if exist, err := pathutil.IsPathExists(getShareFilePath()); err != nil {
return ShareModel{}, err
} else if !exist {
return ShareModel{}, errors.New("No share steplib found")
}
bytes, err := fileutil.ReadBytesFromFile(getShareFilePath())
if err != nil {
return ShareModel{}, err
}
share := ShareModel{}
if err := json.Unmarshal(bytes, &share); err != nil {
return ShareModel{}, err
}
return share, nil
}
// WriteShareSteplibToFile ...
func WriteShareSteplibToFile(share ShareModel) error {
var bytes []byte
bytes, err := json.MarshalIndent(share, "", "\t")
if err != nil {
log.Errorf("Failed to parse json, error: %s", err)
return err
}
return fileutil.WriteBytesToFile(getShareFilePath(), bytes)
}
// GuideTextForStepAudit ...
func GuideTextForStepAudit(toolMode bool) string {
name := "stepman"
if toolMode {
name = "bitrise"
}
b := colorstring.NewBuilder()
b.Plain("First, you need to ensure that your step is stored in a ").Blue("public git repository").NewLine()
b.Plain("and it follows our ").Blue("step development guideline").Plain(": https://github.com/bitrise-io/bitrise/blob/master/_docs/step-development-guideline.md.").NewLine()
b.NewLine()
b.Plain("To audit your step on your local machine call ").Blue("$ %s audit --step-yml path/to/your/step.yml", name)
return b.String()
}
// GuideTextForStart ...
func GuideTextForStart() string {
b := colorstring.NewBuilder()
b.Blue("Fork the StepLib repository ").Plain("you want to share your Step in.").NewLine()
b.Plain(`You can find the main ("official") StepLib repository at `).Plain("https://github.com/bitrise-io/bitrise-steplib")
return b.String()
}
// GuideTextForShareStart ...
func GuideTextForShareStart(toolMode bool) string {
name := "stepman"
if toolMode {
name = "bitrise"
}
b := colorstring.NewBuilder()
b.Plain("Call ").Blue("$ %s share start -c https://github.com/[your-username]/bitrise-steplib.git", name).Plain(", with the git clone URL of your forked StepLib repository.").NewLine()
b.Plain("This will prepare your forked StepLib locally for sharing.")
return b.String()
}
// GuideTextForShareCreate ...
func GuideTextForShareCreate(toolMode bool) string {
name := "stepman"
if toolMode {
name = "bitrise"
}
b := colorstring.NewBuilder()
b.Plain("Next, call ").Blue("$ %s share create --tag [step-version-tag] --git [step-git-uri].git --stepid [step-id]", name).Plain(",").NewLine()
b.Plain("to add your Step to your forked StepLib repository (locally).").NewLine()
b.NewLine()
b.Yellow("Important: ").Plain("you have to add the (version) tag to your Step's repository.")
return b.String()
}
// GuideTextForAudit ...
func GuideTextForAudit(toolMode bool) string {
name := "stepman"
if toolMode {
name = "bitrise"
}
b := colorstring.NewBuilder()
b.Plain("You can call ").Blue("$ %s audit -c https://github.com/[your-username]/bitrise-steplib.git ", name).NewLine()
b.Plain("to perform a complete health-check on your forked StepLib before submitting your Pull Request.").NewLine()
b.NewLine()
b.Plain("This can help you catch issues which might prevent your Step from being accepted.")
return b.String()
}
// GuideTextForShareFinish ...
func GuideTextForShareFinish(toolMode bool) string {
name := "stepman"
if toolMode {
name = "bitrise"
}
b := colorstring.NewBuilder()
b.Plain("Almost done! You should review your Step's step.yml file (the one added to the local StepLib),").NewLine()
b.Plain("and once you're happy with it call ").Blue("$ %s share finish", name).NewLine()
b.NewLine()
b.Plain("This will commit & push the step.yml into your forked StepLib repository.")
return b.String()
}
// GuideTextForFinish ...
func GuideTextForFinish() string {
b := colorstring.NewBuilder()
b.Plain("The only remaining thing is to ").Blue("create a Pull Request").Plain(" in the original StepLib repository. And you are done!")
return b.String()
}
func share(c *cli.Context) {
toolMode := c.Bool(ToolMode)
b := colorstring.NewBuilder()
b.Plain("Do you want to share your own Step with the world? Awesome!").NewLine()
b.NewLine()
b.Plain("Just follow these steps:").NewLine()
b.NewLine()
b.Plain("0. ").Plain(GuideTextForStepAudit(toolMode)).NewLine()
b.NewLine()
b.Plain("1. ").Plain(GuideTextForStart()).NewLine()
b.NewLine()
b.Plain("2. ").Plain(GuideTextForShareStart(toolMode)).NewLine()
b.NewLine()
b.Plain("3. ").Plain(GuideTextForShareCreate(toolMode)).NewLine()
b.NewLine()
b.Plain("4. ").Plain(GuideTextForAudit(toolMode)).NewLine()
b.NewLine()
b.Plain("5. ").Plain(GuideTextForShareFinish(toolMode)).NewLine()
b.NewLine()
b.Plain("6. ").Plain(GuideTextForFinish()).NewLine()
b.NewLine()
fmt.Print(b.String())
}
func getShareFilePath() string {
return filepath.Join(stepman.GetStepmanDirPath(), ShareFilename)
}