From 11d021605aa09fa505fb987710ebd651a0dc9743 Mon Sep 17 00:00:00 2001 From: Nikita Krasnov Date: Mon, 13 Jun 2022 18:27:48 +0300 Subject: [PATCH] docs: add fizzbuzz example --- examples/fizzbuzz.go | 90 +++++++++++++++++++ ...container_creation.go => usage_example.go} | 6 +- 2 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 examples/fizzbuzz.go rename examples/{container_creation.go => usage_example.go} (96%) diff --git a/examples/fizzbuzz.go b/examples/fizzbuzz.go new file mode 100644 index 0000000..ac8328e --- /dev/null +++ b/examples/fizzbuzz.go @@ -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) +} diff --git a/examples/container_creation.go b/examples/usage_example.go similarity index 96% rename from examples/container_creation.go rename to examples/usage_example.go index d7e5280..753b547 100644 --- a/examples/container_creation.go +++ b/examples/usage_example.go @@ -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) }