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 cli for cwl transpiler (Part 1/n for transpiler implementation) #7503

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cmd/cwl2argo/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package commands

import (
"github.com/argoproj/argo-workflows/v3/util/cmd"
"github.com/argoproj/pkg/cli"
"github.com/spf13/cobra"
)

const (
CLIName = "cwl2argo"
)

var (
logLevel string
logFormat string
)

func init() {
cobra.OnInitialize(initConfig)
}

func initConfig() {
cmd.SetLogFormatter(logFormat)
cli.SetLogLevel(logLevel)
}

func NewRootCommand() *cobra.Command {
command := cobra.Command{
Use: CLIName,
Short: "cwl2argo transpiles cwl to argo yaml",
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
command.AddCommand(NewTranspileCommand())
command.PersistentFlags().StringVar(&logLevel, "loglevel", "info", "Set the logging level. One of: debug|info|warn|error")
command.PersistentFlags().StringVar(&logFormat, "log-format", "text", "The formatter to use for logs. One of: text|json")

return &command
}
65 changes: 65 additions & 0 deletions cmd/cwl2argo/commands/transpile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package commands

import (
"errors"
"fmt"
"path/filepath"

"github.com/argoproj/argo-workflows/v3/cmd/cwl2argo/transpiler"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func lengthError() error {
return errors.New("length of filename should always be greater than the length of the extension")
}

func invalidFileError(inputFileExt string) error {
return fmt.Errorf("invalid file extension %s, only common workflow language (.cwl) files are allowed", inputFileExt)
}
func extractNoExtFileName(filename string, ext string) (string, error) {
if len(filename) <= len(ext) {
return "", lengthError()
}
name := filename[0 : len(filename)-len(ext)]
return name, nil
}

func processFile(inputFile string, inputsFile string) {

ext := filepath.Ext(inputFile)
if ext != ".cwl" {
log.Fatalf("%+v", invalidFileError(ext))
}
name, err := extractNoExtFileName(inputFile, ext)
if err != nil {
log.Fatalf("%+v", err)
}
newName := fmt.Sprintf("argo_%s.yaml", name)
log.Infof("Transpiling file %s with extension %s and ext free name %s to %s", inputFile, ext, name, newName)
err = transpiler.TranspileFile(inputFile, inputsFile, newName)
if err != nil {
log.Fatalf("%+v", err)
}

}

// Cobra command to transpile
func NewTranspileCommand() *cobra.Command {

command := cobra.Command{
Use: "transpile",
Short: "input common workflow language file",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("transpile only accepts two arguments <WORKFLOW.cwl> and <INPUTS.(yml|json|cwl)>")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
processFile(args[0], args[1])
},
}

return &command
}
25 changes: 25 additions & 0 deletions cmd/cwl2argo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"os"
"os/exec"

"github.com/argoproj/argo-workflows/v3/cmd/cwl2argo/commands"
)

func main() {

err := commands.NewRootCommand().Execute()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
if exitError.ExitCode() >= 0 {
os.Exit(exitError.ExitCode())
} else {
os.Exit(137) // probably SIGTERM or SIGKILL
}
} else {
println(err.Error())
os.Exit(64)
}
}
}
46 changes: 46 additions & 0 deletions cmd/cwl2argo/transpiler/transpiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package transpiler

import (
"errors"
"os"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

const (
CWLVersion = "v1.2"
)

func TranspileFile(inputFile string, inputsFile string, outputFile string) error {

log.Warn("Currently the transpiler expects preprocessed CWL input, cwlpack is the recommended way of preprocessing, use cwlpack from here https://github.com/rabix/sbpack/tree/84bd7867a0630a826280a702db715377aa879f6a")
var cwl map[string]interface{}
var inputs map[string]interface{}

def, err := os.ReadFile(inputFile)
if err != nil {
return err
}

err = yaml.Unmarshal(def, &cwl)
if err != nil {
return err
}

data, err := os.ReadFile(inputsFile)
if err != nil {
return err
}

err = yaml.Unmarshal(data, &inputs)
if err != nil {
return err
}

if _, ok := cwl["class"]; !ok {
return errors.New("<class> expected")
}

return nil
}