-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add docs option generation into the pipeline (#54)
- Loading branch information
Showing
13 changed files
with
443 additions
and
96 deletions.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: docs ci | ||
on: | ||
pull_request: | ||
paths: | ||
- '.github/workflows/on_pull_request_docs.yaml' | ||
- 'Earthfile' | ||
- '*/**.go' | ||
- '*.go' | ||
- 'go.mod' | ||
- 'go.sum' | ||
- 'docs/usage.md*' | ||
jobs: | ||
lint-docs: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: wistia/[email protected] | ||
|
||
- uses: earthly/actions-setup@v1 | ||
with: { version: "${{ env.EARTHLY_TOOL_VERSION }}" } | ||
|
||
- name: rebuild the docs | ||
run: | | ||
earthly \ | ||
+rebuild-docs \ | ||
--GOLANG_VERSION="${{ env.GOLANG_TOOL_VERSION }}" | ||
- name: verify that the checked in file has not changed | ||
run: | | ||
#!/usr/bin/env bash | ||
exitCode=0 | ||
# Log the actual diff for debugging purposes | ||
git diff --name-only | cat | ||
if ! git diff --exit-code --quiet; then | ||
echo "Please run 'earthly +rebuild-docs' and commit the results to this PR" | ||
exitCode=1 | ||
fi | ||
exit $exitCode |
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
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,4 +1,4 @@ | ||
earthly 0.7.12 | ||
earthly 0.7.14 | ||
golang 1.19.11 | ||
helm 3.12.2 | ||
helm-ct 3.8.0 | ||
|
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
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,98 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type DocOpt[D any] struct { | ||
choices []string | ||
defaultValue *D | ||
shorthand *string | ||
} | ||
|
||
func combine[D any](dst *DocOpt[D], src DocOpt[D]) { | ||
if src.choices != nil { | ||
dst.choices = src.choices | ||
} | ||
if src.defaultValue != nil { | ||
dst.defaultValue = src.defaultValue | ||
} | ||
if src.shorthand != nil { | ||
dst.shorthand = src.shorthand | ||
} | ||
} | ||
|
||
func ViperNameToEnv(s string) string { | ||
s = envKeyReplacer.Replace(s) | ||
s = fmt.Sprintf("%s_%s", envPrefix, s) | ||
s = strings.ToUpper(s) | ||
return s | ||
} | ||
|
||
func boolFlag(flags *pflag.FlagSet, name, usage string, opts ...DocOpt[bool]) { | ||
addFlag(name, usage, opts, flags.Bool, flags.BoolP) | ||
} | ||
|
||
func newStringOpts() DocOpt[string] { | ||
return DocOpt[string]{} | ||
} | ||
|
||
func stringFlag(flags *pflag.FlagSet, name, usage string, opts ...DocOpt[string]) { | ||
addFlag(name, usage, opts, flags.String, flags.StringP) | ||
} | ||
|
||
func addFlag[D any]( | ||
name, usage string, | ||
opts []DocOpt[D], | ||
onlyLong func(string, D, string) *D, | ||
longAndShort func(string, string, D, string) *D, | ||
) { | ||
var opt DocOpt[D] | ||
for _, o := range opts { | ||
combine(&opt, o) | ||
} | ||
|
||
usage = generateUsage(opt, usage, name) | ||
var defaultValue D | ||
if opt.defaultValue != nil { | ||
defaultValue = *opt.defaultValue | ||
} | ||
|
||
if opt.shorthand != nil { | ||
longAndShort(name, *opt.shorthand, defaultValue, usage) | ||
} else { | ||
onlyLong(name, defaultValue, usage) | ||
} | ||
} | ||
|
||
func generateUsage[D any](opt DocOpt[D], usage string, name string) string { | ||
if !strings.HasSuffix(usage, ".") { | ||
panic(fmt.Sprintf("usage for %q must end with a period.", name)) | ||
} | ||
|
||
if opt.choices != nil { | ||
usage = fmt.Sprintf("%s One of %s.", usage, strings.Join(opt.choices, ", ")) | ||
} | ||
|
||
envVar := ViperNameToEnv(name) | ||
usage = fmt.Sprintf("%s (%s)", usage, envVar) | ||
return usage | ||
} | ||
|
||
func (d DocOpt[D]) withDefault(def D) DocOpt[D] { | ||
d.defaultValue = &def | ||
return d | ||
} | ||
|
||
func (d DocOpt[D]) withShortHand(short string) DocOpt[D] { | ||
d.shorthand = &short | ||
return d | ||
} | ||
|
||
func (d DocOpt[D]) withChoices(choices ...string) DocOpt[D] { | ||
d.choices = choices | ||
return d | ||
} |
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,53 @@ | ||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStringUsages(t *testing.T) { | ||
tests := map[string]struct { | ||
expected string | ||
name string | ||
opt DocOpt[any] | ||
usage string | ||
}{ | ||
"string with choices": { | ||
name: "simple-string", | ||
opt: DocOpt[any]{ | ||
choices: []string{ | ||
"blah", | ||
"test", | ||
}, | ||
}, | ||
usage: "This is a test.", | ||
expected: "This is a test. One of blah, test. (KUBECHECKS_SIMPLE_STRING)", | ||
}, | ||
"string with out of order choices": { | ||
name: "simple-string", | ||
opt: DocOpt[any]{ | ||
choices: []string{ | ||
"test", | ||
"blah", | ||
}, | ||
}, | ||
usage: "This is a test.", | ||
expected: "This is a test. One of test, blah. (KUBECHECKS_SIMPLE_STRING)", | ||
}, | ||
"string with no choices": { | ||
name: "string", | ||
opt: DocOpt[any]{}, | ||
usage: "This is a test.", | ||
expected: "This is a test. (KUBECHECKS_STRING)", | ||
}, | ||
} | ||
|
||
for testName, test := range tests { | ||
t.Run(testName, func(t *testing.T) { | ||
|
||
actual := generateUsage(test.opt, test.usage, test.name) | ||
assert.Equal(t, test.expected, actual) | ||
}) | ||
} | ||
} |
Oops, something went wrong.