-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds List Command implementation to list the approvaltask
Signed-off-by: PuneetPunamiya <ppunamiy@redhat.com>
- Loading branch information
1 parent
b242bff
commit 5ff906c
Showing
30 changed files
with
2,833 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,113 @@ | ||
package list | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"text/tabwriter" | ||
"text/template" | ||
|
||
"github.com/fatih/color" | ||
"github.com/openshift-pipelines/manual-approval-gate/pkg/apis/approvaltask/v1alpha1" | ||
cli "github.com/openshift-pipelines/manual-approval-gate/pkg/cli" | ||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func Root() *cobra.Command { | ||
type ListOptions struct { | ||
AllNamespaces bool | ||
} | ||
|
||
var ConditionColor = map[string]color.Attribute{ | ||
"Rejected": color.FgHiRed, | ||
"Approved": color.FgHiGreen, | ||
"Pending": color.FgHiBlue, | ||
} | ||
|
||
const listTemplate = `NAME NumberOfApprovalsRequired PendingApprovals Rejected STATUS{{range .ApprovalTasks.Items}} | ||
{{.Name}} {{.Spec.NumberOfApprovalsRequired}} {{pendingApprovals .}} {{rejected .}} {{state .}}{{end}} | ||
` | ||
|
||
func pendingApprovals(at *v1alpha1.ApprovalTask) int { | ||
return at.Spec.NumberOfApprovalsRequired - len(at.Status.ApproversResponse) | ||
} | ||
|
||
func rejected(at *v1alpha1.ApprovalTask) int { | ||
count := 0 | ||
for _, approver := range at.Status.ApproversResponse { | ||
if approver.Response == "rejected" { | ||
count = count + 1 | ||
} | ||
} | ||
return count | ||
} | ||
|
||
func ColorStatus(status string) string { | ||
return color.New(ConditionColor[status]).Sprint(status) | ||
} | ||
|
||
func state(at *v1alpha1.ApprovalTask) string { | ||
var state string | ||
|
||
switch at.Status.State { | ||
case "approved": | ||
state = "Approved" | ||
case "rejected": | ||
state = "Rejected" | ||
case "pending": | ||
state = "Pending" | ||
} | ||
return ColorStatus(state) | ||
} | ||
|
||
func Root(p cli.Params) *cobra.Command { | ||
opts := &ListOptions{} | ||
funcMap := template.FuncMap{ | ||
"pendingApprovals": pendingApprovals, | ||
"state": state, | ||
"rejected": rejected, | ||
} | ||
c := &cobra.Command{ | ||
Use: "list", | ||
Short: "List all approval tasks", | ||
Long: `This command lists all the approval tasks.`, | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cs, err := p.Clients() | ||
if err != nil { | ||
fmt.Println("Error getting clients:", err) | ||
return | ||
} | ||
|
||
ns := p.Namespace() | ||
if opts.AllNamespaces { | ||
ns = "" | ||
} | ||
|
||
at, err := cs.ApprovalTask.ApprovalTasks(ns).List(context.TODO(), metav1.ListOptions{}) | ||
if err != nil { | ||
fmt.Println("Failed to list the approval tasks:", err) | ||
return | ||
} | ||
|
||
var data = struct { | ||
ApprovalTasks *v1alpha1.ApprovalTaskList | ||
}{ | ||
ApprovalTasks: at, | ||
} | ||
|
||
t, err := template.New("List Approvaltask").Funcs(funcMap).Parse(listTemplate) | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0) | ||
if err := t.Execute(w, data); err != nil { | ||
log.Fatal(err) | ||
} | ||
w.Flush() | ||
|
||
}, | ||
} | ||
|
||
c.Flags().BoolVarP(&opts.AllNamespaces, "all-namespaces", "A", opts.AllNamespaces, "list Tasks from all namespaces") | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
package cmd | ||
|
||
import ( | ||
cli "github.com/openshift-pipelines/manual-approval-gate/pkg/cli" | ||
"github.com/openshift-pipelines/manual-approval-gate/pkg/cli/cmd/list" | ||
"github.com/openshift-pipelines/manual-approval-gate/pkg/cli/flags" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Root() *cobra.Command { | ||
func Root(p cli.Params) *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "tkn-approvaltask", | ||
Short: "tkn approvaltask is a CLI tool for managing approvalTask", | ||
Long: `This application is a CLI tool to manage approvalTask`, | ||
Short: "Approval Task CLI", | ||
Long: `tkn plugin to use approval task as CLI`, | ||
Annotations: map[string]string{ | ||
"commandType": "main", | ||
}, | ||
PersistentPreRunE: flags.PersistentPreRunE(p), | ||
} | ||
|
||
c.AddCommand(list.Root()) | ||
c.AddCommand(list.Root(p)) | ||
|
||
flags.AddOptions(c) | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package flags | ||
|
||
import ( | ||
cli "github.com/openshift-pipelines/manual-approval-gate/pkg/cli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func AddOptions(cmd *cobra.Command) { | ||
cmd.PersistentFlags().StringP( | ||
"namespace", "n", "", | ||
"namespace to use (default: from $KUBECONFIG)") | ||
} | ||
|
||
func PersistentPreRunE(p cli.Params) func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, _ []string) error { | ||
return InitParams(p, cmd) | ||
} | ||
} | ||
|
||
func InitParams(p cli.Params, cmd *cobra.Command) error { | ||
ns, err := cmd.Flags().GetString("namespace") | ||
if err != nil { | ||
return err | ||
} | ||
if ns != "" { | ||
p.SetNamespace(ns) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.