Skip to content

Commit

Permalink
Add simple converter (#3)
Browse files Browse the repository at this point in the history
* feat: add simple converter from new and helper functions

Problem: we should be able to create a new jobspec easily,
and provide helper functions to read/serialize and validate.
Solution: do that.

Signed-off-by: vsoch <[email protected]>
  • Loading branch information
vsoch authored Mar 2, 2024
1 parent 0073278 commit e7fb2bf
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 84 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ COMMONENVVAR=GOOS=$(shell uname -s | tr A-Z a-z)
RELEASE_VERSION?=v$(shell date +%Y%m%d)-$(shell git describe --tags --match "v*")

.PHONY: all
all: example1 example2 example3 example4 example5 example6
all: example1 example2 example3 example4 example5 example6 createnew

.PHONY: build
build:
go mod tidy
mkdir -p ./examples/v1/bin

# Build examples
.PHONY: createnew
createnew: build
$(COMMONENVVAR) $(BUILDENVVAR) go build -ldflags '-w' -o ./examples/v1/bin/createnew examples/v1/createnew/example.go

.PHONY: example1
example1: build
$(COMMONENVVAR) $(BUILDENVVAR) go build -ldflags '-w' -o ./examples/v1/bin/example1 examples/v1/example1/example.go
Expand Down Expand Up @@ -43,6 +47,7 @@ test:
./examples/v1/bin/example4
./examples/v1/bin/example5
./examples/v1/bin/example6
./examples/v1/bin/createnew

.PHONY: clean
clean:
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ Run all tests at once:
make test
```

Here is an example of usage. Note that this isn't a full program, but is intended to show helper functions.
In this small program, we load in a jobspec (from yaml) and then validate and serialize to each of json and yaml.

```go
package main

import (
"fmt"
"os"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

func main() {

// Example 1: reading from file
yamlFile := "examples/v1/example1/jobspec.yaml"

// This is how to read from a YAML file
js, err := v1.LoadJobspecYaml(yamlFile)
// Validate the jobspec
valid, err := js.Validate()

// Convert back to YAML (print out as string(out))
out, err := js.JobspecToYaml()

// Convert back into JSON (also print string(out))
out, err = js.JobspecToJson()

// Example 2: creating from scratch
var nodes int32 = 2
var tasks int32 = 12
js, err := v1.NewSimpleJobspec("myjobspec", "echo hello world", nodes, tasks)
// proceed with equivalent functions above!
}
```

For full examples, see the [examples](examples/v1) directory.

### Version 1

You can run any example (and view the code) to see how it works!
Expand Down
45 changes: 45 additions & 0 deletions examples/v1/createnew/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"os"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

func main() {
fmt.Println("This example creates a new Jobspec")

var nodes int32 = 2
var tasks int32 = 12
js, err := v1.NewSimpleJobspec("myjobspec", "echo hello world", nodes, tasks)
if err != nil {
fmt.Printf("error creating jobspec: %s\n", err)
os.Exit(1)
}

// Validate the jobspec
valid, err := js.Validate()
if !valid || err != nil {
fmt.Printf("schema is not valid:%s\n", err)
os.Exit(1)
} else {
fmt.Println("schema is valid")
}
fmt.Println(js)

out, err := js.JobspecToYaml()
if err != nil {
fmt.Printf("error marshalling jobspec: %s\n", err)
os.Exit(1)
}
fmt.Println(string(out))

// One example of json
out, err = js.JobspecToJson()
if err != nil {
fmt.Printf("error marshalling jobspec: %s\n", err)
os.Exit(1)
}
fmt.Println(string(out))
}
19 changes: 8 additions & 11 deletions examples/v1/example1/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"fmt"
"os"

"sigs.k8s.io/yaml"

"github.com/compspec/jobspec-go/pkg/schema"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

Expand All @@ -24,30 +20,31 @@ func main() {
flag.Usage()
os.Exit(0)
}
file, err := os.ReadFile(yamlFile)
js, err := v1.LoadJobspecYaml(yamlFile)
if err != nil {
fmt.Printf("error reading %s:%s\n", yamlFile, err)
os.Exit(1)
}

// Validate the jobspec
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
valid, err := js.Validate()
if !valid || err != nil {
fmt.Printf("schema is not valid:%s\n", err)
os.Exit(1)
} else {
fmt.Println("schema is valid")
}
fmt.Println(js)

js := v1.Jobspec{}
err = yaml.Unmarshal([]byte(file), &js)
out, err := js.JobspecToYaml()
if err != nil {
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(js)
fmt.Println(string(out))

out, err := yaml.Marshal(js)
// One example of json
out, err = js.JobspecToJson()
if err != nil {
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
os.Exit(1)
Expand Down
17 changes: 3 additions & 14 deletions examples/v1/example2/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"fmt"
"os"

"sigs.k8s.io/yaml"

"github.com/compspec/jobspec-go/pkg/schema"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

Expand All @@ -24,30 +20,23 @@ func main() {
flag.Usage()
os.Exit(0)
}
file, err := os.ReadFile(yamlFile)
js, err := v1.LoadJobspecYaml(yamlFile)
if err != nil {
fmt.Printf("error reading %s:%s\n", yamlFile, err)
os.Exit(1)
}

// Validate the jobspec
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
valid, err := js.Validate()
if !valid || err != nil {
fmt.Printf("schema is not valid:%s\n", err)
os.Exit(1)
} else {
fmt.Println("schema is valid")
}

js := v1.Jobspec{}
err = yaml.Unmarshal([]byte(file), &js)
if err != nil {
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(js)

out, err := yaml.Marshal(js)
out, err := js.JobspecToYaml()
if err != nil {
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
os.Exit(1)
Expand Down
18 changes: 4 additions & 14 deletions examples/v1/example3/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"fmt"
"os"

"sigs.k8s.io/yaml"

"github.com/compspec/jobspec-go/pkg/schema"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

Expand All @@ -24,33 +20,27 @@ func main() {
flag.Usage()
os.Exit(0)
}
file, err := os.ReadFile(yamlFile)
js, err := v1.LoadJobspecYaml(yamlFile)
if err != nil {
fmt.Printf("error reading %s:%s\n", yamlFile, err)
os.Exit(1)
}

// Validate the jobspec
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
valid, err := js.Validate()
if !valid || err != nil {
fmt.Printf("schema is not valid:%s\n", err)
os.Exit(1)
} else {
fmt.Println("schema is valid")
}

js := v1.Jobspec{}
err = yaml.Unmarshal([]byte(file), &js)
if err != nil {
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(js)

out, err := yaml.Marshal(js)
out, err := js.JobspecToYaml()
if err != nil {
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(string(out))

}
19 changes: 4 additions & 15 deletions examples/v1/example4/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"fmt"
"os"

"sigs.k8s.io/yaml"

"github.com/compspec/jobspec-go/pkg/schema"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

Expand All @@ -24,34 +20,27 @@ func main() {
flag.Usage()
os.Exit(0)
}
file, err := os.ReadFile(yamlFile)
js, err := v1.LoadJobspecYaml(yamlFile)
if err != nil {
fmt.Printf("error reading %s:%s\n", yamlFile, err)
os.Exit(1)
}

// Validate the jobspec
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
valid, err := js.Validate()
if !valid || err != nil {
fmt.Printf("schema is not valid:%s\n", err)
os.Exit(1)
} else {
fmt.Println("schema is valid")
}

js := v1.Jobspec{}
err = yaml.Unmarshal([]byte(file), &js)
if err != nil {
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(js)

out, err := yaml.Marshal(js)

out, err := js.JobspecToYaml()
if err != nil {
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(string(out))

}
18 changes: 4 additions & 14 deletions examples/v1/example5/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import (
"fmt"
"os"

"sigs.k8s.io/yaml"

"github.com/compspec/jobspec-go/pkg/schema"

v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
)

Expand All @@ -24,33 +20,27 @@ func main() {
flag.Usage()
os.Exit(0)
}
file, err := os.ReadFile(yamlFile)
js, err := v1.LoadJobspecYaml(yamlFile)
if err != nil {
fmt.Printf("error reading %s:%s\n", yamlFile, err)
os.Exit(1)
}

// Validate the jobspec
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
valid, err := js.Validate()
if !valid || err != nil {
fmt.Printf("schema is not valid:%s\n", err)
os.Exit(1)
} else {
fmt.Println("schema is valid")
}

js := v1.Jobspec{}
err = yaml.Unmarshal([]byte(file), &js)
if err != nil {
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(js)

out, err := yaml.Marshal(js)
out, err := js.JobspecToYaml()
if err != nil {
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
os.Exit(1)
}
fmt.Println(string(out))

}
Loading

0 comments on commit e7fb2bf

Please sign in to comment.