From: early Date: Thu, 7 Nov 2024 02:27:53 +0000 (-0700) Subject: revised api for include.File X-Git-Url: https://git.earlybird.gay/?a=commitdiff_plain;h=104b4573c17ba10c95dd8e6dbbc723474e87bfa8;p=today revised api for include.File --- diff --git a/include/include.go b/include/include.go index ac34fd4..101c3f8 100644 --- a/include/include.go +++ b/include/include.go @@ -71,17 +71,16 @@ func Abs(filename string, ignorePackages ...string) (string, error) { // If ignorePackages is nonempty, callers in any package specified are ignored. // If there's an error accessing the file, it will be returned when calling // Open() (Reader, error). -func File(filename string, ignorePackages ...string) FileOpener { +func File(filename string, ignorePackages ...string) (FileOpener, error) { opener := new(fileOpener) if path.IsAbs(filename) { opener.absPath = filename } else { - caller, err := getCallStackButt(ignorePackages) - if err != nil { - opener.alwaysErr = err - } else { + if caller, err := getCallStackButt(ignorePackages); err == nil { opener.absPath = path.Join(path.Dir(caller), filename) + } else { + return nil, err } } - return opener + return opener, nil } diff --git a/web/component/component.go b/web/component/component.go index 3847343..b163c33 100644 --- a/web/component/component.go +++ b/web/component/component.go @@ -10,8 +10,9 @@ type Component struct { name string fileName string source include.Opener - includes []compile.Source + + err error } type Config func(*Component) @@ -27,10 +28,16 @@ func New(name string, source string, optional ...func(*Component)) *Component { // Assign basic parameters p.name = name p.fileName = source - p.source = include.File(source, "git.earlybird.gay/today/web/component") + p.source, p.err = include.File(source, "git.earlybird.gay/today/web/component") + if p.err != nil { + return p + } // Run optional arguments for _, of := range optional { of(p) + if p.err != nil { + return p + } } return p } diff --git a/web/page/page.go b/web/page/page.go index be74b9e..72e9e8a 100644 --- a/web/page/page.go +++ b/web/page/page.go @@ -40,7 +40,7 @@ func Name(name string) Config { func Source(source string) Config { return func(p *Page) { - p.source = include.File(source, "git.earlybird.gay/today/web/part") + p.source, p.err = include.File(source, "git.earlybird.gay/today/web/part") } } @@ -70,11 +70,14 @@ func Pretty(indent string) Config { } } -func New(name string, source string, optional ...func(*Page)) *Page { +func New(name string, source string, optional ...Config) *Page { p := new(Page) // Assign basic parameters p.name = name - p.source = include.File(source, "git.earlybird.gay/today/web/page") + p.source, p.err = include.File(source, "git.earlybird.gay/today/web/page") + if p.err != nil { + return p + } p.onLoad = func(ctx context.Context, d render.Data) error { return nil } @@ -117,7 +120,7 @@ func Static(source string) *Page { } // With returns a shallow copy of p with all of optional applied to it. -func (p *Page) With(optional ...func(*Page)) *Page { +func (p *Page) With(optional ...Config) *Page { q := &Page{ name: p.name, source: p.source, diff --git a/web/part/part.go b/web/part/part.go index baa8c6c..ef4f253 100644 --- a/web/part/part.go +++ b/web/part/part.go @@ -18,6 +18,8 @@ type Part struct { includes []compile.Source onLoad render.OnLoadFunc templateFuncs template.FuncMap + + err error } type Config func(*Part) @@ -30,7 +32,7 @@ func Name(name string) Config { func Source(source string) Config { return func(p *Part) { - p.source = include.File(source, "git.earlybird.gay/today/web/part") + p.source, p.err = include.File(source, "git.earlybird.gay/today/web/part") } } @@ -64,11 +66,14 @@ func Tag(useTag bool) Config { } } -func New(name string, source string, optional ...func(*Part)) *Part { +func New(name string, source string, optional ...Config) *Part { p := new(Part) // Assign basic parameters p.name = name - p.source = include.File(source, "git.earlybird.gay/today/web/part") + p.source, p.err = include.File(source, "git.earlybird.gay/today/web/part") + if p.err != nil { + return p + } p.onLoad = func(ctx context.Context, data render.Data) error { return nil } @@ -76,12 +81,15 @@ func New(name string, source string, optional ...func(*Part)) *Part { // Run optional arguments for _, of := range optional { of(p) + if p.err != nil { + return p + } } return p } // With returns a shallow copy of p with all of optional applied to it. -func (p *Part) With(optional ...func(*Part)) *Part { +func (p *Part) With(optional ...Config) *Part { q := &Part{ name: p.name, source: p.source, @@ -93,6 +101,9 @@ func (p *Part) With(optional ...func(*Part)) *Part { for _, of := range optional { of(q) + if q.err != nil { + return q + } } return q } @@ -134,3 +145,7 @@ func (p *Part) OnLoad() render.OnLoadFunc { func (p *Part) Includes() []compile.Source { return p.includes } + +func (p *Part) Error() error { + return p.err +}