Skip to content

Commit

Permalink
Merge pull request #761 from SomtochiAma/cli-refactoring
Browse files Browse the repository at this point in the history
Refactor get source commands
  • Loading branch information
stefanprodan committed Jan 26, 2021
2 parents b5ebdb1 + 584f0ee commit 13aa523
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 261 deletions.
85 changes: 19 additions & 66 deletions cmd/flux/get_source_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ limitations under the License.
package main

import (
"context"
"os"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"strconv"
"strings"

"github.com/fluxcd/flux2/internal/utils"
"github.com/fluxcd/pkg/apis/meta"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/spf13/cobra"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var getSourceBucketCmd = &cobra.Command{
Expand All @@ -42,70 +34,31 @@ var getSourceBucketCmd = &cobra.Command{
# List buckets from all namespaces
flux get sources helm --all-namespaces
`,
RunE: getSourceBucketCmdRun,
RunE: getCommand{
apiType: bucketType,
list: &bucketListAdapter{&sourcev1.BucketList{}},
}.run,
}

func init() {
getSourceCmd.AddCommand(getSourceBucketCmd)
}

func getSourceBucketCmdRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return err
}

var listOpts []client.ListOption
if !getArgs.allNamespaces {
listOpts = append(listOpts, client.InNamespace(rootArgs.namespace))
}
var list sourcev1.BucketList
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}

if len(list.Items) == 0 {
logger.Failuref("no bucket sources found in %s namespace", rootArgs.namespace)
return nil
func (a *bucketListAdapter) summariseItem(i int, includeNamespace bool) []string {
item := a.Items[i]
var revision string
if item.GetArtifact() != nil {
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
return append(nameColumns(&item, includeNamespace),
status, msg, revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)))
}

header := []string{"Name", "Ready", "Message", "Revision", "Suspended"}
if getArgs.allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, source := range list.Items {
var row []string
var revision string
if source.GetArtifact() != nil {
revision = source.GetArtifact().Revision
}
if c := apimeta.FindStatusCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
source.GetName(),
string(c.Status),
c.Message,
revision,
strings.Title(strconv.FormatBool(source.Spec.Suspend)),
}
} else {
row = []string{
source.GetName(),
string(metav1.ConditionFalse),
"waiting to be reconciled",
revision,
strings.Title(strconv.FormatBool(source.Spec.Suspend)),
}
}
if getArgs.allNamespaces {
row = append([]string{source.Namespace}, row...)
}
rows = append(rows, row)
func (a bucketListAdapter) headers(includeNamespace bool) []string {
headers := []string{"Name", "Ready", "Message", "Revision", "Suspended"}
if includeNamespace {
headers = append([]string{"Namespace"}, headers...)
}
utils.PrintTable(os.Stdout, header, rows)
return nil
return headers
}
83 changes: 18 additions & 65 deletions cmd/flux/get_source_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ limitations under the License.
package main

import (
"context"
"os"
"strconv"
"strings"

"github.com/fluxcd/flux2/internal/utils"
"github.com/fluxcd/pkg/apis/meta"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/spf13/cobra"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var getSourceHelmChartCmd = &cobra.Command{
Expand All @@ -42,70 +34,31 @@ var getSourceHelmChartCmd = &cobra.Command{
# List Helm charts from all namespaces
flux get sources chart --all-namespaces
`,
RunE: getSourceHelmChartCmdRun,
RunE: getCommand{
apiType: bucketType,
list: &helmChartListAdapter{&sourcev1.HelmChartList{}},
}.run,
}

func init() {
getSourceCmd.AddCommand(getSourceHelmChartCmd)
}

func getSourceHelmChartCmdRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return err
}

var listOpts []client.ListOption
if !getArgs.allNamespaces {
listOpts = append(listOpts, client.InNamespace(rootArgs.namespace))
}
var list sourcev1.HelmChartList
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}

if len(list.Items) == 0 {
logger.Failuref("no chart sources found in %s namespace", rootArgs.namespace)
return nil
func (a *helmChartListAdapter) summariseItem(i int, includeNamespace bool) []string {
item := a.Items[i]
var revision string
if item.GetArtifact() != nil {
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
return append(nameColumns(&item, includeNamespace),
status, msg, revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)))
}

header := []string{"Name", "Ready", "Message", "Revision", "Suspended"}
if getArgs.allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, source := range list.Items {
var row []string
var revision string
if source.GetArtifact() != nil {
revision = source.GetArtifact().Revision
}
if c := apimeta.FindStatusCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
source.GetName(),
string(c.Status),
c.Message,
revision,
strings.Title(strconv.FormatBool(source.Spec.Suspend)),
}
} else {
row = []string{
source.GetName(),
string(metav1.ConditionFalse),
"waiting to be reconciled",
revision,
strings.Title(strconv.FormatBool(source.Spec.Suspend)),
}
}
if getArgs.allNamespaces {
row = append([]string{source.Namespace}, row...)
}
rows = append(rows, row)
func (a helmChartListAdapter) headers(includeNamespace bool) []string {
headers := []string{"Name", "Ready", "Message", "Revision", "Suspended"}
if includeNamespace {
headers = append([]string{"Namespace"}, headers...)
}
utils.PrintTable(os.Stdout, header, rows)
return nil
return headers
}
83 changes: 18 additions & 65 deletions cmd/flux/get_source_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ limitations under the License.
package main

import (
"context"
"os"
"strconv"
"strings"

"github.com/fluxcd/flux2/internal/utils"
"github.com/fluxcd/pkg/apis/meta"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"github.com/spf13/cobra"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var getSourceGitCmd = &cobra.Command{
Expand All @@ -42,70 +34,31 @@ var getSourceGitCmd = &cobra.Command{
# List Git repositories from all namespaces
flux get sources git --all-namespaces
`,
RunE: getSourceGitCmdRun,
RunE: getCommand{
apiType: bucketType,
list: &gitRepositoryListAdapter{&sourcev1.GitRepositoryList{}},
}.run,
}

func init() {
getSourceCmd.AddCommand(getSourceGitCmd)
}

func getSourceGitCmdRun(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext)
if err != nil {
return err
}

var listOpts []client.ListOption
if !getArgs.allNamespaces {
listOpts = append(listOpts, client.InNamespace(rootArgs.namespace))
}
var list sourcev1.GitRepositoryList
err = kubeClient.List(ctx, &list, listOpts...)
if err != nil {
return err
}

if len(list.Items) == 0 {
logger.Failuref("no git sources found in %s namespace", rootArgs.namespace)
return nil
func (a *gitRepositoryListAdapter) summariseItem(i int, includeNamespace bool) []string {
item := a.Items[i]
var revision string
if item.GetArtifact() != nil {
revision = item.GetArtifact().Revision
}
status, msg := statusAndMessage(item.Status.Conditions)
return append(nameColumns(&item, includeNamespace),
status, msg, revision, strings.Title(strconv.FormatBool(item.Spec.Suspend)))
}

header := []string{"Name", "Ready", "Message", "Revision", "Suspended"}
if getArgs.allNamespaces {
header = append([]string{"Namespace"}, header...)
}
var rows [][]string
for _, source := range list.Items {
var row []string
var revision string
if source.GetArtifact() != nil {
revision = source.GetArtifact().Revision
}
if c := apimeta.FindStatusCondition(source.Status.Conditions, meta.ReadyCondition); c != nil {
row = []string{
source.GetName(),
string(c.Status),
c.Message,
revision,
strings.Title(strconv.FormatBool(source.Spec.Suspend)),
}
} else {
row = []string{
source.GetName(),
string(metav1.ConditionFalse),
"waiting to be reconciled",
revision,
strings.Title(strconv.FormatBool(source.Spec.Suspend)),
}
}
if getArgs.allNamespaces {
row = append([]string{source.Namespace}, row...)
}
rows = append(rows, row)
func (a gitRepositoryListAdapter) headers(includeNamespace bool) []string {
headers := []string{"Name", "Ready", "Message", "Revision", "Suspended"}
if includeNamespace {
headers = append([]string{"Namespace"}, headers...)
}
utils.PrintTable(os.Stdout, header, rows)
return nil
return headers
}

0 comments on commit 13aa523

Please sign in to comment.