diff --git a/cmd/argo/commands/submit.go b/cmd/argo/commands/submit.go index 2017d005ea9b..acb8782dfdff 100644 --- a/cmd/argo/commands/submit.go +++ b/cmd/argo/commands/submit.go @@ -78,20 +78,11 @@ func SubmitWorkflows(filePaths []string, submitOpts *util.SubmitOpts, cliOpts *c } defaultWFClient := InitWorkflowClient() - var fileContents [][]byte - if len(filePaths) == 1 && filePaths[0] == "-" { - body, err := util.ReadFromStdin() - if err != nil { - log.Fatal(err) - } - fileContents = append(fileContents, body) - } else { - var err error - fileContents, err = util.ReadFromFilePathsOrUrls(filePaths...) - if err != nil { - log.Fatal(err) - } + fileContents, err := util.ReadManifest(filePaths...) + if err != nil { + log.Fatal(err) } + var workflows []wfv1.Workflow for _, body := range fileContents { wfs := unmarshalWorkflows(body, cliOpts.strict) diff --git a/cmd/argo/commands/template/create.go b/cmd/argo/commands/template/create.go index affe891c59d2..d28befd999f5 100644 --- a/cmd/argo/commands/template/create.go +++ b/cmd/argo/commands/template/create.go @@ -45,19 +45,9 @@ func CreateWorkflowTemplates(filePaths []string, cliOpts *cliCreateOpts) { } defaultWFTmplClient := InitWorkflowTemplateClient() - var fileContents [][]byte - if len(filePaths) == 1 && filePaths[0] == "-" { - body, err := util.ReadFromStdin() - if err != nil { - log.Fatal(err) - } - fileContents = append(fileContents, body) - } else { - var err error - fileContents, err = util.ReadFromFilePathsOrUrls(filePaths...) - if err != nil { - log.Fatal(err) - } + fileContents, err := util.ReadManifest(filePaths...) + if err != nil { + log.Fatal(err) } var workflowTemplates []wfv1.WorkflowTemplate diff --git a/workflow/util/util.go b/workflow/util/util.go index 459e02f9b8b6..395c06ca1fa8 100644 --- a/workflow/util/util.go +++ b/workflow/util/util.go @@ -614,7 +614,7 @@ func ReadFromUrl(url string) ([]byte, error) { // ReadFromFilePathsOrUrls reads the content of a single or a list of file paths and/or urls func ReadFromFilePathsOrUrls(filePathsOrUrls ...string) ([][]byte, error) { - var contents [][]byte + var fileContents [][]byte var body []byte var err error for _, filePathOrUrl := range filePathsOrUrls { @@ -629,7 +629,27 @@ func ReadFromFilePathsOrUrls(filePathsOrUrls ...string) ([][]byte, error) { return [][]byte{}, err } } - contents = append(contents, body) + fileContents = append(fileContents, body) } - return contents, err + return fileContents, err +} + +// ReadManifest reads from stdin, a single file/url, or a list of files and/or urls +func ReadManifest(manifestPaths ...string) ([][]byte, error) { + var manifestContents [][]byte + var err error + if len(manifestPaths) == 1 && manifestPaths[0] == "-" { + body, err := ReadFromStdin() + if err != nil { + return [][]byte{}, err + } + manifestContents = append(manifestContents, body) + } else { + var err error + manifestContents, err = ReadFromFilePathsOrUrls(manifestPaths...) + if err != nil { + return [][]byte{}, err + } + } + return manifestContents, err }