Skip to content

Commit

Permalink
Add print config
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietjan committed Aug 22, 2023
1 parent ceb3d4d commit 14b2dee
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
16 changes: 8 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
)

type config struct {
Interval time.Duration `json:"interval,omitempty"`
Exclude []string `json:"exclude,omitempty"`
Build string `json:"build,omitempty"`
Target string `json:"target,omitempty"`
Wait []string `json:"wait,omitempty"`
Server int `json:"server,omitempty"`
Proxy int `json:"proxy,omitempty"`
Interval time.Duration `json:"interval"`
Exclude []string `json:"exclude"`
Build string `json:"build"`
Target string `json:"target"`
Wait []string `json:"wait"`
Server int `json:"server"`
Proxy int `json:"proxy"`
}

func (c config) watcher() (options []watcher.Option) {
Expand Down Expand Up @@ -64,7 +64,7 @@ func fromFile(c *config) error {

func defaults(c *config) {
if c.Interval == 0 {
c.Interval = 500
c.Interval = 300
}

if len(c.Exclude) == 0 {
Expand Down
40 changes: 39 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -16,6 +18,7 @@ import (
"log/slog"

"github.com/antelman107/net-wait-go/wait"
"github.com/ghodss/yaml"
"github.com/julienschmidt/httprouter"
"github.com/lmittmann/tint"
"github.com/pietjan/dev-server/proxy"
Expand All @@ -35,6 +38,13 @@ var script string
func main() {
config := loadConfig()

configFlag := flag.String(`config`, ``, `example config, specify a format: yaml, json`)
flag.Parse()

if isFlagPassed(`config`) {
printConfig(config, *configFlag)
}

logger := slog.New(tint.NewHandler(os.Stdout, nil))
slog.SetDefault(slog.New(
tint.NewHandler(os.Stdout, &tint.Options{
Expand Down Expand Up @@ -80,7 +90,7 @@ func main() {
messages <- strings.Join(changes, `,`)
}

time.Sleep(time.Millisecond)
time.Sleep(time.Millisecond * config.Interval)
}
}()

Expand Down Expand Up @@ -174,3 +184,31 @@ func open(url string) error {
args = append(args, url)
return exec.Command(cmd, args...).Start()
}

func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}

func printConfig(c config, format string) {
c.Wait = append(c.Wait, `postgres:5432`, `sqlserver:1433`)

switch format {
case `json`:
b, _ := json.MarshalIndent(c, ``, ` `)
fmt.Println(string(b))

default:
b, _ := yaml.Marshal(c)
fmt.Println(`---`)
fmt.Println(string(b))
fmt.Println(`...`)
}

os.Exit(0)
}

0 comments on commit 14b2dee

Please sign in to comment.