Skip to content

Commit 80b888f

Browse files
CR-5825 (#61)
* initial commit * ins and gs clone opts * edit logic * tested successfully * . * without separate struct * removed redundant * added gs name to error logs * not capitalized * updated version to 0.0.67 * ran codegen * fixed lint error Co-authored-by: Noam Gal <noam.gal@codefresh.io>
1 parent d953546 commit 80b888f

File tree

6 files changed

+159
-19
lines changed

6 files changed

+159
-19
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.66
1+
VERSION=v0.0.67
22
OUT_DIR=dist
33
YEAR?=$(shell date +"%Y")
44

cmd/commands/git-source.go

Lines changed: 113 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

190199
func 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+
233331
func 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

272370
func 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
}

docs/commands/cli-v2_git-source.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ cli-v2 git-source [flags]
2626
* [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops
2727
* [cli-v2 git-source create](cli-v2_git-source_create.md) - add a new git-source to an existing runtime
2828
* [cli-v2 git-source delete](cli-v2_git-source_delete.md) - delete a git-source from a runtime
29+
* [cli-v2 git-source edit](cli-v2_git-source_edit.md) - edit a git-source of a runtime
2930
* [cli-v2 git-source list](cli-v2_git-source_list.md) - List all Codefresh git-sources of a given runtime
3031

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## cli-v2 git-source edit
2+
3+
edit a git-source of a runtime
4+
5+
```
6+
cli-v2 git-source edit runtime_name git-source_name [flags]
7+
```
8+
9+
### Examples
10+
11+
```
12+
13+
cli-v2 git-source edit runtime_name git-source_name --git-src-repo https://github.com/owner/repo-name/my-workflow
14+
15+
```
16+
17+
### Options
18+
19+
```
20+
--git-src-git-token string Your git provider api token [GIT_SRC_GIT_TOKEN]
21+
--git-src-provider string The git provider, one of: gitea|github
22+
--git-src-repo string Repository URL [GIT_SRC_GIT_REPO]
23+
-t, --git-token string Your git provider api token [GIT_TOKEN]
24+
-h, --help help for edit
25+
--provider string The git provider, one of: gitea|github
26+
--repo string Repository URL [GIT_REPO]
27+
```
28+
29+
### Options inherited from parent commands
30+
31+
```
32+
--auth-context string Run the next command using a specific authentication context
33+
--cfconfig string Custom path for authentication contexts config file (default "/home/user")
34+
--insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io)
35+
--request-timeout duration Request timeout (default 30s)
36+
```
37+
38+
### SEE ALSO
39+
40+
* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes
41+

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cf version
2020
### Linux
2121
```bash
2222
# download and extract the binary
23-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.66/cf-linux-amd64.tar.gz | tar zx
23+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.67/cf-linux-amd64.tar.gz | tar zx
2424

2525
# move the binary to your $PATH
2626
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -32,7 +32,7 @@ cf version
3232
### Mac
3333
```bash
3434
# download and extract the binary
35-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.66/cf-darwin-amd64.tar.gz | tar zx
35+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.67/cf-darwin-amd64.tar.gz | tar zx
3636

3737
# move the binary to your $PATH
3838
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.0
8-
version: 0.0.66
8+
version: 0.0.67
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)