github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/url/urlutil/api.go (about)

     1  package urlutil
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  )
    10  
    11  // Open will open the URL given by url. A io.ReadCloser is returned, which must
    12  // be closed.
    13  func Open(url string) (io.ReadCloser, error) {
    14  	return open(url)
    15  }
    16  
    17  type CachedReadCloser struct {
    18  	cache         *os.File
    19  	cacheFilename string
    20  	rawReadCloser io.ReadCloser
    21  }
    22  
    23  func (rc *CachedReadCloser) Close() error {
    24  	return rc.close()
    25  }
    26  
    27  func (rc *CachedReadCloser) Read(p []byte) (int, error) {
    28  	return rc.read(p)
    29  }
    30  
    31  func (rc *CachedReadCloser) SaveCache() error {
    32  	return rc.saveCache()
    33  }
    34  
    35  // WatchUrl watches the URL given by url and yields a new io.ReadCloser
    36  // periodically. If the URL is a local file, a new io.ReadCloser is yielded
    37  // when a new inode is found and it is a regular file. If url is a HTTP/HTTPS
    38  // URL a new io.ReadCloser is yielded every checkInterval.
    39  // Each yielded io.ReadCloser must be closed after use.
    40  // Any errors are logged to the logger.
    41  func WatchUrl(url string, checkInterval time.Duration,
    42  	logger log.Logger) (<-chan io.ReadCloser, error) {
    43  	return watchUrl(url, checkInterval, logger)
    44  }
    45  
    46  // WatchUrlWithCache is similar to WatchUrl, except that data are cached.
    47  // A cached copy of the data is stored in the file named cacheFilename when the
    48  // SaveCache method is called. This file is read at startup if the URL is not
    49  // available before the initialTimeout.
    50  // Any errors are logged to the logger.
    51  func WatchUrlWithCache(url string, checkInterval time.Duration,
    52  	cacheFilename string, initialTimeout time.Duration,
    53  	logger log.Logger) (<-chan *CachedReadCloser, error) {
    54  	return watchUrlWithCache(url, checkInterval, cacheFilename, initialTimeout,
    55  		logger)
    56  }