diff options
Diffstat (limited to 'internal/url/addr.go')
| -rw-r--r-- | internal/url/addr.go | 77 |
1 files changed, 74 insertions, 3 deletions
diff --git a/internal/url/addr.go b/internal/url/addr.go index 555a596..e69d200 100644 --- a/internal/url/addr.go +++ b/internal/url/addr.go @@ -1,16 +1,63 @@ package url import ( + "encoding/json" "errors" + "fmt" "strings" + "time" ) type Websites_t []string type UserParams struct { - Websites Websites_t - Timeout int - Retry bool + Websites Websites_t `json:"websites"` + Timeout time.Duration `json:"timeout"` + Retry int8 `json:"retry"` +} + +type options struct { + websites Websites_t + timeout time.Duration + retry int8 +} + +func NewUserParams(opts ...func(*options)) *UserParams { + // will fail on default, but allows for just setting + // websites field without worrying about other opts + o := options{ + websites: nil, + timeout: 1 * time.Hour, + retry: 5, + } + + for _, opt := range opts { + opt(&o) + } + + return &UserParams{ + Websites: o.websites, + Timeout: o.timeout, + Retry: o.retry, + } +} + +func WithWebsites(w Websites_t) func(*options) { + return func(o *options) { + o.websites = w + } +} + +func WithTimeout(t time.Duration) func(*options) { + return func(o *options) { + o.timeout = t + } +} + +func WithRetry(r int8) func(*options) { + return func(o *options) { + o.retry = r + } } func (u *UserParams) NewWebsite(w string) (*Websites_t, error) { @@ -30,3 +77,27 @@ func (u *UserParams) GetWebsites() (*Websites_t, error) { return &(*u).Websites, nil } + +func (u *UserParams) Package() ([]byte, error) { + // turn struct values into json byte array for child proc + if u == nil { + return nil, errors.New("package: userparams is not initialized") + } else if (*u).Websites == nil { + return nil, errors.New("package: websites empty") + } + + packed, err := json.Marshal(u) + if err != nil { + return nil, err + } + + return packed, nil +} + +// TODO: deconstruct opts into a string for relay to chld proc cmd +func (u *UserParams) DeconstructOpts() (string, error) { + if (*u).Retry == 0 || (*u).Timeout == 0 || (*u).Websites == nil { + return "", errors.New("deconstructopts: one or more options are nil") + } + return fmt.Sprintf("websites=[],timeout=%v,retry=%d", (*u).Timeout, (*u).Retry), nil +} |
