You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB
Go

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: %s\n", i+1, bucket.String())
}
return nil
}