Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from Bidaya0/refactor-and-document-change
Browse files Browse the repository at this point in the history
Refactor and document change
  • Loading branch information
Bidaya0 committed Aug 28, 2022
2 parents eb0939a + 13121a1 commit ac7d0bb
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 177 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ gbatect take the `docker-compose.yml` and translates it to `batect.yml`.
- [x] Convert docker-compose.yml to batect.yml
- [x] Support all batect field types
- [x] basic output batect
- [ ] basic cli
- [ ] basic help document
- [x] basic cli
- [x] basic help document
- [x] project quality flow
- [x] coverage test
- [x] code style check
Expand Down
48 changes: 4 additions & 44 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package cmd

import (
"fmt"
"github.com/bidaya0/gbatect/converter"
batecttypes "github.com/bidaya0/gbatect/types"
"github.com/compose-spec/compose-go/loader"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"os"
)

var rootCmd = &cobra.Command{
Expand All @@ -23,53 +18,18 @@ var tofile string

var convertCmd = &cobra.Command{
Use: "convert",
Short: "A batect file convertor",
Long: `gbatect is a tool help users move exists docker-compose to batect.
gbatect take the docker-compose.yml and translates it to batect.yml.
`,
Short: "convert docker-compose file to batect format",
Long: `take the docker-compose.yml and translates it to batect.yml.`,
Run: func(cmd *cobra.Command, args []string) {

if fromfile != "" {
dockercomposefile, err := os.ReadFile(fromfile)
if err != nil {
fmt.Printf("error: %v", err)
}
k1, err := loader.ParseYAML(dockercomposefile)
if err != nil {
fmt.Printf("error: %v", err)
}
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
services, err := converter.LoadServices(tmp3)
containers, err := converter.TransServicesToContainer(services)
var f1 = batecttypes.BatectConfig{
Containers: containers,
}
batectyaml, err := yaml.Marshal(&f1)
if err != nil {
fmt.Printf("error: %v", err)
}

if err != nil {
fmt.Printf("error: %v", err)
}
if tofile != "" {
err := os.WriteFile(tofile, batectyaml, 0644)
if err != nil {
fmt.Printf("%v", err)
}
} else {
fmt.Printf("%v", string(batectyaml))
}
}
converter.ConvertFiletoFile(fromfile, tofile)
},
}

// Execute executes the root command.
func Execute() error {
rootCmd.AddCommand(convertCmd)
convertCmd.Flags().StringVarP(&fromfile, "fromfile", "f", "", "Source directory to read from")
convertCmd.Flags().StringVarP(&tofile, "tofile", "t", "", "Target directory to output")
convertCmd.Flags().StringVarP(&tofile, "tofile", "t", "/dev/stdout", "Target directory to output")
return rootCmd.Execute()
}

Expand Down
55 changes: 34 additions & 21 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"strings"
)

/*
Use to load the services part of docker-compose.yml.
*/
func LoadServices(servicesDict map[string]interface{}) ([]composetypes.ServiceConfig, error) {
var services []composetypes.ServiceConfig
for name, element := range servicesDict {
Expand All @@ -23,6 +26,9 @@ func LoadServices(servicesDict map[string]interface{}) ([]composetypes.ServiceCo
return services, nil
}

/*
Use to convert service defined by compose-go to batecttypes.Containers.
*/
func TransServicesToContainer(servicesconfigs []composetypes.ServiceConfig) (batecttypes.Containers, error) {
containers := make(map[string]batecttypes.ContainerOption)
for _, service := range servicesconfigs {
Expand Down Expand Up @@ -84,26 +90,33 @@ func TransServicesToContainer(servicesconfigs []composetypes.ServiceConfig) (bat
return containers, nil
}

func ReadAndConvert(sourceFilePath string) ([]byte, error) {
dockercomposefile, err := os.ReadFile(sourceFilePath)
if err != nil {
fmt.Printf("error: %v", err)
return nil, err
}
k1, err := loader.ParseYAML(dockercomposefile)
if err != nil {
return nil, err
}
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
services, err := LoadServices(tmp3)
containers, err := TransServicesToContainer(services)
var f1 = batecttypes.BatectConfig{
Containers: containers,
}
batectyaml, err := yaml.Marshal(&f1)
if err != nil {
return nil, err
/*
Use to read docker-compose file from the `fromfile` path , and outout file to the `tofile` path.
*/
func ConvertFiletoFile(fromfile string, tofile string) {
if fromfile != "" {
dockercomposefile, err := os.ReadFile(fromfile)
if err != nil {
fmt.Printf("error: %v", err)
}
k1, err := loader.ParseYAML(dockercomposefile)
if err != nil {
fmt.Printf("error: %v", err)
}
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
services, err := LoadServices(tmp3)
containers, err := TransServicesToContainer(services)
var f1 = batecttypes.BatectConfig{
Containers: containers,
}
batectyaml, err := yaml.Marshal(&f1)
if err != nil {
fmt.Printf("error: %v", err)
}
err = os.WriteFile(tofile, batectyaml, 0644)
if err != nil {
fmt.Printf("%v", err)
}
}
return batectyaml, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
50 changes: 0 additions & 50 deletions testdata/batect.yml

This file was deleted.

58 changes: 0 additions & 58 deletions testdata/docker-compose.yml

This file was deleted.

0 comments on commit ac7d0bb

Please sign in to comment.