Skip to content

Commit 82bafc4

Browse files
committed
refactor: split command to allow for further commands
BREAKING CHANGE: you now need to execute `run` for the main cmd
1 parent cd39d56 commit 82bafc4

5 files changed

Lines changed: 124 additions & 75 deletions

File tree

cmd/gitops-commit/main.go

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,12 @@ package main
22

33
import (
44
"fmt"
5-
"github.com/gsdevme/gitops-commit/internal/pkg/gitops"
6-
"github.com/spf13/cobra"
7-
"io/ioutil"
5+
"github.com/gsdevme/gitops-commit/internal/app/gitops-commit/cmd"
86
"os"
9-
"strings"
107
)
118

12-
func NewRootCommand() *cobra.Command {
13-
c := cobra.Command{
14-
Use: "gitops-commit",
15-
RunE: func(cmd *cobra.Command, args []string) error {
16-
key := cmd.Flag("key").Value.String()
17-
email := cmd.Flag("email").Value.String()
18-
newVersion := cmd.Flag("version").Value.String()
19-
notation := cmd.Flag("notation").Value.String()
20-
repo := strings.TrimRight(cmd.Flag("repo").Value.String(), "/")
21-
file := strings.TrimLeft(cmd.Flag("file").Value.String(), "/")
22-
23-
options, c, err := gitops.NewGitOptions(key)
24-
25-
if len(email) > 0 {
26-
options.Email = email
27-
}
28-
29-
if err != nil {
30-
return err
31-
}
32-
33-
defer c()
34-
35-
r, err := gitops.CloneRepository(options, repo)
36-
37-
if err != nil {
38-
return fmt.Errorf("failed to clone repo %s: %w", repo, err)
39-
}
40-
41-
filename := fmt.Sprintf("%s/%s", options.WorkingDirectory, file)
42-
43-
f, err := ioutil.ReadFile(filename)
44-
45-
if err != nil {
46-
return fmt.Errorf("cannot read file: %w", err)
47-
}
48-
49-
version, err := gitops.ReadCurrentVersion(f, notation)
50-
if err != nil {
51-
return fmt.Errorf("cannot read current version deployed: %w", err)
52-
}
53-
54-
err = gitops.WriteVersion(f, version, newVersion, filename)
55-
if err != nil {
56-
return fmt.Errorf("cannot write new version: %w", err)
57-
}
58-
59-
gitops.PushVersion(r, options, file, fmt.Sprintf("ci: update tag to %s", newVersion))
60-
61-
return nil
62-
},
63-
}
64-
65-
c.Flags().String("notation", "", "The yaml path in dot notation i.e. image.tag")
66-
c.Flags().String("email", "", "The email address of the commit")
67-
c.Flags().String("version", "", "The semver version you want to deploy i.e. v1.1.2")
68-
c.Flags().String("key", fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME")), "Absolute path to the private key")
69-
c.Flags().String("repo", "gsdevme/test", "The org/repo path")
70-
c.Flags().String("file", "/deployments/values.yaml", "The relative path in the repository to the file")
71-
72-
_ = c.MarkFlagRequired("notation")
73-
_ = c.MarkFlagRequired("tag")
74-
_ = c.MarkFlagRequired("file")
75-
76-
return &c
77-
}
78-
799
func main() {
80-
if err := NewRootCommand().Execute(); err != nil {
10+
if err := cmd.NewRootCommand().Execute(); err != nil {
8111
fmt.Println(err)
8212
os.Exit(1)
8313
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
func NewRootCommand() *cobra.Command {
6+
c := cobra.Command{
7+
Use: "Gitops Commit",
8+
Short: "A simple tool to mutate a version within a yaml file to trigger gitops operations",
9+
Run: func(cmd *cobra.Command, args []string) {
10+
cmd.HelpFunc()(cmd, args)
11+
},
12+
}
13+
14+
c.AddCommand(newRunCommand())
15+
16+
return &c
17+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/gsdevme/gitops-commit/internal/pkg/gitops"
6+
"github.com/spf13/cobra"
7+
"os"
8+
"strings"
9+
)
10+
11+
func newRunCommand() *cobra.Command {
12+
c := cobra.Command{
13+
Use: "run",
14+
RunE: func(cmd *cobra.Command, args []string) error {
15+
key := cmd.Flag("key").Value.String()
16+
email := cmd.Flag("email").Value.String()
17+
newVersion := cmd.Flag("version").Value.String()
18+
notation := cmd.Flag("notation").Value.String()
19+
repo := strings.TrimRight(cmd.Flag("repo").Value.String(), "/")
20+
file := strings.TrimLeft(cmd.Flag("file").Value.String(), "/")
21+
22+
options, c, err := gitops.NewGitOptions(key)
23+
24+
if len(email) > 0 {
25+
options.Email = email
26+
}
27+
28+
if err != nil {
29+
return err
30+
}
31+
32+
defer c()
33+
34+
err = gitops.DeployVersionHandler(gitops.DeployVersionCommand{
35+
GitOptions: *options,
36+
Repository: repo,
37+
Notation: notation,
38+
File: file,
39+
Version: newVersion,
40+
})
41+
42+
return err
43+
},
44+
}
45+
46+
c.Flags().String("notation", "", "The yaml path in dot notation i.e. image.tag")
47+
c.Flags().String("email", "", "The email address of the commit")
48+
c.Flags().String("version", "", "The semver version you want to deploy i.e. v1.1.2")
49+
c.Flags().String("key", fmt.Sprintf("%s/.ssh/id_rsa", os.Getenv("HOME")), "Absolute path to the private key")
50+
c.Flags().String("repo", "gsdevme/test", "The org/repo path")
51+
c.Flags().String("file", "/deployments/values.yaml", "The relative path in the repository to the file")
52+
53+
_ = c.MarkFlagRequired("notation")
54+
_ = c.MarkFlagRequired("tag")
55+
_ = c.MarkFlagRequired("file")
56+
57+
return &c
58+
}

internal/pkg/gitops/git.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewGitOptions(key string) (*GitOptions, func(), error) {
2424
return nil, nil, err
2525
}
2626

27-
keys, err := GetPasswordlessKey(key)
27+
keys, err := getPasswordlessKey(key)
2828

2929
if err != nil {
3030
return nil, nil, err
@@ -83,7 +83,7 @@ func PushVersion(r *git.Repository, options *GitOptions, file string, message st
8383
}
8484
}
8585

86-
func GetPasswordlessKey(key string) (*ssh.PublicKeys, error) {
86+
func getPasswordlessKey(key string) (*ssh.PublicKeys, error) {
8787
publicKeys, err := ssh.NewPublicKeysFromFile("git", key, "")
8888
if err != nil {
8989
return nil, fmt.Errorf("private/public key invalid: %w", err)
@@ -92,7 +92,7 @@ func GetPasswordlessKey(key string) (*ssh.PublicKeys, error) {
9292
return publicKeys, nil
9393
}
9494

95-
func CloneRepository(o *GitOptions, r string) (*git.Repository, error) {
95+
func cloneRepository(o *GitOptions, r string) (*git.Repository, error) {
9696

9797
return git.PlainClone(o.WorkingDirectory, false, &git.CloneOptions{
9898
Auth: &o.Keys,

internal/pkg/gitops/handler.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gitops
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
)
7+
8+
type DeployVersionCommand struct {
9+
GitOptions GitOptions
10+
Repository string
11+
Notation string
12+
File string
13+
Version string
14+
}
15+
16+
func DeployVersionHandler(c DeployVersionCommand) error {
17+
r, err := cloneRepository(&c.GitOptions, c.Repository)
18+
19+
if err != nil {
20+
return fmt.Errorf("failed to clone repo %s: %w", c.Repository, err)
21+
}
22+
23+
filename := fmt.Sprintf("%s/%s", c.GitOptions.WorkingDirectory, c.File)
24+
25+
f, err := ioutil.ReadFile(filename)
26+
27+
if err != nil {
28+
return fmt.Errorf("cannot read file: %w", err)
29+
}
30+
31+
version, err := ReadCurrentVersion(f, c.Notation)
32+
if err != nil {
33+
return fmt.Errorf("cannot read current version deployed: %w", err)
34+
}
35+
36+
err = WriteVersion(f, version, c.Version, filename)
37+
if err != nil {
38+
return fmt.Errorf("cannot write new version: %w", err)
39+
}
40+
41+
PushVersion(r, &c.GitOptions, c.File, fmt.Sprintf("ci: update tag to %s", c.Version))
42+
43+
return nil
44+
}

0 commit comments

Comments
 (0)