Skip to content

Commit 124ec33

Browse files
committed
refactor(config): make server configuration optional with smart defaults
1 parent 9fb26d4 commit 124ec33

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

cmd/deploy.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func runDeploy(cmd *cobra.Command, args []string) {
4141
return
4242
}
4343

44-
if err := deployToServer(cfg.Project.Name, cfg, cfg.Server, pDeploy); err != nil {
44+
if err := deployToServer(cfg.Project.Name, cfg, pDeploy); err != nil {
4545
pDeploy.Fail(fmt.Sprintf("Deployment failed: %v", err))
4646
return
4747
}
@@ -63,7 +63,8 @@ func parseConfig(filename string) (*config.Config, error) {
6363
return cfg, nil
6464
}
6565

66-
func deployToServer(project string, cfg *config.Config, server config.Server, spinner *pin.Pin) error {
66+
func deployToServer(project string, cfg *config.Config, spinner *pin.Pin) error {
67+
server := cfg.Server
6768
hostname := server.Host
6869

6970
spinner.UpdateMessage("Connecting to server " + hostname + "...")
@@ -101,7 +102,7 @@ func deployToServer(project string, cfg *config.Config, server config.Server, sp
101102
return nil
102103
}
103104

104-
func connectToServer(server config.Server) (*remote.Runner, error) {
105+
func connectToServer(server *config.Server) (*remote.Runner, error) {
105106
sshKeyPath := filepath.Join(os.Getenv("HOME"), ".ssh", filepath.Base(server.SSHKey))
106107
sshClient, _, err := ssh.FindKeyAndConnectWithUser(server.Host, server.Port, server.User, sshKeyPath)
107108
if err != nil {

pkg/config/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
type Config struct {
2222
Project Project `yaml:"project" validate:"required"`
23-
Server Server `yaml:"server" validate:"required"`
23+
Server *Server `yaml:"server" validate:"omitempty"`
2424
Services []Service `yaml:"services" validate:"required,dive"`
2525
Dependencies []Dependency `yaml:"dependencies" validate:"dive"`
2626
Volumes []string `yaml:"volumes" validate:"dive"`
@@ -319,6 +319,11 @@ func ParseConfig(data []byte) (*Config, error) {
319319
return nil, fmt.Errorf("error parsing YAML: %v", err)
320320
}
321321

322+
// Set empty server if not specified
323+
if config.Server == nil {
324+
config.Server = &Server{}
325+
}
326+
322327
// Set default host to project.domain if not specified
323328
if config.Server.Host == "" {
324329
config.Server.Host = config.Project.Domain

pkg/config/config_test.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ services:
585585
Domain: "example.com",
586586
Email: "admin@example.com",
587587
},
588-
Server: Server{
588+
Server: &Server{
589589
Host: "server.example.com",
590590
Port: 22,
591591
User: "deploy",
@@ -625,7 +625,7 @@ services:
625625
Domain: "example.com",
626626
Email: "admin@example.com",
627627
},
628-
Server: Server{
628+
Server: &Server{
629629
Host: "example.com",
630630
Port: 22,
631631
User: "deploy",
@@ -674,7 +674,7 @@ services:
674674
Domain: "example.com",
675675
Email: "admin@example.com",
676676
},
677-
Server: Server{
677+
Server: &Server{
678678
Host: "server.example.com",
679679
Port: 22,
680680
User: "deploy",
@@ -729,7 +729,7 @@ dependencies:
729729
Domain: "example.com",
730730
Email: "admin@example.com",
731731
},
732-
Server: Server{
732+
Server: &Server{
733733
Host: "server.example.com",
734734
Port: 22,
735735
User: "deploy",
@@ -816,7 +816,7 @@ services:
816816
Domain: "example.com",
817817
Email: "admin@example.com",
818818
},
819-
Server: Server{
819+
Server: &Server{
820820
Host: "server.example.com",
821821
Port: 22,
822822
User: "deploy",
@@ -1133,3 +1133,24 @@ func TestFindDefaultSSHKey(t *testing.T) {
11331133
assert.NoError(t, err)
11341134
assert.Equal(t, filepath.Join(sshDir, "id_rsa"), foundKey)
11351135
}
1136+
1137+
func TestNilServer(t *testing.T) {
1138+
yamlData := []byte(`
1139+
project:
1140+
name: test-project
1141+
domain: example.com
1142+
email: admin@example.com
1143+
services:
1144+
- name: web
1145+
image: nginx:latest
1146+
port: 80
1147+
routes:
1148+
- path: /
1149+
`)
1150+
1151+
config, err := ParseConfig(yamlData)
1152+
assert.NoError(t, err)
1153+
assert.NotNil(t, config)
1154+
assert.NotNil(t, config.Server)
1155+
assert.Equal(t, "example.com", config.Server.Host)
1156+
}

pkg/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func Setup(ctx context.Context, cfg *config.Config, dockerCreds DockerCredential
3131
return nil
3232
}
3333

34-
func setupServer(ctx context.Context, cfg config.Server, dockerCreds DockerCredentials, newUserPassword string, spinner *pin.Pin) error {
34+
func setupServer(ctx context.Context, cfg *config.Server, dockerCreds DockerCredentials, newUserPassword string, spinner *pin.Pin) error {
3535
spinner.UpdateMessage("Establishing SSH connection to server " + cfg.Host + " as root...")
3636
sshClient, rootKey, err := ssh.FindKeyAndConnectWithUser(cfg.Host, cfg.Port, "root", cfg.SSHKey)
3737
if err != nil {
@@ -118,7 +118,7 @@ func createUser(ctx context.Context, runner *remote.Runner, user, password strin
118118
return runner.RunCommands(ctx, commands)
119119
}
120120

121-
func setupSSHKey(ctx context.Context, runner *remote.Runner, server config.Server) error {
121+
func setupSSHKey(ctx context.Context, runner *remote.Runner, server *config.Server) error {
122122
keyData, err := readSSHKey(server.SSHKey)
123123
if err != nil {
124124
return err

0 commit comments

Comments
 (0)