From af8e7ebedbbeb827da2c5e0d2d2dbe5240522bc1 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sun, 3 Nov 2019 14:50:02 -0800 Subject: [PATCH] initial structure - reads cli arguments --- cmd/buckets-of-fun/main.go | 25 +++++++++++++++++++ go.mod | 3 +++ lib/application/app.go | 51 ++++++++++++++++++++++++++++++++++++++ lib/bucket/bucket.go | 9 +++++++ 4 files changed, 88 insertions(+) create mode 100644 cmd/buckets-of-fun/main.go create mode 100644 go.mod create mode 100644 lib/application/app.go create mode 100644 lib/bucket/bucket.go diff --git a/cmd/buckets-of-fun/main.go b/cmd/buckets-of-fun/main.go new file mode 100644 index 0000000..4bf6dc2 --- /dev/null +++ b/cmd/buckets-of-fun/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "git.buddy.wtf/challenges/buckets-of-fun/lib/application" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func run() error { + flag.Parse() + app, err := application.New(flag.Args()...) + if err != nil { + return err + } + return app.Run() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f7e05fc --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.buddy.wtf/challenges/buckets-of-fun + +go 1.13 diff --git a/lib/application/app.go b/lib/application/app.go new file mode 100644 index 0000000..0197505 --- /dev/null +++ b/lib/application/app.go @@ -0,0 +1,51 @@ +package application + +import ( + "errors" + "fmt" + "strconv" + + "git.buddy.wtf/challenges/buckets-of-fun/lib/bucket" +) + +// App holds context for the application +type App struct { + Target uint64 + Buckets []bucket.Bucket +} + +// New returns an instance of the app +func New(args ...string) (*App, error) { + if len(args) < 2 { + msg := fmt.Sprintf("usage error: requires at least 2 arguments, received %d", len(args)) + return nil, errors.New(msg) + } + target, err := strconv.ParseUint(args[0], 10, 64) + if err != nil { + return nil, errors.New("target must be a positive integer: " + err.Error()) + } + + app := App{ + Target: target, + Buckets: []bucket.Bucket{}, + } + + for _, arg := range args[1:] { + capacity, err := strconv.ParseUint(arg, 10, 64) + if err != nil { + return nil, errors.New("bucket capacity must be a positive integer: " + err.Error()) + } + app.Buckets = append(app.Buckets, bucket.Bucket{Capacity: capacity}) + } + + return &app, nil +} + +// Run runs the application +func (a *App) Run() error { + fmt.Printf("target: %d\n", a.Target) + for i, bucket := range a.Buckets { + fmt.Printf("bucket %d: %d\n", i+1, bucket.Capacity) + } + return nil +} diff --git a/lib/bucket/bucket.go b/lib/bucket/bucket.go new file mode 100644 index 0000000..97e8975 --- /dev/null +++ b/lib/bucket/bucket.go @@ -0,0 +1,9 @@ +package bucket + +// Bucket to store volume +type Bucket struct { + // Capacity amount the bucket can store + Capacity uint64 + // Volume is the current volume of the bucket + Volume uint64 +}