Skip to content

Commit

Permalink
docs: Add generated env reference
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 17, 2024
1 parent 39bf615 commit 3fa833b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ CastSponsorSkip can be configured with envs, command-line flags, or a config fil
To use an env that is not listed here, capitalize all characters, replace `-` with `_`, and prefix with `CSS_`. For example, `--paused-interval=1m` would become `CSS_PAUSED_INTERVAL=1m`.

### Notable Envs
Here's a list of commonly used envs. Also see [full env reference](docs/envs.md).
| Env | Description | Default |
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------|
| `CSS_DISCOVER_INTERVAL` | Interval to restart the DNS discovery client. | `5m` |
Expand All @@ -171,7 +173,7 @@ To use an env that is not listed here, capitalize all characters, replace `-` wi
> [sponsorblockcast envs](https://github.com/nichobi/sponsorblockcast#configuration) are also supported to simplify the migration to CastSponsorSkip. When used, a deprecation warning will be logged with an updated env key and value. There are currently no plans to remove these envs.
### Flags
See command-line flag documentation [here](./docs/castsponsorskip.md).
See [command-line reference](./docs/castsponsorskip.md).
### Systemd
To modify the variables when running as a systemd service, create an override for the service with:
Expand Down
18 changes: 18 additions & 0 deletions docs/envs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Environment Variables

| Name | Usage | Default |
| --- | --- | --- |
| `CSS_ACTION_TYPES` | SponsorBlock action types to handle. Shorter segments that overlap with content can be muted instead of skipped. | `skip,mute` |
| `CSS_CATEGORIES` | Comma-separated list of SponsorBlock categories to skip | `sponsor` |
| `CSS_DEVICES` | Comma-separated list of device addresses. This will disable discovery and is not recommended unless discovery fails | ` ` |
| `CSS_DISCOVER_INTERVAL` | Interval to restart the DNS discovery client | `5m0s` |
| `CSS_IGNORE_SEGMENT_DURATION` | Ignores the previous sponsored segment for a set amount of time. Useful if you want to to go back and watch a segment. | `1m0s` |
| `CSS_LOG_FORMAT` | Log format (one of: auto, color, plain, json) | `auto` |
| `CSS_LOG_LEVEL` | Log level (one of: debug, info, warn, error) | `info` |
| `CSS_MUTE_ADS` | Mutes the device while an ad is playing | `true` |
| `CSS_NETWORK_INTERFACE` | Network interface to use for multicast dns discovery. (default all interfaces) | ` ` |
| `CSS_PAUSED_INTERVAL` | Interval to scan paused devices | `1m0s` |
| `CSS_PLAYING_INTERVAL` | Interval to scan playing devices | `500ms` |
| `CSS_SKIP_DELAY` | Delay skipping the start of a segment | `0s` |
| `CSS_SKIP_SPONSORS` | Skip sponsored segments with SponsorBlock | `true` |
| `CSS_YOUTUBE_API_KEY` | YouTube API key for fallback video identification (required on some Chromecast devices). | ` ` |
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/buger/jsonparser v1.1.1
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/env v0.1.0
github.com/knadh/koanf/providers/file v1.1.0
Expand Down Expand Up @@ -43,12 +44,14 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/miekg/dns v1.1.58 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
go.opencensus.io v0.24.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+UV8OU=
github.com/jedib0t/go-pretty/v6 v6.5.9/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
Expand All @@ -100,6 +102,8 @@ github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
Expand All @@ -116,6 +120,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down
91 changes: 80 additions & 11 deletions internal/generate/docs/main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,101 @@
package main

import (
"errors"
"fmt"
"log"
"io"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"

"github.com/gabe565/castsponsorskip/cmd"
"github.com/gabe565/castsponsorskip/internal/config"
"github.com/gabe565/castsponsorskip/internal/config/names"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
)

func main() {
var err error
output := "./docs"

err = os.RemoveAll(output)
if err != nil {
log.Fatal(fmt.Errorf("failed to remove existing dia: %w", err))
if err := os.RemoveAll(output); err != nil {
slog.Error("failed to remove existing dir", "error", err)
os.Exit(1)
}

err = os.MkdirAll(output, 0o755)
if err != nil {
log.Fatal(fmt.Errorf("failed to mkdir: %w", err))
if err := os.MkdirAll(output, 0o755); err != nil {
slog.Error("failed to mkdir", "error", err)
os.Exit(1)
}

root := cmd.New(cmd.WithVersion("beta"))

if err := errors.Join(
generateFlagDoc(root, output),
generateEnvDoc(root, filepath.Join(output, "envs.md")),
); err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}

func generateFlagDoc(cmd *cobra.Command, output string) error {
if err := doc.GenMarkdownTree(cmd, output); err != nil {
return fmt.Errorf("failed to generate markdown: %w", err)
}
return nil
}

rootCmd := cmd.New(cmd.WithVersion("beta"))
func generateEnvDoc(cmd *cobra.Command, output string) error {
excludeNames := []string{"completion", names.FlagConfig, "help", "version"}
var rows []table.Row
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
if slices.Contains(excludeNames, flag.Name) {
return
}

err = doc.GenMarkdownTree(rootCmd, output)
var value string
switch fv := flag.Value.(type) {
case pflag.SliceValue:
value = strings.Join(flag.Value.(pflag.SliceValue).GetSlice(), ",")
default:
value = fv.String()
}
if value == "" {
value = " "
}

rows = append(rows, table.Row{
"`" + config.EnvPrefix + strings.ReplaceAll(strings.ToUpper(flag.Name), "-", "_") + "`",
flag.Usage,
"`" + value + "`",
})
})
t := table.NewWriter()
t.AppendHeader(table.Row{"Name", "Usage", "Default"})
t.AppendRows(rows)

var buf strings.Builder
buf.WriteString("# Environment Variables\n\n")
buf.WriteString(t.RenderMarkdown())

f, err := os.Create(output)
if err != nil {
log.Fatal(fmt.Errorf("failed to generate markdown: %w", err))
return fmt.Errorf("failed to create env file: %w", err)
}
defer f.Close()

if _, err := io.WriteString(f, buf.String()); err != nil {
return fmt.Errorf("failed to write to env file: %w", err)
}

if err := f.Close(); err != nil {
return fmt.Errorf("failed to close env file: %w", err)
}

return nil
}

0 comments on commit 3fa833b

Please sign in to comment.