Skip to content

Commit

Permalink
added /types and generateScripts()
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuda committed Jul 29, 2024
1 parent 36d1a21 commit 672db2d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
33 changes: 5 additions & 28 deletions internal/dockercompose/dockercompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ import (
"path/filepath"

"github.com/fatih/color"
DockermiTypes "github.com/mkhuda/dockermi/types"
"gopkg.in/yaml.v2"
)

// Service represents a service in the docker-compose.yml file.
type Service struct {
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Labels map[string]string `yaml:"labels"`
}

// DockerCompose represents the structure of the docker-compose.yml file.
type DockerCompose struct {
Services map[string]Service `yaml:"services"`
}

// FindServices searches for docker-compose.yml files in the specified directory.
// It scans the directory and its subdirectories for docker-compose.yml files,
// parses them to extract services with specific labels, and returns a list of
Expand All @@ -32,16 +21,8 @@ type DockerCompose struct {
// - []struct{Order, ServiceName, ComposeFile string}: a slice of structs containing
// the order, service name, and path to the docker-compose file for each relevant service
// - bool: a boolean indicating if any docker-compose.yml files were found
func FindServices(root string) ([]struct {
Order string
ServiceName string
ComposeFile string
}, bool) {
var services []struct {
Order string
ServiceName string
ComposeFile string
}
func FindServices(root string) (DockermiTypes.ServiceScriptReturn, bool) {
var services DockermiTypes.ServiceScriptReturn

foundDockerCompose := false

Expand All @@ -61,7 +42,7 @@ func FindServices(root string) ([]struct {
return err
}

var dockerCompose DockerCompose
var dockerCompose DockermiTypes.DockerCompose
if err := yaml.Unmarshal(data, &dockerCompose); err != nil {
return err
}
Expand All @@ -75,11 +56,7 @@ func FindServices(root string) ([]struct {
color.Blue("Order: %s", order)
color.Yellow("Active: %s", active)

services = append(services, struct {
Order string
ServiceName string
ComposeFile string
}{
services = append(services, DockermiTypes.ServiceScript{
Order: order,
ServiceName: serviceName,
ComposeFile: path,
Expand Down
7 changes: 2 additions & 5 deletions internal/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@ import (
"time"

"github.com/fatih/color"
DockermiTypes "github.com/mkhuda/dockermi/types"
"github.com/schollz/progressbar/v3"
)

// CreateDockermiScript generates the dockermi.sh script based on the provided services.
func CreateDockermiScript(scriptPath string, services []struct {
Order string
ServiceName string
ComposeFile string
}) error {
func CreateDockermiScript(scriptPath string, services DockermiTypes.ServiceScriptReturn) error {
dockermiScript, err := os.Create(scriptPath)
if err != nil {
return err
Expand Down
12 changes: 5 additions & 7 deletions pkg/dockermi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/fatih/color"
)

const version = "0.0.8"
const version = "0.0.9"

// RunDockermi executes the main logic of the dockermi command. It takes a
// projectDir parameter, which specifies the directory where the function
Expand All @@ -41,13 +41,11 @@ func RunDockermi(projectDir string) (string, error) {
return "", nil
}

// projectDir, err := os.Getwd()
// if err != nil {
// color.Red("Error getting current directory: %v", err)
// return err
// }
return generateScripts(projectDir)
}

// Find docker-compose.yml files
// generateScripts finds docker-compose.yml files and generates corresponding scripts.
func generateScripts(projectDir string) (string, error) {
services, foundDockerCompose := dockercompose.FindServices(projectDir)

if !foundDockerCompose {
Expand Down
23 changes: 23 additions & 0 deletions types/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package types

// ServiceScript is a struct at the ServiceScriptReturn
type ServiceScript struct {
Order string
ServiceName string
ComposeFile string
}

// ServiceScriptReturn represent the return of some internal methods
type ServiceScriptReturn []ServiceScript

// Service represents a service in the docker-compose.yml file.
type Service struct {
Image string `yaml:"image"`
Ports []string `yaml:"ports"`
Labels map[string]string `yaml:"labels"`
}

// DockerCompose represents the structure of the docker-compose.yml file.
type DockerCompose struct {
Services map[string]Service `yaml:"services"`
}

0 comments on commit 672db2d

Please sign in to comment.