@@ -30,6 +30,7 @@ import (
3030 "github.com/argoproj-labs/argocd-autopilot/pkg/application"
3131 "github.com/argoproj-labs/argocd-autopilot/pkg/fs"
3232 "github.com/argoproj-labs/argocd-autopilot/pkg/git"
33+ apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
3334 aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
3435 wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
3536 wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
@@ -49,10 +50,17 @@ type (
4950 }
5051
5152 GitSourceDeleteOptions struct {
52- RuntimeName string
53- GsName string
54- CloneOpts * git.CloneOptions
55- Timeout time.Duration
53+ RuntimeName string
54+ GsName string
55+ InsCloneOpts * git.CloneOptions
56+ Timeout time.Duration
57+ }
58+
59+ GitSourceEditOptions struct {
60+ RuntimeName string
61+ GsName string
62+ InsCloneOpts * git.CloneOptions
63+ GsCloneOpts * git.CloneOptions
5664 }
5765)
5866
@@ -70,6 +78,7 @@ func NewGitSourceCommand() *cobra.Command {
7078 cmd .AddCommand (NewGitSourceCreateCommand ())
7179 cmd .AddCommand (NewGitSourceListCommand ())
7280 cmd .AddCommand (NewGitSourceDeleteCommand ())
81+ cmd .AddCommand (NewGitSourceEditCommand ())
7382
7483 return cmd
7584}
@@ -189,7 +198,7 @@ func RunGitSourceList(runtimeName string) error {
189198
190199func NewGitSourceDeleteCommand () * cobra.Command {
191200 var (
192- cloneOpts * git.CloneOptions
201+ insCloneOpts * git.CloneOptions
193202 )
194203
195204 cmd := & cobra.Command {
@@ -209,27 +218,116 @@ func NewGitSourceDeleteCommand() *cobra.Command {
209218 log .G (ctx ).Fatal ("must enter git-source name" )
210219 }
211220
212- cloneOpts .Parse ()
221+ insCloneOpts .Parse ()
213222 },
214223 RunE : func (cmd * cobra.Command , args []string ) error {
215224 ctx := cmd .Context ()
216225
217226 return RunDeleteGitSource (ctx , & GitSourceDeleteOptions {
218- RuntimeName : args [0 ],
219- GsName : args [1 ],
220- Timeout : aputil .MustParseDuration (cmd .Flag ("request-timeout" ).Value .String ()),
221- CloneOpts : cloneOpts ,
227+ RuntimeName : args [0 ],
228+ GsName : args [1 ],
229+ Timeout : aputil .MustParseDuration (cmd .Flag ("request-timeout" ).Value .String ()),
230+ InsCloneOpts : insCloneOpts ,
222231 })
223232 },
224233 }
225234
226- cloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
235+ insCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
227236 FS : memfs .New (),
228237 })
229238
230239 return cmd
231240}
232241
242+ func NewGitSourceEditCommand () * cobra.Command {
243+ var (
244+ insCloneOpts * git.CloneOptions
245+ gsCloneOpts * git.CloneOptions
246+ )
247+
248+ cmd := & cobra.Command {
249+ Use : "edit runtime_name git-source_name" ,
250+ Short : "edit a git-source of a runtime" ,
251+ Example : util .Doc (`
252+ <BIN> git-source edit runtime_name git-source_name --git-src-repo https://github.com/owner/repo-name/my-workflow
253+ ` ),
254+ PreRun : func (cmd * cobra.Command , args []string ) {
255+ ctx := cmd .Context ()
256+
257+ if len (args ) < 1 {
258+ log .G (ctx ).Fatal ("must enter a runtime name" )
259+ }
260+
261+ if len (args ) < 2 {
262+ log .G (ctx ).Fatal ("must enter a git-source name" )
263+ }
264+
265+ if gsCloneOpts .Repo == "" {
266+ log .G (ctx ).Fatal ("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow" )
267+ }
268+
269+ insCloneOpts .Parse ()
270+ gsCloneOpts .Parse ()
271+ },
272+ RunE : func (cmd * cobra.Command , args []string ) error {
273+ ctx := cmd .Context ()
274+
275+ return RunEditGitSource (ctx , & GitSourceEditOptions {
276+ RuntimeName : args [0 ],
277+ GsName : args [1 ],
278+ InsCloneOpts : insCloneOpts ,
279+ GsCloneOpts : gsCloneOpts ,
280+ })
281+ },
282+ }
283+
284+ insCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
285+ CreateIfNotExist : true ,
286+ FS : memfs .New (),
287+ })
288+
289+ gsCloneOpts = git .AddFlags (cmd , & git.AddFlagsOptions {
290+ Prefix : "git-src" ,
291+ Optional : true ,
292+ CreateIfNotExist : true ,
293+ FS : memfs .New (),
294+ })
295+
296+ return cmd
297+ }
298+
299+ func RunEditGitSource (ctx context.Context , opts * GitSourceEditOptions ) error {
300+ repo , fs , err := opts .InsCloneOpts .GetRepo (ctx )
301+ if err != nil {
302+ return fmt .Errorf ("failed to clone the installation repo, attemptint to edit git-source %s. Err: %w" , opts .GsName , err )
303+ }
304+
305+ c := & application.Config {}
306+ err = fs .ReadJson (fs .Join (apstore .Default .AppsDir , opts .GsName , opts .RuntimeName , "config.json" ), c )
307+ if err != nil {
308+ return fmt .Errorf ("failed to read the config.json of git-source: %s. Err: %w" , opts .GsName , err )
309+ }
310+
311+ c .SrcPath = opts .GsCloneOpts .Path ()
312+ c .SrcRepoURL = opts .GsCloneOpts .URL ()
313+ c .SrcTargetRevision = opts .GsCloneOpts .Revision ()
314+
315+ err = fs .WriteJson (fs .Join (apstore .Default .AppsDir , opts .GsName , opts .RuntimeName , "config.json" ), c )
316+ if err != nil {
317+ return fmt .Errorf ("failed to write the updated config.json of git-source: %s. Err: %w" , opts .GsName , err )
318+ }
319+
320+ _ , err = repo .Persist (ctx , & git.PushOptions {
321+ CommitMsg : "Persisted an updated git-source" ,
322+ })
323+
324+ if err != nil {
325+ return fmt .Errorf ("failed to persist the updated git-source: %s. Err: %w" , opts .GsName , err )
326+ }
327+
328+ return nil
329+ }
330+
233331func RunCreateGitSource (ctx context.Context , opts * GitSourceCreateOptions ) error {
234332 gsRepo , gsFs , err := opts .gsCloneOpts .GetRepo (ctx )
235333 if err != nil {
@@ -264,24 +362,24 @@ func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error
264362 return fmt .Errorf ("failed to create git-source application. Err: %w" , err )
265363 }
266364
267- log .G (ctx ).Infof ("done creating a new git-source: '%s'" , opts .gsName )
365+ log .G (ctx ).Infof ("Successfully created the git-source: '%s'" , opts .gsName )
268366
269367 return nil
270368}
271369
272370func RunDeleteGitSource (ctx context.Context , opts * GitSourceDeleteOptions ) error {
273371 err := apcmd .RunAppDelete (ctx , & apcmd.AppDeleteOptions {
274- CloneOpts : opts .CloneOpts ,
372+ CloneOpts : opts .InsCloneOpts ,
275373 ProjectName : opts .RuntimeName ,
276374 AppName : opts .GsName ,
277375 Global : false ,
278376 })
279377
280378 if err != nil {
281- return fmt .Errorf ("failed to delete git-source %s. Err: %w" , opts .GsName , err )
379+ return fmt .Errorf ("failed to delete the git-source %s. Err: %w" , opts .GsName , err )
282380 }
283381
284- log .G (ctx ).Debug ("successfully deleted git-source: %s" , opts .GsName )
382+ log .G (ctx ).Debug ("Successfully deleted the git-source: %s" , opts .GsName )
285383
286384 return nil
287385}
0 commit comments