Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added lint from stdin #5095

Merged
merged 13 commits into from
Feb 19, 2021
59 changes: 20 additions & 39 deletions cmd/argo/commands/clustertemplate/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,46 @@ package clustertemplate
import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

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

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
"github.com/argoproj/argo-workflows/v3/pkg/apiclient/clusterworkflowtemplate"
"github.com/argoproj/argo-workflows/v3/workflow/validate"
"github.com/argoproj/argo-workflows/v3/workflow/lint"
)

func NewLintCommand() *cobra.Command {
var (
strict bool
format string
)
var command = &cobra.Command{
Use: "lint FILE...",
Short: "validate files or directories of cluster workflow template manifests",
Run: func(cmd *cobra.Command, args []string) {
ctx, apiClient := client.NewAPIClient()
serviceClient := apiClient.NewClusterWorkflowTemplateServiceClient()
fmtr, err := lint.GetFormatter(format)
errors.CheckError(err)

lint := func(file string) error {
cwfTmpls, err := validate.ParseCWfTmplFromFile(file, strict)
if err != nil {
return err
}
for _, cfwft := range cwfTmpls {
_, err := serviceClient.LintClusterWorkflowTemplate(ctx, &clusterworkflowtemplate.ClusterWorkflowTemplateLintRequest{Template: &cfwft})
if err != nil {
return err
}
}
fmt.Printf("%s is valid\n", file)
return nil
}
res, err := lint.Lint(ctx, &lint.LintOptions{
ServiceClients: lint.ServiceClients{
ClusterWorkflowTemplateClient: apiClient.NewClusterWorkflowTemplateServiceClient(),
},
Files: args,
Strict: strict,
DefaultNamespace: client.Namespace(),
Formatter: fmtr,
})
errors.CheckError(err)

for _, file := range args {
stat, err := os.Stat(file)
errors.CheckError(err)
if stat.IsDir() {
err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
fileExt := filepath.Ext(info.Name())
switch fileExt {
case ".yaml", ".yml", ".json":
default:
return nil
}
return lint(path)
})
errors.CheckError(err)
} else {
err := lint(file)
errors.CheckError(err)
}
fmt.Print(res)
if !res.Success {
os.Exit(1)
}
fmt.Printf("Cluster Workflow Template manifests validated\n")
},
}

command.Flags().StringVar(&format, "format", "pretty", "Linting results output format. One of: pretty|simple")
command.Flags().BoolVar(&strict, "strict", true, "perform strict workflow validation")
return command
}
76 changes: 21 additions & 55 deletions cmd/argo/commands/cron/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,46 @@ package cron
import (
"fmt"
"os"
"path/filepath"

"github.com/argoproj/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
cronworkflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/cronworkflow"
"github.com/argoproj/argo-workflows/v3/workflow/validate"
"github.com/argoproj/argo-workflows/v3/workflow/lint"
)

func NewLintCommand() *cobra.Command {
var (
strict bool
format string
)
var command = &cobra.Command{
Use: "lint FILE...",
Short: "validate files or directories of cron workflow manifests",
Run: func(cmd *cobra.Command, args []string) {
ctx, apiClient := client.NewAPIClient()
serviceClient := apiClient.NewCronWorkflowServiceClient()
lint := func(file string) error {
wfs, err := validate.ParseCronWorkflowsFromFile(file, strict)
if err != nil {
return err
}
if len(wfs) == 0 {
return fmt.Errorf("there was nothing to validate")
}
for _, wf := range wfs {
if wf.Namespace == "" {
wf.Namespace = client.Namespace()
}
_, err := serviceClient.LintCronWorkflow(ctx, &cronworkflowpkg.LintCronWorkflowRequest{Namespace: wf.Namespace, CronWorkflow: &wf})
if err != nil {
return err
}
}
fmt.Printf("%s is valid\n", file)
return nil
}
fmtr, err := lint.GetFormatter(format)
errors.CheckError(err)

invalid := false
for _, file := range args {
stat, err := os.Stat(file)
errors.CheckError(err)
if stat.IsDir() {
err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
fileExt := filepath.Ext(info.Name())
switch fileExt {
case ".yaml", ".yml", ".json":
default:
return nil
}
if err := lint(path); err != nil {
log.Errorf("Error in file %s: %s", path, err)
invalid = true
}
return nil
})
errors.CheckError(err)
} else {
if err := lint(file); err != nil {
log.Errorf("Error in file %s: %s", file, err)
invalid = true
}
}
}
if invalid {
log.Fatalf("Errors encountered in validation")
res, err := lint.Lint(ctx, &lint.LintOptions{
ServiceClients: lint.ServiceClients{
CronWorkflowsClient: apiClient.NewCronWorkflowServiceClient(),
},
Files: args,
Strict: strict,
DefaultNamespace: client.Namespace(),
Formatter: fmtr,
})
errors.CheckError(err)

fmt.Print(res)
if !res.Success {
os.Exit(1)
}
fmt.Printf("Cron workflow manifests validated\n")
},
}
command.Flags().BoolVar(&strict, "strict", true, "perform strict workflow validation")

command.Flags().StringVar(&format, "format", "pretty", "Linting results output format. One of: pretty|simple")
command.Flags().BoolVar(&strict, "strict", true, "perform strict validation")
return command
}
94 changes: 32 additions & 62 deletions cmd/argo/commands/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,56 @@ package commands
import (
"fmt"
"os"
"path/filepath"

"github.com/argoproj/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/argoproj/argo-workflows/v3/cmd/argo/commands/client"
workflowpkg "github.com/argoproj/argo-workflows/v3/pkg/apiclient/workflow"
"github.com/argoproj/argo-workflows/v3/workflow/validate"
"github.com/argoproj/argo-workflows/v3/workflow/lint"
)

func NewLintCommand() *cobra.Command {
var (
strict bool
strict bool
allKinds bool
format string
)
var command = &cobra.Command{

command := &cobra.Command{
Use: "lint FILE...",
Short: "validate files or directories of workflow manifests",
Run: func(cmd *cobra.Command, args []string) {
ctx, apiClient := client.NewAPIClient()
serviceClient := apiClient.NewWorkflowServiceClient()

lint := func(file string) error {
wfs, err := validate.ParseWfFromFile(file, strict)
if err != nil {
return err
}
if len(wfs) == 0 {
return fmt.Errorf("there was nothing to validate")
}
for _, wf := range wfs {
if wf.Namespace == "" {
wf.Namespace = client.Namespace()
}
_, err := serviceClient.LintWorkflow(ctx, &workflowpkg.WorkflowLintRequest{Namespace: wf.Namespace, Workflow: &wf})
if err != nil {
return err
}
}
fmt.Printf("%s is valid\n", file)
return nil
clients := lint.ServiceClients{
WorkflowsClient: apiClient.NewWorkflowServiceClient(),
}

invalid := false
for _, file := range args {
stat, err := os.Stat(file)
errors.CheckError(err)
if stat.IsDir() {
_ = filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
// If there was an error with the walk, return
if err != nil {
return err
}

fileExt := filepath.Ext(info.Name())
switch fileExt {
case ".yaml", ".yml", ".json":
default:
return nil
}

if err := lint(path); err != nil {
log.Errorf("Error in file %s: %s", path, err)
invalid = true
}
return nil
})
} else {
if err := lint(file); err != nil {
log.Errorf("Error in file %s: %s", file, err)
invalid = true
}
}
if allKinds {
clients.WorkflowTemplatesClient = apiClient.NewWorkflowTemplateServiceClient()
clients.CronWorkflowsClient = apiClient.NewCronWorkflowServiceClient()
clients.ClusterWorkflowTemplateClient = apiClient.NewClusterWorkflowTemplateServiceClient()
}
if invalid {
log.Fatalf("Errors encountered in validation")
fmtr, err := lint.GetFormatter(format)
errors.CheckError(err)

res, err := lint.Lint(ctx, &lint.LintOptions{
ServiceClients: clients,
Files: args,
Strict: strict,
DefaultNamespace: client.Namespace(),
Formatter: fmtr,
})
errors.CheckError(err)

fmt.Print(res)
if !res.Success {
os.Exit(1)
}
fmt.Printf("Workflow manifests validated\n")
},
}
command.Flags().BoolVar(&strict, "strict", true, "perform strict workflow validation")

command.Flags().BoolVar(&allKinds, "all-kinds", false, "Lint all kinds, not just workflows")
command.Flags().StringVar(&format, "format", "pretty", "Linting results output format. One of: pretty|simple")
command.Flags().BoolVar(&strict, "strict", true, "Perform strict workflow validation")

return command
}