Skip to content

Commit

Permalink
F
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Sep 29, 2024
1 parent 6f66118 commit e0913b5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 53 deletions.
10 changes: 7 additions & 3 deletions internal/cmd/beta/beta_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"

"github.com/stateful/runme/v3/internal/cmd/beta/server"
"github.com/stateful/runme/v3/internal/config"
Expand Down Expand Up @@ -36,7 +37,7 @@ All commands use the runme.yaml configuration file.`,
cmd.SetErr(io.Discard)
}

err := autoconfig.InvokeForCommand(func(cfg *config.Config) error {
err := autoconfig.InvokeForCommand(func(cfg *config.Config, log *zap.Logger) error {
// Override the filename if provided.
if cFlags.filename != "" {
cfg.Project.Filename = cFlags.filename
Expand All @@ -54,10 +55,13 @@ All commands use the runme.yaml configuration file.`,
)
}

log.Info("final configuration", zap.Any("config", cfg))

return nil
})
// print the error to stderr but don't return it because error modes
// are neither fully baked yet nor ready for users to consume

// Print the error to stderr but don't return it because error modes
// are neither fully baked yet nor ready for users to consume.
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", err)
}
Expand Down
13 changes: 7 additions & 6 deletions internal/cmd/beta/run_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func runCmd(*commonFlags) *cobra.Command {
var serverAddress string
var remote bool

cmd := cobra.Command{
Use: "run [command1 command2 ...]",
Expand All @@ -40,7 +40,7 @@ Run all blocks from the "setup" and "teardown" tags:
RunE: func(cmd *cobra.Command, args []string) error {
return autoconfig.InvokeForCommand(
func(
client *runnerv2client.Client,
clientFactory autoconfig.ClientFactory,
cmdFactory command.Factory,
filters []project.Filter,
logger *zap.Logger,
Expand Down Expand Up @@ -74,9 +74,10 @@ Run all blocks from the "setup" and "teardown" tags:

ctx := cmd.Context()

if serverAddress != "" && client != nil {
if serverAddress != "local" {
return errors.New("only local server is supported")
if remote {
client, err := clientFactory()
if err != nil {
return err
}

sessionResp, err := client.CreateSession(
Expand Down Expand Up @@ -122,7 +123,7 @@ Run all blocks from the "setup" and "teardown" tags:
},
}

cmd.Flags().StringVarP(&serverAddress, "server", "s", "", "Server address to connect to. Use 'local' for local server.")
cmd.Flags().BoolVarP(&remote, "remote", "r", false, "Run commands on a remote server.")

return &cmd
}
Expand Down
93 changes: 49 additions & 44 deletions internal/config/autoconfig/autoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func mustProvide(err error) {

func init() {
mustProvide(container.Provide(getClient))
mustProvide(container.Provide(getClientFactory))
mustProvide(container.Provide(getCommandFactory))
mustProvide(container.Provide(getConfigLoader))
mustProvide(container.Provide(getDocker))
Expand All @@ -76,6 +77,39 @@ func init() {
mustProvide(container.Provide(getUserConfigDir))
}

func getClient(cfg *config.Config, logger *zap.Logger) (*runnerv2client.Client, error) {
if cfg.Server == nil {
return nil, nil
}

var opts []grpc.DialOption

if cfg.Server.Tls != nil && cfg.Server.Tls.Enabled {
tlsConfig, err := runmetls.LoadClientConfig(cfg.Server.Tls.CertFile, cfg.Server.Tls.KeyFile)
if err != nil {
return nil, errors.WithStack(err)
}
creds := credentials.NewTLS(tlsConfig)
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

return runnerv2client.New(
cfg.Server.Address,
logger,
opts...,
)
}

type ClientFactory func() (*runnerv2client.Client, error)

func getClientFactory(cfg *config.Config, logger *zap.Logger) ClientFactory {
return func() (*runnerv2client.Client, error) {
return getClient(cfg, logger)
}
}

func getCommandFactory(docker *dockerexec.Docker, logger *zap.Logger, proj *project.Project) command.Factory {
return command.NewFactory(
command.WithDocker(docker),
Expand Down Expand Up @@ -112,31 +146,6 @@ func getDocker(c *config.Config, logger *zap.Logger) (*dockerexec.Docker, error)
return dockerexec.New(options)
}

func getClient(cfg *config.Config, logger *zap.Logger) (*runnerv2client.Client, error) {
if cfg.Server == nil {
return nil, nil
}

var opts []grpc.DialOption

if cfg.Server.Tls != nil && cfg.Server.Tls.Enabled {
tlsConfig, err := runmetls.LoadClientConfig(cfg.Server.Tls.CertFile, cfg.Server.Tls.KeyFile)
if err != nil {
return nil, errors.WithStack(err)
}
creds := credentials.NewTLS(tlsConfig)
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

return runnerv2client.New(
cfg.Server.Address,
logger,
opts...,
)
}

func getLogger(c *config.Config) (*zap.Logger, error) {
if c == nil || c.Log == nil || !c.Log.Enabled {
return zap.NewNop(), nil
Expand Down Expand Up @@ -176,32 +185,28 @@ func getProject(c *config.Config, logger *zap.Logger) (*project.Project, error)
project.WithLogger(logger),
}

if c.Project.Filename == "" {
if c.Project.Filename != "" {
return project.NewFileProject(c.Project.Filename, opts...)
}

if c.Project.Root != "" {
projDir := c.Project.Root
// If no project directory is specified, use the current directory.
if projDir == "" {
projDir = "."
}

opts = append(
opts,
project.WithIgnoreFilePatterns(c.Project.Ignore...),
project.WithRespectGitignore(!c.Project.DisableGitignore),
project.WithEnvFilesReadOrder(c.Project.Env.Sources),
)
projDir := c.Project.Root
// If no project directory is specified, use the current directory.
if projDir == "" {
projDir = "."
}

if c.Project.FindRepoUpward {
opts = append(opts, project.WithFindRepoUpward())
}
opts = append(
opts,
project.WithIgnoreFilePatterns(c.Project.Ignore...),
project.WithRespectGitignore(!c.Project.DisableGitignore),
project.WithEnvFilesReadOrder(c.Project.Env.Sources),
)

return project.NewDirProject(projDir, opts...)
if c.Project.FindRepoUpward {
opts = append(opts, project.WithFindRepoUpward())
}

return nil, nil
return project.NewDirProject(projDir, opts...)
}

func getProjectFilters(c *config.Config) ([]project.Filter, error) {
Expand Down
11 changes: 11 additions & 0 deletions internal/config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@
"address"
]
},
"client": {
"type": "object",
"properties": {
"server_address": {
"type": "string"
}
},
"required": [
"server_address"
]
},
"log": {
"type": "object",
"properties": {
Expand Down

0 comments on commit e0913b5

Please sign in to comment.