Skip to content

Commit a70f205

Browse files
committed
feat: embed greyproxy.yml as default config when no -C flag is passed
Uses Go's //go:embed to bake greyproxy.yml into the binary so it works out of the box without requiring an external config file.
1 parent e6671c7 commit a70f205

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

cmd/greyproxy/program.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"syscall"
1212

13+
defaults "github.com/greyhavenhq/greyproxy"
1314
"github.com/greyhavenhq/greyproxy/internal/gostcore/logger"
1415
"github.com/greyhavenhq/greyproxy/internal/gostcore/service"
1516
greyproxy "github.com/greyhavenhq/greyproxy/internal/greyproxy"
@@ -37,12 +38,13 @@ type program struct {
3738

3839
func (p *program) Init(env svc.Environment) error {
3940
parser.Init(parser.Args{
40-
CfgFile: cfgFile,
41-
Services: services,
42-
Nodes: nodes,
43-
Debug: debug,
44-
Trace: trace,
45-
MetricsAddr: metricsAddr,
41+
CfgFile: cfgFile,
42+
DefaultConfig: defaults.DefaultConfig,
43+
Services: services,
44+
Nodes: nodes,
45+
Debug: debug,
46+
Trace: trace,
47+
MetricsAddr: metricsAddr,
4648
})
4749

4850
return nil

defaultconfig.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package greyproxy
2+
3+
import _ "embed"
4+
5+
// DefaultConfig is the embedded default greyproxy.yml configuration.
6+
// It is used when no -C flag is provided on the command line.
7+
//
8+
//go:embed greyproxy.yml
9+
var DefaultConfig []byte

internal/gostx/config/parsing/parser/parser.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package parser
22

33
import (
44
"bufio"
5+
"bytes"
56
"encoding/json"
67
"os"
78
"strings"
@@ -28,12 +29,13 @@ func Parse() (*config.Config, error) {
2829
}
2930

3031
type Args struct {
31-
CfgFile string
32-
Services []string
33-
Nodes []string
34-
Debug bool
35-
Trace bool
36-
MetricsAddr string
32+
CfgFile string
33+
DefaultConfig []byte // embedded default config (used when CfgFile is empty)
34+
Services []string
35+
Nodes []string
36+
Debug bool
37+
Trace bool
38+
MetricsAddr string
3739
}
3840

3941
type parser struct {
@@ -68,6 +70,11 @@ func (p *parser) Parse() (*config.Config, error) {
6870
return nil, err
6971
}
7072
}
73+
} else if len(p.args.DefaultConfig) > 0 {
74+
// No -C flag: use the embedded default configuration.
75+
if err := cfg.Read(bytes.NewReader(p.args.DefaultConfig), "yaml"); err != nil {
76+
return nil, err
77+
}
7178
}
7279

7380
cmdCfg, err := cmd.BuildConfigFromCmd(p.args.Services, p.args.Nodes)

0 commit comments

Comments
 (0)