Skip to content

Commit

Permalink
feat: added lint from stdin (#5095)
Browse files Browse the repository at this point in the history
Signed-off-by: roi.kramer <roi.kramer@codefresh.io>
Co-authored-by: roi.kramer <roi.kramer@codefresh.io>
  • Loading branch information
rkrmr33 and roi-codefresh committed Feb 19, 2021
1 parent 633da25 commit 377c5f8
Show file tree
Hide file tree
Showing 28 changed files with 1,322 additions and 613 deletions.
65 changes: 27 additions & 38 deletions cmd/argo/commands/clustertemplate/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,51 @@ package clustertemplate
import (
"fmt"
"os"
"path/filepath"

"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/cmd/argo/lint"
)

func NewLintCommand() *cobra.Command {
var strict bool
var (
strict bool
format string
)

command := &cobra.Command{
Use: "lint FILE...",
Short: "validate files or directories of cluster workflow template manifests",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
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.Msg())
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
}
85 changes: 29 additions & 56 deletions cmd/argo/commands/cron/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,51 @@ 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/cmd/argo/lint"
)

func NewLintCommand() *cobra.Command {
var strict bool
var (
strict bool
format string
)

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
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
ctx, apiClient := client.NewAPIClient()
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.Msg())
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
}
98 changes: 37 additions & 61 deletions cmd/argo/commands/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,60 @@ 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/cmd/argo/lint"
)

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

command := &cobra.Command{
Use: "lint FILE...",
Short: "validate files or directories of workflow manifests",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
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.Msg())
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
}

0 comments on commit 377c5f8

Please sign in to comment.