Skip to content

Commit

Permalink
docs: add fizzbuzz example
Browse files Browse the repository at this point in the history
  • Loading branch information
synalice committed Jun 13, 2022
1 parent a0c9a25 commit 11d0216
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
90 changes: 90 additions & 0 deletions examples/fizzbuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"log"
"time"

"github.com/synalice/gobox/docker/config"
"github.com/synalice/gobox/docker/container"
"github.com/synalice/gobox/docker/controller"
"github.com/synalice/gobox/docker/file"
"github.com/synalice/gobox/docker/mount"
)

func main() {
ctrl, err := controller.NewController()
if err != nil {
log.Println(err)
}

myFile := file.File{
Name: "main.py",
Body: `
n = int(input())
if n % 3 == 0 and n % 5 == 0:
print("FizzBuzz")
elif n % 3 == 0:
print("Fizz")
elif n % 5 == 0:
print("Buzz")
else:
print(n)
`,
}

mount1, err := mount.NewMount(ctrl, "", "/theFolder1")
if err != nil {
log.Println(err)
}

configBuilder := config.NewConfigBuilder(ctrl)

configBuilder.
Image("python").
Cmd("python", "/theFolder1/main.py").
Mount(mount1).
TimeLimit(1 * time.Second).
MemoryLimit(64)

newConfig := configBuilder.Build()

containerBuilder := container.NewContainerBuilder(ctrl)

containerBuilder.
SetConfig(newConfig).
SetFile(myFile, mount1)

builtContainer, err := containerBuilder.Build()
if err != nil {
log.Println(err)
}

err = container.Start(ctrl, builtContainer, "15")
if err != nil {
log.Println(err)
}

_, err = container.Wait(ctrl, builtContainer.ID, builtContainer.TimeLimit)
if err != nil {
log.Println(err)
}

logs, err := container.GetLogs(ctrl, builtContainer)
if err != nil {
log.Println(err)
}

err = container.Remove(ctrl, builtContainer.ID)
if err != nil {
log.Println(err)
}

err = mount.Remove(ctrl, mount1)
if err != nil {
log.Println(err)
}

fmt.Println(logs)
}
6 changes: 2 additions & 4 deletions examples/container_creation.go → examples/usage_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,11 @@ print("Have a great day!")`,
// Let's wait until the container finishes its work.
//
// One of the errors you might actually want to expect from this
// function is container.ErrorTimeout. If you get it - it means that
// function is `container.ErrorTimeout`. If you get it - it means that
// the container hasn't finished it's work in the allotted time and was
// killed.
_, err = container.Wait(ctrl, builtContainer.ID, builtContainer.TimeLimit)
if err == container.ErrorTimeout {
log.Println(err)
} else if err != nil {
if err != nil {
log.Println(err)
}

Expand Down

0 comments on commit 11d0216

Please sign in to comment.