Skip to content

Commit

Permalink
refactor options into 1 struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake committed Feb 18, 2022
1 parent 2fdf317 commit 7cb7c41
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 91 deletions.
6 changes: 3 additions & 3 deletions cmd/gitops/add/profiles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"k8s.io/client-go/util/homedir"
)

var opts profiles.AddOptions
var opts profiles.Options

// AddCommand provides support for adding a profile to a cluster.
func AddCommand() *cobra.Command {
Expand Down Expand Up @@ -65,7 +65,7 @@ func addProfileCmdRunE() func(*cobra.Command, []string) error {
factory := services.NewFactory(fluxClient, log)
providerClient := internal.NewGitProviderClient(os.Stdout, os.LookupEnv, auth.NewAuthCLIHandler, log)

if err := validateAddOptions(opts); err != nil {
if err := validateOptions(opts); err != nil {
return err
}

Expand Down Expand Up @@ -103,7 +103,7 @@ func addProfileCmdRunE() func(*cobra.Command, []string) error {
}
}

func validateAddOptions(opts profiles.AddOptions) error {
func validateOptions(opts profiles.Options) error {
if models.ApplicationNameTooLong(opts.Name) {
return fmt.Errorf("--name value is too long: %s; must be <= %d characters",
opts.Name, models.MaxKubernetesResourceNameLength)
Expand Down
2 changes: 1 addition & 1 deletion cmd/gitops/update/profiles/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/client-go/util/homedir"
)

var opts profiles.UpdateOptions
var opts profiles.Options

// UpdateCommand provides support for updating a profile that is installed on a cluster.
func UpdateCommand() *cobra.Command {
Expand Down
28 changes: 9 additions & 19 deletions pkg/services/profiles/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package profiles
import (
"context"
"fmt"
"strings"

"github.com/weaveworks/weave-gitops/pkg/git"
"github.com/weaveworks/weave-gitops/pkg/gitproviders"
Expand All @@ -17,21 +18,9 @@ import (

const AddCommitMessage = "Add profile manifests"

type AddOptions struct {
Name string
Cluster string
ConfigRepo string
Version string
ProfilesPort string
Namespace string
Kubeconfig string
AutoMerge bool
PROptions
}

// Add installs an available profile in a cluster's namespace by appending a HelmRelease to the profile manifest in the config repo,
// provided that such a HelmRelease does not exist with the same profile name and version in the same namespace and cluster.
func (s *ProfilesSvc) Add(ctx context.Context, gitProvider gitproviders.GitProvider, opts AddOptions) error {
func (s *ProfilesSvc) Add(ctx context.Context, gitProvider gitproviders.GitProvider, opts Options) error {
configRepoURL, err := gitproviders.NewRepoURL(opts.ConfigRepo)
if err != nil {
return fmt.Errorf("failed to parse url: %w", err)
Expand Down Expand Up @@ -75,7 +64,7 @@ func (s *ProfilesSvc) Add(ctx context.Context, gitProvider gitproviders.GitProvi
}

path := git.GetProfilesPath(opts.Cluster, models.WegoProfilesPath)
pr, err := gitProvider.CreatePullRequest(ctx, configRepoURL, addPRInfo(opts, defaultBranch, gitprovider.CommitFile{
pr, err := gitProvider.CreatePullRequest(ctx, configRepoURL, prInfo(opts, "add", defaultBranch, gitprovider.CommitFile{
Path: &path,
Content: &content,
}))
Expand All @@ -99,18 +88,19 @@ func (s *ProfilesSvc) Add(ctx context.Context, gitProvider gitproviders.GitProvi
return nil
}

func addPRInfo(opts AddOptions, defaultBranch string, commitFile gitprovider.CommitFile) gitproviders.PullRequestInfo {
title := fmt.Sprintf("GitOps add %s", opts.Name)
func prInfo(opts Options, action, defaultBranch string, commitFile gitprovider.CommitFile) gitproviders.PullRequestInfo {
title := fmt.Sprintf("GitOps %s %s", action, opts.Name)

if opts.Title != "" {
title = opts.Title
}

description := fmt.Sprintf("Add manifest for %s profile", opts.Name)
description := fmt.Sprintf("%s manifest for %s profile", strings.Title(action), opts.Name)
if opts.Description != "" {
description = opts.Description
}

commitMessage := AddCommitMessage
commitMessage := fmt.Sprintf("%s profile manifests", strings.Title(action))
if opts.Message != "" {
commitMessage = opts.Message
}
Expand All @@ -135,7 +125,7 @@ func addPRInfo(opts AddOptions, defaultBranch string, commitFile gitprovider.Com
}
}

func (s *ProfilesSvc) printAddSummary(opts AddOptions) {
func (s *ProfilesSvc) printAddSummary(opts Options) {
s.Logger.Println("Adding profile:\n")
s.Logger.Println("Name: %s", opts.Name)
s.Logger.Println("Version: %s", opts.Version)
Expand Down
13 changes: 9 additions & 4 deletions pkg/services/profiles/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"sigs.k8s.io/yaml"
)

var addOptions profiles.AddOptions
var addOptions profiles.Options

var _ = Describe("Add", func() {
var (
Expand All @@ -40,7 +40,7 @@ var _ = Describe("Add", func() {
fakePR = &fakegitprovider.PullRequest{}
profilesSvc = profiles.NewService(clientSet, fakeLogger)

addOptions = profiles.AddOptions{
addOptions = profiles.Options{
ConfigRepo: "ssh://git@github.com/owner/config-repo.git",
Name: "podinfo",
Cluster: "prod",
Expand Down Expand Up @@ -83,7 +83,12 @@ var _ = Describe("Add", func() {

When("PR settings are configured", func() {
It("opens a PR with the configuration", func() {
addOptions.PROptions = profiles.PROptions{
addOptions = profiles.Options{
ConfigRepo: "ssh://git@github.com/owner/config-repo.git",
Name: "podinfo",
Cluster: "prod",
Namespace: "weave-system",
Version: "latest",
HeadBranch: "foo",
BaseBranch: "bar",
Message: "sup",
Expand Down Expand Up @@ -221,7 +226,7 @@ var _ = Describe("Add", func() {

When("the config repository does not exist", func() {
It("fails if the --config-repo url format is wrong", func() {
addOptions = profiles.AddOptions{
addOptions = profiles.Options{
Name: "foo",
ConfigRepo: "{http:/-*wrong-url-827",
Cluster: "prod",
Expand Down
24 changes: 16 additions & 8 deletions pkg/services/profiles/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,27 @@ const (

type ProfilesService interface {
// Add installs a profile on a cluster
Add(ctx context.Context, gitProvider gitproviders.GitProvider, opts AddOptions) error
Add(ctx context.Context, gitProvider gitproviders.GitProvider, opts Options) error
// Get lists all the available profiles in a cluster
Get(ctx context.Context, opts GetOptions) error
// Update updates a profile
Update(ctx context.Context, gitProvider gitproviders.GitProvider, opts UpdateOptions) error
Update(ctx context.Context, gitProvider gitproviders.GitProvider, opts Options) error
}

type PROptions struct {
HeadBranch string
BaseBranch string
Message string
Title string
Description string
type Options struct {
Name string
Cluster string
ConfigRepo string
Version string
ProfilesPort string
Namespace string
Kubeconfig string
AutoMerge bool
HeadBranch string
BaseBranch string
Message string
Title string
Description string
}

type ProfilesSvc struct {
Expand Down
55 changes: 3 additions & 52 deletions pkg/services/profiles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,12 @@ import (

"github.com/fluxcd/go-git-providers/gitprovider"
helmv2beta1 "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/google/uuid"
)

const UpdateCommitMessage = "Update profile manifests"

type UpdateOptions struct {
Name string
Cluster string
ConfigRepo string
Version string
ProfilesPort string
Namespace string
Kubeconfig string
AutoMerge bool
PROptions
}

// Update updates an installed profile
func (s *ProfilesSvc) Update(ctx context.Context, gitProvider gitproviders.GitProvider, opts UpdateOptions) error {
func (s *ProfilesSvc) Update(ctx context.Context, gitProvider gitproviders.GitProvider, opts Options) error {
configRepoURL, err := gitproviders.NewRepoURL(opts.ConfigRepo)
if err != nil {
return fmt.Errorf("failed to parse url: %w", err)
Expand Down Expand Up @@ -72,7 +59,7 @@ func (s *ProfilesSvc) Update(ctx context.Context, gitProvider gitproviders.GitPr

path := git.GetProfilesPath(opts.Cluster, models.WegoProfilesPath)

pr, err := gitProvider.CreatePullRequest(ctx, configRepoURL, updatePRInfo(opts, defaultBranch, gitprovider.CommitFile{
pr, err := gitProvider.CreatePullRequest(ctx, configRepoURL, prInfo(opts, "update", defaultBranch, gitprovider.CommitFile{
Path: &path,
Content: &content,
}))
Expand All @@ -95,43 +82,7 @@ func (s *ProfilesSvc) Update(ctx context.Context, gitProvider gitproviders.GitPr
return nil
}

func updatePRInfo(opts UpdateOptions, defaultBranch string, commitFile gitprovider.CommitFile) gitproviders.PullRequestInfo {
title := fmt.Sprintf("GitOps update %s", opts.Name)
if opts.Title != "" {
title = opts.Title
}

description := fmt.Sprintf("Update manifest for %s profile", opts.Name)
if opts.Description != "" {
description = opts.Description
}

commitMessage := UpdateCommitMessage
if opts.Message != "" {
commitMessage = opts.Message
}

headBranch := defaultBranch
if opts.HeadBranch != "" {
headBranch = opts.HeadBranch
}

newBranch := uuid.New().String()
if opts.BaseBranch != "" {
newBranch = opts.BaseBranch
}

return gitproviders.PullRequestInfo{
Title: title,
Description: description,
CommitMessage: commitMessage,
TargetBranch: headBranch,
NewBranch: newBranch,
Files: []gitprovider.CommitFile{commitFile},
}
}

func (s *ProfilesSvc) printUpdateSummary(opts UpdateOptions) {
func (s *ProfilesSvc) printUpdateSummary(opts Options) {
s.Logger.Println("Updating profile:\n")
s.Logger.Println("Name: %s", opts.Name)
s.Logger.Println("Version: %s", opts.Version)
Expand Down
13 changes: 9 additions & 4 deletions pkg/services/profiles/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/client-go/testing"
)

var updateOptions profiles.UpdateOptions
var updateOptions profiles.Options

var _ = Describe("Update Profile(s)", func() {
var (
Expand All @@ -40,7 +40,7 @@ var _ = Describe("Update Profile(s)", func() {
fakePR = &fakegitprovider.PullRequest{}
profilesSvc = profiles.NewService(clientSet, fakeLogger)

updateOptions = profiles.UpdateOptions{
updateOptions = profiles.Options{
ConfigRepo: "ssh://git@github.com/owner/config-repo.git",
Name: "podinfo",
Cluster: "prod",
Expand Down Expand Up @@ -106,7 +106,12 @@ var _ = Describe("Update Profile(s)", func() {

When("PR settings are configured", func() {
It("opens a PR with the configuration", func() {
updateOptions.PROptions = profiles.PROptions{
updateOptions = profiles.Options{
ConfigRepo: "ssh://git@github.com/owner/config-repo.git",
Name: "podinfo",
Cluster: "prod",
Namespace: "weave-system",
Version: "latest",
HeadBranch: "foo",
BaseBranch: "bar",
Message: "sup",
Expand Down Expand Up @@ -267,7 +272,7 @@ var _ = Describe("Update Profile(s)", func() {

When("the config repository does not exist", func() {
It("fails if the --config-repo url format is wrong", func() {
updateOptions = profiles.UpdateOptions{
updateOptions = profiles.Options{
Name: "foo",
ConfigRepo: "{http:/-*wrong-url-827",
Cluster: "prod",
Expand Down

0 comments on commit 7cb7c41

Please sign in to comment.