initial structure - reads cli arguments

master
Buddy Sandidge 5 years ago
parent c9ea33d4fa
commit af8e7ebedb

@ -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()
}

@ -0,0 +1,3 @@
module git.buddy.wtf/challenges/buckets-of-fun
go 1.13

@ -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
}

@ -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
}
Loading…
Cancel
Save