Skip to content

Commit 03a7023

Browse files
feat: show progress flag
1 parent f4865bf commit 03a7023

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Config struct {
3636
MaintainedVersion string
3737
PrependChangelog bool
3838
DownloadPlugins bool
39+
ShowProgress bool
3940
}
4041

4142
func mustGetString(cmd *cobra.Command, name string) string {
@@ -124,6 +125,7 @@ func NewConfig(cmd *cobra.Command) (*Config, error) {
124125
MaintainedVersion: viper.GetString("maintainedVersion"),
125126
PrependChangelog: mustGetBool(cmd, "prepend-changelog"),
126127
DownloadPlugins: mustGetBool(cmd, "download-plugins"),
128+
ShowProgress: mustGetBool(cmd, "show-progress"),
127129
}
128130
return conf, nil
129131
}
@@ -177,6 +179,7 @@ func InitConfig(cmd *cobra.Command) error {
177179
cmd.Flags().Bool("allow-no-changes", false, "exit with code 0 if no changes are found, useful if semantic-release is automatically run")
178180
cmd.Flags().Bool("prepend-changelog", false, "if the changelog file already exist the new changelog is prepended")
179181
cmd.Flags().Bool("download-plugins", false, "downloads all required plugins if needed")
182+
cmd.Flags().Bool("show-progress", false, "shows the plugin download progress")
180183
cmd.Flags().SortFlags = true
181184

182185
viper.AddConfigPath(".")

pkg/plugin/discovery/discovery.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/cavaliercoder/grab"
2121
"github.com/go-semantic-release/semantic-release/v2/pkg/analyzer"
2222
"github.com/go-semantic-release/semantic-release/v2/pkg/condition"
23+
"github.com/go-semantic-release/semantic-release/v2/pkg/config"
2324
"github.com/go-semantic-release/semantic-release/v2/pkg/generator"
2425
"github.com/go-semantic-release/semantic-release/v2/pkg/hooks"
2526
"github.com/go-semantic-release/semantic-release/v2/pkg/plugin"
@@ -75,7 +76,15 @@ type apiPlugin struct {
7576
Versions map[string]*apiPluginRelease
7677
}
7778

78-
func getPluginInfo(name string) (*apiPlugin, error) {
79+
type Discovery struct {
80+
config *config.Config
81+
}
82+
83+
func New(config *config.Config) (*Discovery, error) {
84+
return &Discovery{config}, nil
85+
}
86+
87+
func (d *Discovery) getPluginInfo(name string) (*apiPlugin, error) {
7988
res, err := http.Get(fmt.Sprintf("%s/plugins/%s.json", PluginAPI, name))
8089
if err != nil {
8190
return nil, err
@@ -125,8 +134,8 @@ func showDownloadProgressBar(name string, res *grab.Response) {
125134
<-done
126135
}
127136

128-
func fetchPlugin(name, pth string, cons *semver.Constraints) (string, error) {
129-
pluginInfo, err := getPluginInfo(name)
137+
func (d *Discovery) fetchPlugin(name, pth string, cons *semver.Constraints) (string, error) {
138+
pluginInfo, err := d.getPluginInfo(name)
130139
if err != nil {
131140
return "", err
132141
}
@@ -176,7 +185,9 @@ func fetchPlugin(name, pth string, cons *semver.Constraints) (string, error) {
176185
}
177186

178187
res := grab.DefaultClient.Do(req)
179-
showDownloadProgressBar(name, res)
188+
if d.config.ShowProgress {
189+
showDownloadProgressBar(name, res)
190+
}
180191
if err := res.Err(); err != nil {
181192
return "", err
182193
}
@@ -220,7 +231,7 @@ func getMatchingVersionDir(pth string, cons *semver.Constraints) (string, error)
220231
return "", errors.New("no matching version found")
221232
}
222233

223-
func findPluginLocally(pth string, cons *semver.Constraints) (string, error) {
234+
func (d *Discovery) findPluginLocally(pth string, cons *semver.Constraints) (string, error) {
224235
vPth, err := getMatchingVersionDir(pth, cons)
225236
if err != nil {
226237
return "", err
@@ -263,7 +274,7 @@ func getPluginType(t string) string {
263274
return ""
264275
}
265276

266-
func FindPlugin(t, name string) (*plugin.PluginOpts, error) {
277+
func (d *Discovery) FindPlugin(t, name string) (*plugin.PluginOpts, error) {
267278
pType := getPluginType(t)
268279
if pType == "" {
269280
return nil, errors.New("invalid plugin type")
@@ -285,9 +296,9 @@ func FindPlugin(t, name string) (*plugin.PluginOpts, error) {
285296
return nil, err
286297
}
287298

288-
binPath, err := findPluginLocally(pPath, cons)
299+
binPath, err := d.findPluginLocally(pPath, cons)
289300
if err != nil {
290-
binPath, err = fetchPlugin(pName, pPath, cons)
301+
binPath, err = d.fetchPlugin(pName, pPath, cons)
291302
if err != nil {
292303
return nil, err
293304
}

pkg/plugin/manager/manager.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ import (
1313
)
1414

1515
type PluginManager struct {
16-
config *config.Config
16+
config *config.Config
17+
discovery *discovery.Discovery
1718
}
1819

1920
func New(config *config.Config) (*PluginManager, error) {
20-
return &PluginManager{config}, nil
21+
dis, err := discovery.New(config)
22+
if err != nil {
23+
return nil, err
24+
}
25+
return &PluginManager{
26+
config: config,
27+
discovery: dis,
28+
}, nil
2129
}
2230

2331
func (m *PluginManager) GetCICondition() (condition.CICondition, error) {
24-
opts, err := discovery.FindPlugin(condition.CIConditionPluginName, m.config.CIConditionPlugin)
32+
opts, err := m.discovery.FindPlugin(condition.CIConditionPluginName, m.config.CIConditionPlugin)
2533
if err != nil {
2634
return nil, err
2735
}
@@ -34,7 +42,7 @@ func (m *PluginManager) GetCICondition() (condition.CICondition, error) {
3442
}
3543

3644
func (m *PluginManager) GetProvider() (provider.Provider, error) {
37-
opts, err := discovery.FindPlugin(provider.PluginName, m.config.ProviderPlugin)
45+
opts, err := m.discovery.FindPlugin(provider.PluginName, m.config.ProviderPlugin)
3846
if err != nil {
3947
return nil, err
4048
}
@@ -47,7 +55,7 @@ func (m *PluginManager) GetProvider() (provider.Provider, error) {
4755
}
4856

4957
func (m *PluginManager) GetCommitAnalyzer() (analyzer.CommitAnalyzer, error) {
50-
opts, err := discovery.FindPlugin(analyzer.CommitAnalyzerPluginName, m.config.CommitAnalyzerPlugin)
58+
opts, err := m.discovery.FindPlugin(analyzer.CommitAnalyzerPluginName, m.config.CommitAnalyzerPlugin)
5159
if err != nil {
5260
return nil, err
5361
}
@@ -60,7 +68,7 @@ func (m *PluginManager) GetCommitAnalyzer() (analyzer.CommitAnalyzer, error) {
6068
}
6169

6270
func (m *PluginManager) GetChangelogGenerator() (generator.ChangelogGenerator, error) {
63-
opts, err := discovery.FindPlugin(generator.ChangelogGeneratorPluginName, m.config.ChangelogGeneratorPlugin)
71+
opts, err := m.discovery.FindPlugin(generator.ChangelogGeneratorPluginName, m.config.ChangelogGeneratorPlugin)
6472
if err != nil {
6573
return nil, err
6674
}
@@ -75,7 +83,7 @@ func (m *PluginManager) GetChangelogGenerator() (generator.ChangelogGenerator, e
7583
func (m *PluginManager) GetChainedUpdater() (*updater.ChainedUpdater, error) {
7684
updaters := make([]updater.FilesUpdater, 0)
7785
for _, pl := range m.config.FilesUpdaterPlugins {
78-
opts, err := discovery.FindPlugin(updater.FilesUpdaterPluginName, pl)
86+
opts, err := m.discovery.FindPlugin(updater.FilesUpdaterPluginName, pl)
7987
if err != nil {
8088
return nil, err
8189
}
@@ -96,7 +104,7 @@ func (m *PluginManager) GetChainedUpdater() (*updater.ChainedUpdater, error) {
96104
func (m *PluginManager) GetChainedHooksExecutor() (*hooks.ChainedHooksExecutor, error) {
97105
hooksChain := make([]hooks.Hooks, 0)
98106
for _, pl := range m.config.HooksPlugins {
99-
opts, err := discovery.FindPlugin(hooks.PluginName, pl)
107+
opts, err := m.discovery.FindPlugin(hooks.PluginName, pl)
100108
if err != nil {
101109
return nil, err
102110
}
@@ -125,21 +133,21 @@ func (m *PluginManager) FetchAllPlugins() error {
125133
generator.ChangelogGeneratorPluginName: m.config.ChangelogGeneratorPlugin,
126134
}
127135
for t, name := range pluginMap {
128-
_, err := discovery.FindPlugin(t, name)
136+
_, err := m.discovery.FindPlugin(t, name)
129137
if err != nil {
130138
return err
131139
}
132140
}
133141

134142
for _, pl := range m.config.FilesUpdaterPlugins {
135-
_, err := discovery.FindPlugin(updater.FilesUpdaterPluginName, pl)
143+
_, err := m.discovery.FindPlugin(updater.FilesUpdaterPluginName, pl)
136144
if err != nil {
137145
return err
138146
}
139147
}
140148

141149
for _, pl := range m.config.HooksPlugins {
142-
_, err := discovery.FindPlugin(hooks.PluginName, pl)
150+
_, err := m.discovery.FindPlugin(hooks.PluginName, pl)
143151
if err != nil {
144152
return err
145153
}

0 commit comments

Comments
 (0)