Skip to content

Commit 91f1bc7

Browse files
authored
Merge pull request #1 from SVilgelm/the-code
The Code
2 parents 0402a05 + 376e974 commit 91f1bc7

12 files changed

+129
-252
lines changed

.github/Brewfile

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
tap "golangci/tap"
2-
tap "vektra/tap"
32
tap "kyoh86/tap"
43
tap "sonatype-nexus-community/nancy-tap"
54
tap "sv-tools/apps"
@@ -13,7 +12,5 @@ brew "kyoh86/tap/richgo"
1312
brew "sonatype-nexus-community/nancy-tap/nancy"
1413
# bumptag is a tool to increment a version and to create a git tag with an annotation.
1514
brew "sv-tools/apps/bumptag"
16-
# A mock code autogenerator for Go
17-
brew "vektra/tap/mockery"
1815
# Deliver Go binaries as fast and easily as possible
1916
brew "goreleaser/tap/goreleaser"

.github/Dockerfile

-3
This file was deleted.

.github/goreleaser-cli.yml

-146
This file was deleted.

.github/goreleaser-lib.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
project_name: go-repo-template
1+
project_name: conf-reader-flags
22

33
release:
44
github:
55
owner: sv-tools
6-
name: go-repo-template
6+
name: conf-reader-flags
77

88
builds:
99
- skip: true

.github/workflows/checks.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
- cron: "0 0 * * 0"
1212

1313
env:
14-
GO: 1.16
14+
GO: 1.17
1515

1616
jobs:
1717
CodeQL:

.github/workflows/release.yaml

+1-57
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
- "v[0-9]+.[0-9]+.[0-9]+"
99

1010
env:
11-
GO: 1.16
11+
GO: 1.17
1212

1313
jobs:
1414
bump-version:
@@ -45,62 +45,6 @@ jobs:
4545
token: "${{ secrets.GITHUB_TOKEN }}"
4646
title: Next
4747

48-
release-cli:
49-
if: github.event_name == 'push'
50-
runs-on: ubuntu-latest
51-
env:
52-
DOCKER_CLI_EXPERIMENTAL: "enabled"
53-
steps:
54-
- name: Checkout code
55-
uses: actions/checkout@v2
56-
57-
- name: Install Go
58-
uses: actions/setup-go@v2
59-
with:
60-
go-version: ${{ env.GO }}
61-
62-
- name: Prepare gpg
63-
run: |
64-
gpgconf --kill gpg-agent
65-
gpg --quiet --batch --yes --decrypt --passphrase="${{ secrets.GPG_PASSPHRASE }}" --output .github/sv-tools-bot.asc .github/sv-tools-bot.asc.gpg
66-
gpg --batch --yes --import .github/sv-tools-bot.asc
67-
rm .github/sv-tools-bot.asc
68-
69-
- name: Allow arm Docker builds # https://github.com/linuxkit/linuxkit/tree/master/pkg/binfmt
70-
run: sudo docker run --privileged linuxkit/binfmt:v0.8
71-
72-
- name: Docker Login
73-
uses: docker/login-action@v1
74-
with:
75-
registry: ghcr.io
76-
username: sv-tools-bot
77-
password: ${{ secrets.BOT_TOKEN }}
78-
79-
- name: Docker Login
80-
uses: docker/login-action@v1
81-
with:
82-
username: svtools
83-
password: ${{ secrets.DOCKER_HUB_TOKEN }}
84-
85-
- name: Snapcraft Login
86-
env:
87-
SNAPCRAFT_LOGIN: ${{ secrets.SNAPCRAFT_LOGIN }}
88-
run: |
89-
sudo apt-get -yq --no-install-suggests --no-install-recommends install snapcraft
90-
snapcraft login --with <(echo "$SNAPCRAFT_LOGIN")
91-
92-
- name: Run GoReleaser
93-
uses: goreleaser/goreleaser-action@v2
94-
with:
95-
version: latest
96-
args: release --rm-dist --config .github/goreleaser-cli.yml
97-
env:
98-
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
99-
100-
- name: Clear
101-
if: always()
102-
run: rm -f ${HOME}/.docker/config.json
103-
10448
release-lib:
10549
if: github.event_name == 'push'
10650
runs-on: ubuntu-latest

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ linters-settings:
4545
disable:
4646
- shadow
4747
gci:
48-
local-prefixes: github.com/sv-tools/go-repo-template
48+
local-prefixes: github.com/sv-tools/conf-reader-flags
4949
gocognit:
5050
min-complexity: 15
5151
gocyclo:

README.md

+6-37
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
1-
# go-repo-template
1+
# conf-reader-flags
22

3-
[![Code Analysis](https://github.com/sv-tools/go-repo-template/actions/workflows/checks.yaml/badge.svg)](https://github.com/sv-tools/go-repo-template/actions/workflows/checks.yaml)
4-
[![Go Reference](https://pkg.go.dev/badge/github.com/sv-tools/go-repo-template.svg)](https://pkg.go.dev/github.com/sv-tools/go-repo-template)
5-
[![codecov](https://codecov.io/gh/sv-tools/go-repo-template/branch/main/graph/badge.svg?token=0XVOTDR1CW)](https://codecov.io/gh/sv-tools/go-repo-template)
6-
[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/sv-tools/go-repo-template?style=flat)](https://github.com/sv-tools/go-repo-template/releases)
7-
8-
The template for new go repositories
9-
10-
## Features
11-
12-
1. `Makefile` to run some basic validations:
13-
1. `golangci-lint`
14-
2. `nancy`
15-
3. unit and benchmarks tests
16-
4. installing all needed tools on macOS using `brew`
17-
5. cleaning up the `go.sum` file by removing it and re-creating by `go mod tidy`
18-
2. MIT License by default
19-
3. Github Action workflows:
20-
1. testing all pull requests by running same tools and checking code coverage using `codecov` action
21-
2. making a new release, triggered by closed milestone
22-
1. creates a new tag using `bumptag` tool
23-
2. creates new `Next` milestone
24-
3. runs `goreleaser` to build a new release
25-
26-
## Usage
27-
28-
1. Create a repository using this repo as template
29-
2. Replace in all files `go-repo-template` to the project's name
30-
3. In case of:
31-
1. library
32-
1. Remove `.github/Dockerfile`, `.github/goreleaser-cli.yml` files
33-
2. Remove `release-cli` section in the `.github/workflows/release.yaml` file
34-
2. command line tool (cli)
35-
1. Remove `.github/goreleaser-lib.yml` file
36-
2. Remove `release-lib` section in the `.github/workflows/release.yaml` file
37-
4. Modify `README.md` by removing this text
38-
5. Feel free to modify any other files
3+
[![Code Analysis](https://github.com/sv-tools/conf-reader-flags/actions/workflows/checks.yaml/badge.svg)](https://github.com/sv-tools/conf-reader-flags/actions/workflows/checks.yaml)
4+
[![Go Reference](https://pkg.go.dev/badge/github.com/sv-tools/conf-reader-flags.svg)](https://pkg.go.dev/github.com/sv-tools/conf-reader-flags)
5+
[![codecov](https://codecov.io/gh/sv-tools/conf-reader-flags/branch/main/graph/badge.svg?token=0XVOTDR1CW)](https://codecov.io/gh/sv-tools/conf-reader-flags)
6+
[![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/sv-tools/conf-reader-flags?style=flat)](https://github.com/sv-tools/conf-reader-flags/releases)
397

8+
The [conf](https://github.com/sv-tools/conf) reader for Command Line Flags.
409

4110
## License
4211

go.mod

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
module github.com/sv-tools/go-repo-template
1+
module github.com/sv-tools/conf-reader-flags
22

3-
go 1.16
3+
go 1.17
4+
5+
require (
6+
github.com/spf13/pflag v1.0.5
7+
github.com/stretchr/testify v1.7.0
8+
github.com/sv-tools/conf v1.2.0
9+
)
10+
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
github.com/spf13/cast v1.4.1 // indirect
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
16+
)

go.sum

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
7+
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
8+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
9+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
10+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
12+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
13+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14+
github.com/sv-tools/conf v1.2.0 h1:wnY7DVMopQxY8tI8dEYI9KV24f+32iPvAcmOZtHmMdQ=
15+
github.com/sv-tools/conf v1.2.0/go.mod h1:JXH0HRFlTwyvLPr0IbD3CNHdp9oiQujuj+iIuYdRkmQ=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
19+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

reader.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package confflags
2+
3+
import (
4+
"context"
5+
6+
"github.com/spf13/pflag"
7+
"github.com/sv-tools/conf"
8+
)
9+
10+
type flagReader struct {
11+
mapFlagKey map[string]string
12+
flagSet *pflag.FlagSet
13+
prefix string
14+
}
15+
16+
func (r *flagReader) Prefix() string {
17+
return r.prefix
18+
}
19+
20+
func (r *flagReader) Read(ctx context.Context) (interface{}, error) {
21+
res := map[string]string{}
22+
for name, key := range r.mapFlagKey {
23+
if fl := r.flagSet.Lookup(name); fl != nil {
24+
res[key] = fl.Value.String()
25+
}
26+
}
27+
28+
return res, ctx.Err()
29+
}
30+
31+
// New creates the Env reader
32+
// `mapFlagKey` is a map of the names of the flag and the configuration keys
33+
// `prefix` is a default prefix that will be added to all configuration keys
34+
func New(mapFlagKey map[string]string, prefix string, flagSet *pflag.FlagSet) conf.Reader {
35+
if flagSet == nil {
36+
flagSet = pflag.CommandLine
37+
}
38+
return &flagReader{
39+
mapFlagKey: mapFlagKey,
40+
prefix: prefix,
41+
flagSet: flagSet,
42+
}
43+
}

0 commit comments

Comments
 (0)