@@ -15,6 +15,7 @@ import (
1515 "github.com/AlecAivazis/survey/v2/terminal"
1616 "github.com/Masterminds/sprig"
1717 "github.com/goccy/go-yaml"
18+ "github.com/outblocks/outblocks-cli/internal/fileutil"
1819 "github.com/outblocks/outblocks-cli/pkg/config"
1920 "github.com/outblocks/outblocks-cli/pkg/logger"
2021 "github.com/outblocks/outblocks-cli/pkg/plugins"
2930
3031type Init struct {
3132 log logger.Logger
32- loader * plugins.Loader
3333 pluginCacheDir string
3434 opts * InitOptions
3535}
@@ -48,10 +48,9 @@ type InitOptions struct {
4848 }
4949}
5050
51- func NewInit (log logger.Logger , loader * plugins. Loader , pluginCacheDir string , opts * InitOptions ) * Init {
51+ func NewInit (log logger.Logger , pluginCacheDir string , opts * InitOptions ) * Init {
5252 return & Init {
5353 log : log ,
54- loader : loader ,
5554 pluginCacheDir : pluginCacheDir ,
5655 opts : opts ,
5756 }
@@ -63,17 +62,16 @@ func funcMap() template.FuncMap {
6362 }
6463}
6564
66- func (d * Init ) Run (ctx context.Context , cfg * config. Project ) error {
65+ func (d * Init ) Run (ctx context.Context ) error {
6766 curDir , err := os .Getwd ()
6867 if err != nil {
6968 return fmt .Errorf ("getting current working dir error: %w" , err )
7069 }
7170
72- if cfg == nil {
73- cfg = & config. Project {}
71+ cfg := & config. Project {}
72+ loader := plugins . NewLoader ( curDir , d . pluginCacheDir )
7473
75- d .loader = plugins .NewLoader (curDir , d .pluginCacheDir )
76- } else if ! d .opts .Overwrite {
74+ if ! d .opts .Overwrite && fileutil .FindYAML (filepath .Join (curDir , config .ProjectYAMLName )) != "" {
7775 proceed := false
7876 prompt := & survey.Confirm {
7977 Message : "Project config already exists! Do you want to overwrite it?" ,
@@ -87,7 +85,7 @@ func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
8785 }
8886 }
8987
90- cfg , err = d .prompt (ctx , cfg , curDir )
88+ cfg , err = d .prompt (ctx , cfg , loader , curDir )
9189 if errors .Is (err , errInitCanceled ) {
9290 d .log .Println ("Init canceled." )
9391 return nil
@@ -97,7 +95,7 @@ func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
9795 return err
9896 }
9997
100- err = cfg .LoadPlugins (ctx , d .log , d . loader )
98+ err = cfg .LoadPlugins (ctx , d .log , loader )
10199 if err != nil {
102100 return err
103101 }
@@ -160,7 +158,7 @@ func (d *Init) Run(ctx context.Context, cfg *config.Project) error {
160158 return err
161159}
162160
163- func (d * Init ) prompt (ctx context.Context , cfg * config.Project , curDir string ) (* config.Project , error ) {
161+ func (d * Init ) prompt (ctx context.Context , cfg * config.Project , loader * plugins. Loader , curDir string ) (* config.Project , error ) {
164162 var qs []* survey.Question
165163
166164 if d .opts .Name == "" {
@@ -228,12 +226,12 @@ func (d *Init) prompt(ctx context.Context, cfg *config.Project, curDir string) (
228226
229227 cfg .Name = answers .Name
230228
231- _ , latestDeployVersion , err := d . loader .MatchingVersion (ctx , answers .DeployPlugin , "" , nil )
229+ _ , latestDeployVersion , err := loader .MatchingVersion (ctx , answers .DeployPlugin , "" , nil )
232230 if err != nil {
233231 return nil , fmt .Errorf ("error retrieving latest version of plugin '%s': %w" , answers .RunPlugin , err )
234232 }
235233
236- _ , latestRunVersion , err := d . loader .MatchingVersion (ctx , answers .RunPlugin , "" , nil )
234+ _ , latestRunVersion , err := loader .MatchingVersion (ctx , answers .RunPlugin , "" , nil )
237235 if err != nil {
238236 return nil , fmt .Errorf ("error retrieving latest version of plugin '%s': %w" , answers .RunPlugin , err )
239237 }
0 commit comments