Skip to content

Commit

Permalink
feat: adds configuration file support.
Browse files Browse the repository at this point in the history
  • Loading branch information
atombender committed Feb 7, 2023
1 parent 114340b commit b000c45
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 20 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ To abort tailing, hit `Ctrl+C`.

Run `ktail -h` for usage.

## Configuration

Ktail will read the file `$HOME/.config/ktail/config.yml` if it exists. This must be a file in YAML format. The following options can be set (these are the defaults):

```yaml
noColor: false
raw: false
timestamps: false
quiet: false
colorScheme: bw
colorMode: auto
kubeConfigPath: ""
templateString: ""
```
## Templating
ktail has a basic output format. To override, you can use a simple Go template. For example:
Expand Down
43 changes: 43 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"os"
"path/filepath"

"k8s.io/apimachinery/pkg/util/yaml"
)

type Config struct {
Quiet bool `yaml:"quiet"`
NoColor bool `yaml:"noColor"`
Raw bool `yaml:"raw"`
Timestamps bool `yaml:"timestamps"`
ColorMode string `yaml:"colorMode"`
ColorScheme string `yaml:"colorScheme"`
TemplateString string `yaml:"templateString"`
KubeConfigPath string `yaml:"kubeConfigPath"`
}

func (c *Config) LoadDefault() error {
home := os.Getenv("HOME")
if home == "" {
return nil
}
if err := c.LoadFromPath(filepath.Join(home, ".config", "ktail", "config.yml")); err != nil &&
!os.IsNotExist(err) {
return err
}
return nil
}

func (c *Config) LoadFromPath(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
if err := yaml.UnmarshalStrict(data, c); err != nil {
return fmt.Errorf("parsing config file %q: %w", path, err)
}
return nil
}
48 changes: 28 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ import (
"k8s.io/klog/v2"
)

const defaultColorScheme = "bw"

func main() {
klog.SetLogger(logr.New(&kubeLogger{}))

cfg := Config{
ColorMode: "auto",
ColorScheme: "bw",
}

var (
contextName string
contextName string
labelSelectorExpr string
namespaces []string
allNamespaces bool

kubeconfigPath string
labelSelectorExpr string
namespaces []string
allNamespaces bool
quiet bool
timestamps bool
raw bool
Expand All @@ -49,36 +53,41 @@ func main() {
colorScheme string
)

if err := cfg.LoadDefault(); err != nil {
fail(err.Error())
}

flags := pflag.NewFlagSet("ktail", pflag.ContinueOnError)
flags.SortFlags = false
flags.Usage = func() {
fmt.Printf("Usage: ktail [OPTION ...] PATTERN [PATTERN ...]\n")
flags.PrintDefaults()
}
flags.StringVar(&contextName, "context", "", "Kubernetes context name")
flags.StringVar(&kubeconfigPath, "kubeconfig", "",
"Path to kubeconfig (only required out-of-cluster)")
flags.StringArrayVarP(&namespaces, "namespace", "n", []string{}, "Kubernetes namespace")
flags.BoolVar(&allNamespaces, "all-namespaces", false, "Apply to all Kubernetes namespaces")
flags.StringArrayVarP(&excludePatternStrings, "exclude", "x", []string{},
"Exclude using a regular expression. Pattern can be repeated. Takes priority over"+
" include patterns and labels.")
flags.StringVarP(&labelSelectorExpr, "selector", "l", "",
"Match pods by label (see 'kubectl get -h' for syntax).")
flags.StringVarP(&tmplString, "template", "t", "",
"Template to format each line. For example, for"+
" just the message, use --template '{{ .Message }}'.")
flags.BoolVarP(&raw, "raw", "r", false, "Don't format output; output messages only (unless --timestamps)")
flags.BoolVarP(&timestamps, "timestamps", "T", false, "Include timestamps on each line")
flags.BoolVarP(&quiet, "quiet", "q", false, "Don't print events about new/deleted pods")
flags.BoolVarP(&sinceStart, "since-start", "s", false,
"Start reading log from the beginning of the container's lifetime.")
flags.BoolVarP(&showVersion, "version", "", false, "Show version.")
flags.BoolVar(&noColor, "no-color", false, "Alias for --color=never.")
flags.StringVar(&colorMode, "color", "auto", "Set color mode: one of 'auto' (default), 'never', or 'always'. (Aliased as --colour.)")
flags.StringVar(&colorMode, "colour", "auto", "Set color mode: one of 'auto' (default), 'never', or 'always'.")
flags.StringVar(&colorScheme, "color-scheme", defaultColorScheme, "Set color scheme (see https://github.com/alecthomas/chroma/tree/master/styles). (Aliased as --colour-scheme.)")
flags.StringVar(&colorScheme, "colour-scheme", defaultColorScheme, "Set color scheme (see https://github.com/alecthomas/chroma/tree/master/styles).")

flags.StringVar(&kubeconfigPath, "kubeconfig", cfg.KubeConfigPath,
"Path to kubeconfig (only required out-of-cluster)")
flags.StringVarP(&tmplString, "template", "t", cfg.TemplateString,
"Template to format each line. For example, for"+
" just the message, use --template '{{ .Message }}'.")
flags.BoolVarP(&raw, "raw", "r", cfg.Raw, "Don't format output; output messages only (unless --timestamps)")
flags.BoolVarP(&timestamps, "timestamps", "T", cfg.Timestamps, "Include timestamps on each line")
flags.BoolVarP(&quiet, "quiet", "q", cfg.Quiet, "Don't print events about new/deleted pods")
flags.BoolVar(&noColor, "no-color", cfg.NoColor, "Alias for --color=never.")
flags.StringVar(&colorMode, "color", cfg.ColorMode, "Set color mode: one of 'auto' (default), 'never', or 'always'. (Aliased as --colour.)")
flags.StringVar(&colorMode, "colour", cfg.ColorMode, "Set color mode: one of 'auto' (default), 'never', or 'always'.")
flags.StringVar(&colorScheme, "color-scheme", cfg.ColorScheme, "Set color scheme (see https://github.com/alecthomas/chroma/tree/master/styles). (Aliased as --colour-scheme.)")
flags.StringVar(&colorScheme, "colour-scheme", cfg.ColorScheme, "Set color scheme (see https://github.com/alecthomas/chroma/tree/master/styles).")
_ = flags.MarkHidden("colour")
_ = flags.MarkHidden("colour-scheme")

Expand All @@ -87,7 +96,6 @@ func main() {
os.Exit(2)
}
fail(err.Error())
os.Exit(1)
}

if noColor {
Expand Down

0 comments on commit b000c45

Please sign in to comment.