Documentation ¶
Overview ¶
Package fileargs implements a scanner that allows to read a FileArguments struct from an io.Reader.
The semantic of API is the same as bufio.Scanner.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FileArguments ¶
A FileArguments contains all informations read from an arguments file. The type is a Stringer.
Example ¶
This example creates a fileargs.FileArguments and show how it can be formatted into a string with String method.
package main import ( "fmt" "time" "github.com/parro-it/fileargs" ) func main() { args := fileargs.FileArguments{ CfgPath: "wrfda-runner.cfg", Periods: []*fileargs.Period{ {time.Date(2020, 11, 26, 0, 0, 0, 0, time.UTC), 24 * time.Hour}, {time.Date(2020, 11, 27, 0, 0, 0, 0, time.UTC), 48 * time.Hour}, }, } fmt.Println(args.String()) fmt.Println(args.Periods[1].String()) }
Output: wrfda-runner.cfg 2020112600 24 2020112700 48 2020112700 48
func ReadAll ¶
ReadAll is a convenience functions that creates a Scanner from reader source, read it to completion and returns a *FileArguments structure filled from the data read.
fsys is used to check for config file existence during scan.
Scanner error is returned if any happens.
Example ¶
This example show how to use fileargs.ReadAll to parse from an io.Reader instance
package main import ( "embed" "fmt" "io/fs" "github.com/parro-it/fileargs" ) //go:embed fixtures var fixtureRootFS embed.FS var fixtureFS, _ = fs.Sub(fixtureRootFS, "fixtures") func main() { f, err := fixtureFS.Open("dates.txt") if err != nil { panic(err) } args, err := fileargs.ReadAll(f, fixtureFS) if err != nil { panic(err) } fmt.Println(args.String()) }
Output: wrfda-runner.cfg 2020112600 24 2020112700 48
func ReadFile ¶
func ReadFile(fsys fs.FS, file string) (*FileArguments, error)
ReadFile reads arguments contained in file, and return a filled *FileArguments or an error if any happens. A givens fs.FS instance is used to read the file.
Example ¶
This example show how to use fileargs.ReadFile to parse from a file.
package main import ( "embed" "fmt" "io/fs" "github.com/parro-it/fileargs" ) //go:embed fixtures var fixtureRootFS embed.FS var fixtureFS, _ = fs.Sub(fixtureRootFS, "fixtures") func main() { args, err := fileargs.ReadFile(fixtureFS, "dates.txt") if err != nil { panic(err) } fmt.Println(args.String()) }
Output: wrfda-runner.cfg 2020112600 24 2020112700 48
func (FileArguments) String ¶
func (args FileArguments) String() string
String implements fmt.Stringer
type Period ¶
A Period represents a lapse of time with a Start instant and a Duration The type is a Stringer.
type Scanner ¶
Scanner provides a convenient interface for reading an arguments file. Successive calls to the Scan method will step through the lines of a file, skipping the bytes between the tokens. First line is parsed as a string to be used as config file path, other ones as Period.
Scanning stops unrecoverably at EOF, the first I/O error, or a malformed line. When a scan stops, the reader may have advanced arbitrarily far past the last token.
Example ¶
This example creates a fake bytes.Buffer containing a well formatted file args, and then manually creates a fileargs.Scanner to parse it.
package main import ( "bytes" "embed" "fmt" "io/fs" "github.com/parro-it/fileargs" ) //go:embed fixtures var fixtureRootFS embed.FS var fixtureFS, _ = fs.Sub(fixtureRootFS, "fixtures") func main() { var buf bytes.Buffer buf.WriteString("wrfda-runner.cfg\n") buf.WriteString("2020112600 24\n") buf.WriteString("2020112700 48\n") r := fileargs.New(&buf, fixtureFS) for r.Scan() { if cfg, ok := r.CfgPath(); ok { fmt.Println(cfg) continue } p, _ := r.Period() fmt.Println(p.String()) } if err := r.Err(); err != nil { panic(err) } }
Output: wrfda-runner.cfg 2020112600 24 2020112700 48
func New ¶
New returns a new Scanner initialized for the given source, that will use fsys to check for config file existence during scan.
func (*Scanner) CfgPath ¶
CfgPath returns the last value parsed if it was the config file path, otherwise it fails and return an empty string.
Second bool return value indicates if the returned string is valid.
func (*Scanner) Period ¶
Period returns the last value parsed if it was a period, otherwise it fails and return nil.
Second bool return value indicates if the returned period is valid.
func (*Scanner) Scan ¶
Scan advances the Scanner to the next line, parsed data will be available through the CfgPath or Period method. It returns false when the scan stops, either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil.