Skip to content

Commit

Permalink
feat(100DaysOfCode): ✨ go-r1-day-45 and docker improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sheldonhull committed Jun 24, 2021
1 parent 936f5bd commit bef33d3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .code-config
@@ -1,3 +1,3 @@
COUNTER=44
COUNTER=45
ROUND=1
LANGUAGE=go
28 changes: 28 additions & 0 deletions content/docs/docker.md
Expand Up @@ -40,6 +40,34 @@ docker build --pull --rm -f ".devcontainer/Dockerfile" -t codespace:latest ".dev

`gh repo clone microsoft/vscode-dev-containers` and then copy the `script-library` directory to `${ProjectDirectory}/.devcontainer/script-library`

## Syntax Tips

### Installing Apps

Use an environment variable to cleanup the dockerfile install commands.

```dockerfile
# Additonal applications to install
ENV ADDITIONAL_APPS \
curl \
g++ \
gcc \
gettext \
git \
locales \
locales-all \
make \
nano \
openssh-client \
rsync
RUN apt -yqq install ${ADDITIONAL_APPS} --no-install-recommends \
&& apt-get -yqq autoremove \
&& rm -rf /var/lib/apt/lists/*

```



## Best Practice

### Smaller Layers
Expand Down
85 changes: 85 additions & 0 deletions content/posts/2021/2021-06-23-go-R1-day-45.md
@@ -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)
Binary file added static/images/r1-d45-main-calling-run.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bef33d3

Please sign in to comment.