Skip to content

Commit

Permalink
update env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
burnerlee committed Jan 3, 2024
1 parent f14a531 commit f33d5b2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 38 deletions.
8 changes: 3 additions & 5 deletions internal/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ func Deploy(ctx context.Context, configFile string) error {
if err := s.Validate(ctx); err != nil {
return err
}
go func(s service.Service) {
if err := s.Deploy(ctx); err != nil {
fmt.Println("error deploying service", err)
}
}(s)
if err := s.Deploy(ctx); err != nil {
fmt.Println("error deploying service", err)
}
}
return nil
}
12 changes: 6 additions & 6 deletions internal/service/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func (d *dockerService) getContainerConfigs(ctx context.Context) (*container.Con
if err != nil {
return nil, fmt.Errorf("error parsing port configs: %v", err)
}
dockerEnvs, err := getDockerEnvConfigs(ctx, d.Env)
envsFromFiles, err := parseEnvFiles(ctx, d.EnvFiles)
if err != nil {
return nil, fmt.Errorf("error getting docker env configs: %v", err)
return nil, fmt.Errorf("error parsing env files: %v", err)
}
envsFromFiles = append(envsFromFiles, d.Env...)
return &container.Config{
Image: d.Image,

Env: dockerEnvs,
Image: d.Image,
Env: envsFromFiles,
ExposedPorts: exposedPorts,
Labels: map[string]string{
"starter": "up",
Expand Down Expand Up @@ -75,7 +75,7 @@ func (d *dockerService) deployDocker(ctx context.Context) error {

var authStr string

if d.AuthConfig != nil {
if d.Auth {
authConfigs := registry.AuthConfig{
Username: d.AuthConfig.Username,
Password: d.AuthConfig.Password,
Expand Down
30 changes: 18 additions & 12 deletions internal/service/env.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package service

import (
"bufio"
"context"
"fmt"
"os"
"path"
"strings"
)

func getDockerEnvConfigs(ctx context.Context, env []string) ([]string, error) {
dockerEnvs := make([]string, 0)
for _, e := range env {
for _, envVar := range UpConfigs.Env {
envSplit := strings.Split(string(envVar), "=")
if len(envSplit) < 2 {
fmt.Printf("invalid env variable %s\n", envVar)
}
if e == envSplit[0] {
dockerEnvs = append(dockerEnvs, string(envVar))
func parseEnvFiles(ctx context.Context, files []string) ([]string, error) {
envs := make([]string, 0)
for _, f := range files {
filePath := path.Join(os.Getenv("PWD"), f)
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "=") {
envs = append(envs, line)
}
}
}
return dockerEnvs, nil
return envs, nil
}
12 changes: 6 additions & 6 deletions internal/service/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package service
import (
"context"
"fmt"
"os"
"path"
)

func (l *localService) validate(ctx context.Context) error {
Expand All @@ -11,15 +13,13 @@ func (l *localService) validate(ctx context.Context) error {

func (l *localService) deployLocal(ctx context.Context) error {
fmt.Println("deploying local service", l.Name)
envs, err := getDockerEnvConfigs(ctx, l.Env)
if err != nil {
return err
}
for _, e := range l.Exec {
path := path.Join(os.Getenv("PWD"), l.Path)
fmt.Println("running command", e, "in path", path)
cmd := CommandConfig{
Command: e,
EnvVars: envs,
WorkingDir: l.Path,
EnvVars: l.Env,
WorkingDir: path,
}
if err := cmd.Run(ctx); err != nil {
fmt.Println("error running command", err)
Expand Down
14 changes: 12 additions & 2 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"fmt"
"os"
)

func (s *Service) Validate(ctx context.Context) error {
Expand All @@ -15,6 +16,12 @@ func (s *Service) Validate(ctx context.Context) error {
func (s *Service) Deploy(ctx context.Context) error {
switch s.Source {
case "docker":
if s.Auth && s.AuthConfig == nil {
s.AuthConfig = &AuthConfig{
Username: os.Getenv("DOCKER_USERNAME"),
Password: os.Getenv("DOCKER_PASSWORD"),
}
}
d := &dockerService{
Name: s.Name,
ContainerName: s.ContainerName,
Expand All @@ -24,7 +31,9 @@ func (s *Service) Deploy(ctx context.Context) error {
Pre: s.Pre,
Volumes: s.Volumes,
Networks: s.Networks,
AuthConfig: s.Auth,
Auth: s.Auth,
EnvFiles: s.EnvFiles,
AuthConfig: s.AuthConfig,
}
if err := d.validate(ctx); err != nil {
return err
Expand All @@ -41,8 +50,9 @@ func (s *Service) Deploy(ctx context.Context) error {
if err := l.validate(ctx); err != nil {
return err
}
return l.deployLocal(ctx)
go l.deployLocal(ctx)
default:
return fmt.Errorf("unknown source: %s", s.Source)
}
return nil
}
18 changes: 11 additions & 7 deletions internal/service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ type Service struct {
Pre []string `yaml:"pre,omitempty"`
Exec []string `yaml:"exec,omitempty"`
Networks []string `yaml:"networks,omitempty"`
Auth *authConfig `yaml:"auth,omitempty"`
}

type authConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Auth bool `yaml:"auth,omitempty"`
EnvFiles []string `yaml:"env_files,omitempty"`
AuthConfig *AuthConfig `yaml:"auth_config,omitempty"`
}

type Command struct {
Expand All @@ -42,7 +39,9 @@ type dockerService struct {
Env []string
Pre []string
Networks []string
AuthConfig *authConfig
Auth bool
EnvFiles []string
AuthConfig *AuthConfig
}

type Config struct {
Expand All @@ -53,6 +52,11 @@ type Config struct {
Deploy []string `yaml:"deploy"`
}

type AuthConfig struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}

type localService struct {
Name string
Path string
Expand Down

0 comments on commit f33d5b2

Please sign in to comment.