Skip to content

Commit

Permalink
refactor(cli): move common functions to util.go (argoproj#12839)
Browse files Browse the repository at this point in the history
Signed-off-by: shuangkun <[email protected]>
  • Loading branch information
shuangkun authored Mar 25, 2024
1 parent 2e5fb3b commit 842c613
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 196 deletions.
21 changes: 0 additions & 21 deletions cmd/argo/commands/clustertemplate/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import (
"log"
"os"

"github.com/argoproj/pkg/json"
"github.com/spf13/cobra"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
"github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)

type cliCreateOpts struct {
Expand Down Expand Up @@ -70,21 +67,3 @@ func createClusterWorkflowTemplates(ctx context.Context, filePaths []string, cli
printClusterWorkflowTemplate(created, cliOpts.output)
}
}

// unmarshalClusterWorkflowTemplates unmarshals the input bytes as either json or yaml
func unmarshalClusterWorkflowTemplates(wfBytes []byte, strict bool) ([]wfv1.ClusterWorkflowTemplate, error) {
var cwft wfv1.ClusterWorkflowTemplate
var jsonOpts []json.JSONOpt
if strict {
jsonOpts = append(jsonOpts, json.DisallowUnknownFields)
}
err := json.Unmarshal(wfBytes, &cwft, jsonOpts...)
if err == nil {
return []wfv1.ClusterWorkflowTemplate{cwft}, nil
}
yamlWfs, err := common.SplitClusterWorkflowTemplateYAMLFile(wfBytes, strict)
if err == nil {
return yamlWfs, nil
}
return nil, err
}
28 changes: 0 additions & 28 deletions cmd/argo/commands/clustertemplate/get.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package clustertemplate

import (
"encoding/json"
"fmt"
"log"

"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
clusterworkflowtmplpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)

func NewGetCommand() *cobra.Command {
Expand Down Expand Up @@ -41,26 +36,3 @@ func NewGetCommand() *cobra.Command {
command.Flags().StringVarP(&output, "output", "o", "", "Output format. One of: json|yaml|wide")
return command
}

func printClusterWorkflowTemplate(wf *wfv1.ClusterWorkflowTemplate, outFmt string) {
switch outFmt {
case "name":
fmt.Println(wf.ObjectMeta.Name)
case "json":
outBytes, _ := json.MarshalIndent(wf, "", " ")
fmt.Println(string(outBytes))
case "yaml":
outBytes, _ := yaml.Marshal(wf)
fmt.Print(string(outBytes))
case "wide", "":
printClusterWorkflowTemplateHelper(wf)
default:
log.Fatalf("Unknown output format: %s", outFmt)
}
}

func printClusterWorkflowTemplateHelper(wf *wfv1.ClusterWorkflowTemplate) {
const fmtStr = "%-20s %v\n"
fmt.Printf(fmtStr, "Name:", wf.ObjectMeta.Name)
fmt.Printf(fmtStr, "Created:", humanize.Timestamp(wf.ObjectMeta.CreationTimestamp.Time))
}
49 changes: 49 additions & 0 deletions cmd/argo/commands/clustertemplate/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package clustertemplate

import (
"encoding/json"
"fmt"
"log"

argoJson "github.com/argoproj/pkg/json"
"sigs.k8s.io/yaml"

"github.com/argoproj/pkg/humanize"

wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
"github.com/argoproj/argo-workflows/v3/workflow/util"
)

Expand All @@ -28,3 +36,44 @@ func generateClusterWorkflowTemplates(filePaths []string, strict bool) []wfv1.Cl

return clusterWorkflowTemplates
}

// unmarshalClusterWorkflowTemplates unmarshals the input bytes as either json or yaml
func unmarshalClusterWorkflowTemplates(wfBytes []byte, strict bool) ([]wfv1.ClusterWorkflowTemplate, error) {
var cwft wfv1.ClusterWorkflowTemplate
var jsonOpts []argoJson.JSONOpt
if strict {
jsonOpts = append(jsonOpts, argoJson.DisallowUnknownFields)
}
err := argoJson.Unmarshal(wfBytes, &cwft, jsonOpts...)
if err == nil {
return []wfv1.ClusterWorkflowTemplate{cwft}, nil
}
yamlWfs, err := common.SplitClusterWorkflowTemplateYAMLFile(wfBytes, strict)
if err == nil {
return yamlWfs, nil
}
return nil, err
}

func printClusterWorkflowTemplate(wf *wfv1.ClusterWorkflowTemplate, outFmt string) {
switch outFmt {
case "name":
fmt.Println(wf.ObjectMeta.Name)
case "json":
outBytes, _ := json.MarshalIndent(wf, "", " ")
fmt.Println(string(outBytes))
case "yaml":
outBytes, _ := yaml.Marshal(wf)
fmt.Print(string(outBytes))
case "wide", "":
printClusterWorkflowTemplateHelper(wf)
default:
log.Fatalf("Unknown output format: %s", outFmt)
}
}

func printClusterWorkflowTemplateHelper(wf *wfv1.ClusterWorkflowTemplate) {
const fmtStr = "%-20s %v\n"
fmt.Printf(fmtStr, "Name:", wf.ObjectMeta.Name)
fmt.Printf(fmtStr, "Created:", humanize.Timestamp(wf.ObjectMeta.CreationTimestamp.Time))
}
21 changes: 0 additions & 21 deletions cmd/argo/commands/cron/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"fmt"
"log"

"github.com/argoproj/pkg/json"
"github.com/spf13/cobra"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/workflow/common"
"github.com/argoproj/argo-workflows/v3/workflow/util"
)

Expand Down Expand Up @@ -88,22 +86,3 @@ func CreateCronWorkflows(ctx context.Context, filePaths []string, cliOpts *cliCr
fmt.Print(getCronWorkflowGet(created))
}
}

// unmarshalCronWorkflows unmarshals the input bytes as either json or yaml
func unmarshalCronWorkflows(wfBytes []byte, strict bool) []wfv1.CronWorkflow {
var cronWf wfv1.CronWorkflow
var jsonOpts []json.JSONOpt
if strict {
jsonOpts = append(jsonOpts, json.DisallowUnknownFields)
}
err := json.Unmarshal(wfBytes, &cronWf, jsonOpts...)
if err == nil {
return []wfv1.CronWorkflow{cronWf}
}
yamlWfs, err := common.SplitCronWorkflowYAMLFile(wfBytes, strict)
if err == nil {
return yamlWfs
}
log.Fatalf("Failed to parse cron workflow: %v", err)
return nil
}
73 changes: 0 additions & 73 deletions cmd/argo/commands/cron/get.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package cron

import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/argoproj/pkg/errors"
"github.com/argoproj/pkg/humanize"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
"github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)

func NewGetCommand() *cobra.Command {
Expand Down Expand Up @@ -48,69 +41,3 @@ func NewGetCommand() *cobra.Command {
command.Flags().StringVarP(&output, "output", "o", "", "Output format. One of: json|yaml|wide")
return command
}

func printCronWorkflow(wf *wfv1.CronWorkflow, outFmt string) {
switch outFmt {
case "name":
fmt.Println(wf.ObjectMeta.Name)
case "json":
outBytes, _ := json.MarshalIndent(wf, "", " ")
fmt.Println(string(outBytes))
case "yaml":
outBytes, _ := yaml.Marshal(wf)
fmt.Print(string(outBytes))
case "wide", "":
fmt.Print(getCronWorkflowGet(wf))
default:
log.Fatalf("Unknown output format: %s", outFmt)
}
}

func getCronWorkflowGet(cwf *wfv1.CronWorkflow) string {
const fmtStr = "%-30s %v\n"

out := ""
out += fmt.Sprintf(fmtStr, "Name:", cwf.ObjectMeta.Name)
out += fmt.Sprintf(fmtStr, "Namespace:", cwf.ObjectMeta.Namespace)
out += fmt.Sprintf(fmtStr, "Created:", humanize.Timestamp(cwf.ObjectMeta.CreationTimestamp.Time))
out += fmt.Sprintf(fmtStr, "Schedule:", cwf.Spec.GetScheduleString())
out += fmt.Sprintf(fmtStr, "Suspended:", cwf.Spec.Suspend)
if cwf.Spec.Timezone != "" {
out += fmt.Sprintf(fmtStr, "Timezone:", cwf.Spec.Timezone)
}
if cwf.Spec.StartingDeadlineSeconds != nil {
out += fmt.Sprintf(fmtStr, "StartingDeadlineSeconds:", *cwf.Spec.StartingDeadlineSeconds)
}
if cwf.Spec.ConcurrencyPolicy != "" {
out += fmt.Sprintf(fmtStr, "ConcurrencyPolicy:", cwf.Spec.ConcurrencyPolicy)
}
if cwf.Status.LastScheduledTime != nil {
out += fmt.Sprintf(fmtStr, "LastScheduledTime:", humanize.Timestamp(cwf.Status.LastScheduledTime.Time))
}

next, err := GetNextRuntime(cwf)
if err == nil {
out += fmt.Sprintf(fmtStr, "NextScheduledTime:", humanize.Timestamp(next)+" (assumes workflow-controller is in UTC)")
}

if len(cwf.Status.Active) > 0 {
var activeWfNames []string
for _, activeWf := range cwf.Status.Active {
activeWfNames = append(activeWfNames, activeWf.Name)
}
out += fmt.Sprintf(fmtStr, "Active Workflows:", strings.Join(activeWfNames, ", "))
}
if len(cwf.Status.Conditions) > 0 {
out += cwf.Status.Conditions.DisplayString(fmtStr, map[wfv1.ConditionType]string{wfv1.ConditionTypeSubmissionError: "✖"})
}
if len(cwf.Spec.WorkflowSpec.Arguments.Parameters) > 0 {
out += fmt.Sprintf(fmtStr, "Workflow Parameters:", "")
for _, param := range cwf.Spec.WorkflowSpec.Arguments.Parameters {
if !param.HasValue() {
continue
}
out += fmt.Sprintf(fmtStr, " "+param.Name+":", param.GetValue())
}
}
return out
}
97 changes: 95 additions & 2 deletions cmd/argo/commands/cron/util.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package cron

import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/argoproj/pkg/errors"
argoJson "github.com/argoproj/pkg/json"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/argoproj/argo-workflows/v3/workflow/common"
"github.com/argoproj/argo-workflows/v3/workflow/util"

"github.com/robfig/cron/v3"
"github.com/argoproj/pkg/errors"
"github.com/argoproj/pkg/humanize"

"github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)

// GetNextRuntime returns the next time the workflow should run in local time. It assumes the workflow-controller is in
Expand Down Expand Up @@ -64,3 +72,88 @@ func checkArgs(cmd *cobra.Command, args []string, parametersFile string, submitO
errors.CheckError(err)
}
}

// unmarshalCronWorkflows unmarshals the input bytes as either json or yaml
func unmarshalCronWorkflows(wfBytes []byte, strict bool) []wfv1.CronWorkflow {
var cronWf wfv1.CronWorkflow
var jsonOpts []argoJson.JSONOpt
if strict {
jsonOpts = append(jsonOpts, argoJson.DisallowUnknownFields)
}
err := argoJson.Unmarshal(wfBytes, &cronWf, jsonOpts...)
if err == nil {
return []wfv1.CronWorkflow{cronWf}
}
yamlWfs, err := common.SplitCronWorkflowYAMLFile(wfBytes, strict)
if err == nil {
return yamlWfs
}
log.Fatalf("Failed to parse cron workflow: %v", err)
return nil
}

func printCronWorkflow(wf *wfv1.CronWorkflow, outFmt string) {
switch outFmt {
case "name":
fmt.Println(wf.ObjectMeta.Name)
case "json":
outBytes, _ := json.MarshalIndent(wf, "", " ")
fmt.Println(string(outBytes))
case "yaml":
outBytes, _ := yaml.Marshal(wf)
fmt.Print(string(outBytes))
case "wide", "":
fmt.Print(getCronWorkflowGet(wf))
default:
log.Fatalf("Unknown output format: %s", outFmt)
}
}

func getCronWorkflowGet(cwf *wfv1.CronWorkflow) string {
const fmtStr = "%-30s %v\n"

out := ""
out += fmt.Sprintf(fmtStr, "Name:", cwf.ObjectMeta.Name)
out += fmt.Sprintf(fmtStr, "Namespace:", cwf.ObjectMeta.Namespace)
out += fmt.Sprintf(fmtStr, "Created:", humanize.Timestamp(cwf.ObjectMeta.CreationTimestamp.Time))
out += fmt.Sprintf(fmtStr, "Schedule:", cwf.Spec.GetScheduleString())
out += fmt.Sprintf(fmtStr, "Suspended:", cwf.Spec.Suspend)
if cwf.Spec.Timezone != "" {
out += fmt.Sprintf(fmtStr, "Timezone:", cwf.Spec.Timezone)
}
if cwf.Spec.StartingDeadlineSeconds != nil {
out += fmt.Sprintf(fmtStr, "StartingDeadlineSeconds:", *cwf.Spec.StartingDeadlineSeconds)
}
if cwf.Spec.ConcurrencyPolicy != "" {
out += fmt.Sprintf(fmtStr, "ConcurrencyPolicy:", cwf.Spec.ConcurrencyPolicy)
}
if cwf.Status.LastScheduledTime != nil {
out += fmt.Sprintf(fmtStr, "LastScheduledTime:", humanize.Timestamp(cwf.Status.LastScheduledTime.Time))
}

next, err := GetNextRuntime(cwf)
if err == nil {
out += fmt.Sprintf(fmtStr, "NextScheduledTime:", humanize.Timestamp(next)+" (assumes workflow-controller is in UTC)")
}

if len(cwf.Status.Active) > 0 {
var activeWfNames []string
for _, activeWf := range cwf.Status.Active {
activeWfNames = append(activeWfNames, activeWf.Name)
}
out += fmt.Sprintf(fmtStr, "Active Workflows:", strings.Join(activeWfNames, ", "))
}
if len(cwf.Status.Conditions) > 0 {
out += cwf.Status.Conditions.DisplayString(fmtStr, map[wfv1.ConditionType]string{wfv1.ConditionTypeSubmissionError: "✖"})
}
if len(cwf.Spec.WorkflowSpec.Arguments.Parameters) > 0 {
out += fmt.Sprintf(fmtStr, "Workflow Parameters:", "")
for _, param := range cwf.Spec.WorkflowSpec.Arguments.Parameters {
if !param.HasValue() {
continue
}
out += fmt.Sprintf(fmtStr, " "+param.Name+":", param.GetValue())
}
}
return out
}
File renamed without changes.
Loading

0 comments on commit 842c613

Please sign in to comment.