Skip to content

Commit

Permalink
Refactor autoconfig 2
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Jan 12, 2025
1 parent 0f14169 commit 06cf35a
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 193 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/beta/beta_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ All commands use the runme.yaml configuration file.`,
cmd.SetErr(io.Discard)
}

err := autoconfig.InvokeForCommand(func(cfg *config.Config, log *zap.Logger) error {
err := autoconfig.Invoke(func(cfg *config.Config, log *zap.Logger) error {
// Override the filename if provided.
if cFlags.filename != "" {
cfg.Project.Filename = cFlags.filename
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ List all blocks from the "setup" and "teardown" tags:
runme beta list --tag=setup,teardown
`,
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
proj *project.Project,
filters []project.Filter,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/print_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Print content of commands from the "setup" and "teardown" tags:
runme beta print --tag=setup,teardown
`,
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
proj *project.Project,
filters []project.Filter,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Run all blocks from the "setup" and "teardown" tags:
runme beta run --tag=setup,teardown
`,
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
clientFactory autoconfig.ClientFactory,
cmdFactory command.Factory,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/server/server_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Cmd() *cobra.Command {
Short: "Commands to manage and call a runme server.",
Hidden: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cfg *config.Config,
) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/server/server_grpcurl_describe_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func serverGRPCurlDescribeCmd() *cobra.Command {
Short: "Describe gRPC services and methods exposed by the server.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cfg *config.Config,
logger *zap.Logger,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/server/server_grpcurl_invoke_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func serverGRPCurlInvokeCmd() *cobra.Command {
Short: "Invoke gRPC command to the server.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cfg *config.Config,
logger *zap.Logger,
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/server/server_grpcurl_list_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func serverGRPCurlListCmd() *cobra.Command {
Short: "List gRPC services exposed by the server.",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cfg *config.Config,
logger *zap.Logger,
Expand Down
4 changes: 1 addition & 3 deletions internal/cmd/beta/server/server_start_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/stateful/runme/v3/internal/command"
"github.com/stateful/runme/v3/internal/config"
"github.com/stateful/runme/v3/internal/config/autoconfig"
"github.com/stateful/runme/v3/internal/server"
Expand All @@ -19,11 +18,10 @@ func serverStartCmd() *cobra.Command {
Use: "start",
Short: "Start a server.",
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cfg *config.Config,
server *server.Server,
cmdFactory command.Factory,
logger *zap.Logger,
) error {
defer logger.Sync()
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/server/server_stop_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func serverStopCmd() *cobra.Command {
Use: "stop",
Short: "Stop a server.",
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cfg *config.Config,
logger *zap.Logger,
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/session_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func sessionCmd(*commonFlags) *cobra.Command {
All exported variables during the session will be available to the subsequent commands.
`,
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cmdFactory command.Factory,
logger *zap.Logger,
Expand Down Expand Up @@ -133,7 +133,7 @@ func sessionSetupCmd() *cobra.Command {
Use: "setup",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
return autoconfig.Invoke(
func(
cmdFactory command.Factory,
logger *zap.Logger,
Expand Down
84 changes: 41 additions & 43 deletions internal/config/autoconfig/autoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,34 @@ import (
"github.com/stateful/runme/v3/internal/runnerv2service"
"github.com/stateful/runme/v3/internal/server"
runmetls "github.com/stateful/runme/v3/internal/tls"
parserv1 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/parser/v1"
projectv1 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/project/v1"
runnerv2 "github.com/stateful/runme/v3/pkg/api/gen/proto/go/runme/runner/v2"
"github.com/stateful/runme/v3/pkg/document/editor/editorservice"
"github.com/stateful/runme/v3/pkg/project"
)

var (
container = dig.New()
commandScope = container.Scope("command")
serverScope = container.Scope("server")
)

func DecorateRoot(decorator interface{}, opts ...dig.DecorateOption) error {
return container.Decorate(decorator, opts...)
}
var defaultBuilder = NewBuilder()

// InvokeForCommand is used to invoke the function with the given dependencies.
// The package will automatically figure out how to instantiate them
// using the available configuration.
//
// Use it only for commands because it supports only singletons
// created during the program initialization.
func InvokeForCommand(function interface{}, opts ...dig.InvokeOption) error {
err := commandScope.Invoke(function, opts...)
return dig.RootCause(err)
type Builder struct {
*dig.Container
}

// InvokeForServer is similar to InvokeForCommand, but it does not provide
// all the dependencies, in particular, it does not provide dependencies
// that differ per request.
func InvokeForServer(function interface{}, opts ...dig.InvokeOption) error {
err := serverScope.Invoke(function, opts...)
return dig.RootCause(err)
func NewBuilder() *Builder {
b := Builder{Container: dig.New()}
b.init()
return &b
}

func mustProvide(err error) {
if err != nil {
panic("failed to provide: " + err.Error())
func (b *Builder) init() {
mustProvide := func(err error) {
if err != nil {
panic("failed to provide: " + err.Error())
}
}
}

func init() {
container := b

mustProvide(container.Provide(getClient))
mustProvide(container.Provide(getClientFactory))
mustProvide(container.Provide(getCommandFactory))
Expand All @@ -80,7 +68,18 @@ func init() {
mustProvide(container.Provide(getProjectFilters))
mustProvide(container.Provide(getRootConfig))
mustProvide(container.Provide(getServer))
mustProvide(container.Provide(getUserConfigDir))
}

func Decorate(decorator interface{}, opts ...dig.DecorateOption) error {
return defaultBuilder.Decorate(decorator, opts...)
}

// Invoke is used to invoke the function with the given dependencies.
// The package will automatically figure out how to instantiate them
// using the available configuration.
func Invoke(function interface{}, opts ...dig.InvokeOption) error {
err := defaultBuilder.Invoke(function, opts...)
return dig.RootCause(err)
}

func getClient(cfg *config.Config, clientConn *grpc.ClientConn, logger *zap.Logger) (*runnerv2client.Client, error) {
Expand Down Expand Up @@ -136,7 +135,6 @@ func getDocker(c *config.Config, logger *zap.Logger) (*dockerexec.Docker, error)

func getGRPCClient(
cfg *config.Config,
server *server.Server,
logger *zap.Logger,
) (*grpc.ClientConn, error) {
if cfg.Server == nil {
Expand All @@ -159,7 +157,7 @@ func getGRPCClient(
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

conn, err := grpc.NewClient(server.Addr(), opts...)
conn, err := grpc.NewClient(cfg.Server.Address, opts...)
return conn, errors.WithStack(err)
}

Expand Down Expand Up @@ -282,7 +280,7 @@ func getProjectFilters(c *config.Config) ([]project.Filter, error) {
return filters, nil
}

func getRootConfig(cfgLoader *config.Loader, userCfgDir UserConfigDir) (*config.Config, error) {
func getRootConfig(cfgLoader *config.Loader) (*config.Config, error) {
var cfg *config.Config

items, err := cfgLoader.RootConfigs()
Expand All @@ -300,6 +298,11 @@ func getRootConfig(cfgLoader *config.Loader, userCfgDir UserConfigDir) (*config.
if cfg.Server != nil && cfg.Server.Tls != nil && cfg.Server.Tls.Enabled {
tls := cfg.Server.Tls

userCfgDir, err := os.UserConfigDir()
if err != nil {
return nil, errors.WithMessage(err, "failed to get user config directory")
}

if tls.CertFile == nil {
path := filepath.Join(string(userCfgDir), "runme", "tls", "cert.pem")
tls.CertFile = &path
Expand Down Expand Up @@ -327,16 +330,11 @@ func getServer(cfg *config.Config, cmdFactory command.Factory, logger *zap.Logge

return server.New(
cfg,
parserService,
projectService,
runnerService,
logger,
func(sr grpc.ServiceRegistrar) {
parserv1.RegisterParserServiceServer(sr, parserService)
projectv1.RegisterProjectServiceServer(sr, projectService)
runnerv2.RegisterRunnerServiceServer(sr, runnerService)
},
)
}

type UserConfigDir string

func getUserConfigDir() (UserConfigDir, error) {
dir, err := os.UserConfigDir()
return UserConfigDir(dir), errors.WithStack(err)
}
Loading

0 comments on commit 06cf35a

Please sign in to comment.