Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(100DaysOfCode): ✨ go-r1-day-45 and docker improvements
- Loading branch information
1 parent
936f5bd
commit bef33d3
Showing
4 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
COUNTER=44 | ||
COUNTER=45 | ||
ROUND=1 | ||
LANGUAGE=go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
--- | ||
date: 2021-06-23T19:50:48-05:00 | ||
title: Go R1 Day 45 | ||
slug: go-r1-day-45 | ||
area: go | ||
round: 1 | ||
day_counter: 45 | ||
tags: | ||
- tech | ||
- development | ||
- 100DaysOfCode | ||
- golang | ||
- microblog | ||
images: [/images/r1-d45-main-calling-run.png] | ||
typora-root-url: ../../../static | ||
typora-copy-images-to: ../../../static/images | ||
--- | ||
|
||
## progress | ||
|
||
- Worked through merging flags and the "run" approach from Mat Ryer. | ||
- Used ff for parsing. | ||
|
||
The resulting logic seems correct with main being very simple with: | ||
|
||
```go | ||
package main | ||
import( | ||
"io" | ||
"flag" | ||
"os" | ||
"github.com/peterbourgon/ff/v3" | ||
"github.com/peterbourgon/ff/v3/ffcli" | ||
"github.com/peterbourgon/ff/v3/fftoml" | ||
) | ||
const ( | ||
// exitFail is the exit code if the program | ||
// fails. | ||
exitFail = 1 | ||
) | ||
|
||
// main configuration from Matt Ryer with minimal logic, passing to run, to allow easier CLI tests | ||
func main() { | ||
if err := run(os.Args, os.Stdout); err != nil { | ||
fmt.Fprintf(os.Stderr, "%s\n", err) | ||
os.Exit(exitFail) | ||
} | ||
} | ||
``` | ||
|
||
The run function then handles the actual parsing: | ||
|
||
```go | ||
func run(args []string, stdout io.Writer) error { | ||
if len(args) == 0 { | ||
return errors.New("no arguments") | ||
} | ||
flags := flag.NewFlagSet(args[0], flag.ExitOnError) | ||
flag.BoolVar(&debug, "debug", false, "sets log level to debug") | ||
|
||
if err := ff.Parse(flags, args, | ||
ff.WithEnvVarNoPrefix(), | ||
ff.WithConfigFileFlag("config"), | ||
ff.WithConfigFileParser(fftoml.Parser), | ||
); err != nil { | ||
return err | ||
} | ||
if debug { | ||
logLevel = "debug" | ||
} | ||
// proceed with initialization of logger and other tools | ||
return nil | ||
``` | ||
I like this approach, as the examples by Mat show how you can end up testing the inputs and variations on flags as well. | ||
The example from his blog post showed how easy it became with: | ||
```go | ||
err := run([]string{"program", "-v", "-debug=true", "-another=2"}) | ||
``` | ||
## links | ||
- [Why you shouldn't use func main in Go by Mat Ryer - PACE.](https://pace.dev/blog/2020/02/12/why-you-shouldnt-use-func-main-in-golang-by-mat-ryer.html) | ||
- [ff · pkg.go.dev](https://pkg.go.dev/github.com/peterbourgon/ff) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.