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 #3 from Bidaya0/develop-add-gofmt-check-ci
Browse files Browse the repository at this point in the history
develop-add-gofmt-check-ci
  • Loading branch information
Bidaya0 committed Aug 21, 2022
2 parents f49eee1 + 34c54e6 commit 8dfa7b8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 84 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/gofmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: "Go fmt check"

on:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]

jobs:
fmt-check:
name: fmt-check
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '>=1.19.0'
- run: |
gofmt -l -e -d .
test -z $(gofmt -l .)
35 changes: 15 additions & 20 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package cmd

import (
"github.com/bidaya0/gbatect/converter"
"github.com/spf13/cobra"
"fmt"
"github.com/bidaya0/gbatect/converter"
"github.com/spf13/cobra"
"os"
)


var rootCmd = &cobra.Command{
Use: "gbatect",
Short: "A batect file convertor",
Expand All @@ -16,7 +15,6 @@ var rootCmd = &cobra.Command{
`,
}


var fromfile string
var tofile string

Expand All @@ -26,26 +24,24 @@ var convertCmd = &cobra.Command{
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.
`,
Run: func(cmd *cobra.Command, args []string) {
//if len(args) > 0{
// filepath := args[0]
//}
if fromfile != "" {
result := converter.ReadAndConvert(fromfile)
if tofile != "" {
err := os.WriteFile(tofile, result, 0644)
if err != nil{
fmt.Printf("%v", err)
}
}else{
fmt.Printf("%v", string(result))
Run: func(cmd *cobra.Command, args []string) {
//if len(args) > 0{
// filepath := args[0]
//}
if fromfile != "" {
result := converter.ReadAndConvert(fromfile)
if tofile != "" {
err := os.WriteFile(tofile, result, 0644)
if err != nil {
fmt.Printf("%v", err)
}
} else {
fmt.Printf("%v", string(result))
}
}
},
}



// Execute executes the root command.
func Execute() error {
rootCmd.AddCommand(convertCmd)
Expand All @@ -56,4 +52,3 @@ func Execute() error {

func init() {
}

36 changes: 18 additions & 18 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package converter

import (
"fmt"
batecttypes "github.com/bidaya0/gbatect/types"
"github.com/compose-spec/compose-go/loader"
composetypes "github.com/compose-spec/compose-go/types"
batecttypes "github.com/bidaya0/gbatect/types"
"gopkg.in/yaml.v3"
"os"
"strings"
Expand All @@ -27,18 +27,18 @@ func TransServicesToContainer(servicesconfigs []composetypes.ServiceConfig) (bat
containers := make(map[string]batecttypes.ContainerOption)
for _, service := range servicesconfigs {
containeroption := batecttypes.ContainerOption{
Image: service.Image,
AdditionalHosts: service.ExtraHosts,
CapabilitiesToAdd: service.CapAdd,
Image: service.Image,
AdditionalHosts: service.ExtraHosts,
CapabilitiesToAdd: service.CapAdd,
CapabilitiesToDrop: service.CapDrop,
Devices: service.Devices,
EnableInitProcess: service.Init,
Environment: service.Environment,
ShmSize: service.ShmSize,
WorkingDirectory: service.WorkingDir,
Devices: service.Devices,
EnableInitProcess: service.Init,
Environment: service.Environment,
ShmSize: service.ShmSize,
WorkingDirectory: service.WorkingDir,
}
for _, network := range service.Networks {
containeroption.AdditionalHostnames = append(containeroption.AdditionalHostnames, network.Aliases...)
containeroption.AdditionalHostnames = append(containeroption.AdditionalHostnames, network.Aliases...)
}
if service.Build != nil {
containeroption.BuildArgs = service.Build.Args
Expand All @@ -48,10 +48,10 @@ func TransServicesToContainer(servicesconfigs []composetypes.ServiceConfig) (bat
}
if service.HealthCheck != nil {
containeroption.HealthCheck = batecttypes.HealthCheck{
Interval: service.HealthCheck.Interval,
Retries: service.HealthCheck.Retries,
StartPeriod: service.HealthCheck.StartPeriod,
Timeout: service.HealthCheck.Timeout,
Interval: service.HealthCheck.Interval,
Retries: service.HealthCheck.Retries,
StartPeriod: service.HealthCheck.StartPeriod,
Timeout: service.HealthCheck.Timeout,
}
if service.HealthCheck.Test != nil {
containeroption.HealthCheck.Command = strings.Join(service.HealthCheck.Test, " ")
Expand Down Expand Up @@ -84,7 +84,7 @@ func TransServicesToContainer(servicesconfigs []composetypes.ServiceConfig) (bat
return containers, nil
}

func ReadAndConvert(sourceFilePath string) ([]byte){
func ReadAndConvert(sourceFilePath string) []byte {
dockercomposefile, err := os.ReadFile(sourceFilePath)
if err != nil {
fmt.Printf("error: %v", err)
Expand All @@ -97,12 +97,12 @@ func ReadAndConvert(sourceFilePath string) ([]byte){
tmp3, _ := tmpk.(map[string]interface{})
services, err := LoadServices(tmp3)
containers, err := TransServicesToContainer(services)
var f1 batecttypes.BatectConfig
f1.Containers = containers
var f1 = batecttypes.BatectConfig{
Containers: containers,
}
batectyaml, err := yaml.Marshal(&f1)
if err != nil {
fmt.Printf("error: %v", err)
}
return batectyaml
}

3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"github.com/bidaya0/gbatect/cmd"
"github.com/bidaya0/gbatect/cmd"
"os"
)


func main() {
if err := cmd.Execute(); err != nil {
os.Exit(1)
Expand Down
87 changes: 43 additions & 44 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,60 @@
package types

import (
// "fmt"
// "github.com/compose-spec/compose-go/loader"
// "fmt"
// "github.com/compose-spec/compose-go/loader"
composetypes "github.com/compose-spec/compose-go/types"
// "gopkg.in/yaml.v3"
// "os"
// "strings"
// "gopkg.in/yaml.v3"
// "os"
// "strings"
)


type ContainerOption struct {
AdditionalHostnames []string "additional_hostnames,omitempty"
AdditionalHosts composetypes.HostsList "additional_hosts,omitempty"
BuildArgs composetypes.MappingWithEquals "build_args,omitempty"
BuildDirectory string "build_directory,omitempty"
BuildStage string "build_stage,omitempty"
BuildTarget string "build_target,omitempty"
CapabilitiesToAdd []string "capabilities_to_add,omitempty"
CapabilitiesToDrop []string "capabilities_to_drop,omitempty"
Command string "command,omitempty"
Dependencies []string "dependencies,omitempty"
Devices []string "devices,omitempty" // /dev/sda:/dev/disk:r ??
Dockerfile string ",omitempty"
EnableInitProcess *bool "enable_init_process,omitempty"
Entrypoint string "entrypoint,omitempty"
Environment composetypes.MappingWithEquals ",omitempty"
HealthCheck HealthCheck "health_check,omitempty"
Image string ",omitempty"
ImagePullPolicy string "image_pull_policy,omitempty"
LogDriver string "log_driver,omitempty"
LogOptions map[string]string "log_options,omitempty"
Ports []string ",omitempty"
Privileged bool "privileged,omitempty"
RunAsCurrentUser RunAsCurrentUser "run_as_current_user,omitempty"
setup_commands []SetupCommand "setup_commands,omitempty"
ShmSize composetypes.UnitBytes "shm_size,omitempty"
Volumes []string ",omitempty"
WorkingDirectory string "working_directory,omitempty"
AdditionalHostnames []string "additional_hostnames,omitempty"
AdditionalHosts composetypes.HostsList "additional_hosts,omitempty"
BuildArgs composetypes.MappingWithEquals "build_args,omitempty"
BuildDirectory string "build_directory,omitempty"
BuildStage string "build_stage,omitempty"
BuildTarget string "build_target,omitempty"
CapabilitiesToAdd []string "capabilities_to_add,omitempty"
CapabilitiesToDrop []string "capabilities_to_drop,omitempty"
Command string "command,omitempty"
Dependencies []string "dependencies,omitempty"
Devices []string "devices,omitempty" // /dev/sda:/dev/disk:r ??
Dockerfile string ",omitempty"
EnableInitProcess *bool "enable_init_process,omitempty"
Entrypoint string "entrypoint,omitempty"
Environment composetypes.MappingWithEquals ",omitempty"
HealthCheck HealthCheck "health_check,omitempty"
Image string ",omitempty"
ImagePullPolicy string "image_pull_policy,omitempty"
LogDriver string "log_driver,omitempty"
LogOptions map[string]string "log_options,omitempty"
Ports []string ",omitempty"
Privileged bool "privileged,omitempty"
RunAsCurrentUser RunAsCurrentUser "run_as_current_user,omitempty"
setup_commands []SetupCommand "setup_commands,omitempty"
ShmSize composetypes.UnitBytes "shm_size,omitempty"
Volumes []string ",omitempty"
WorkingDirectory string "working_directory,omitempty"
}

type SetupCommand struct{
Command string "command,omitempty"
workingDirectory string "working_directory,omitempty"
type SetupCommand struct {
Command string "command,omitempty"
workingDirectory string "working_directory,omitempty"
}

type RunAsCurrentUser struct{
Enabled bool "enabled,omitempty"
HomeDirectory bool "home_directory,omitempty"
type RunAsCurrentUser struct {
Enabled bool "enabled,omitempty"
HomeDirectory bool "home_directory,omitempty"
}

type HealthCheck struct {
Command string ",omitempty"
Retries *uint64 ",omitempty"
Interval *composetypes.Duration ",omitempty"
StartPeriod *composetypes.Duration "start_period,omitempty"
Timeout *composetypes.Duration "timeout,omitempty"
Command string ",omitempty"
Retries *uint64 ",omitempty"
Interval *composetypes.Duration ",omitempty"
StartPeriod *composetypes.Duration "start_period,omitempty"
Timeout *composetypes.Duration "timeout,omitempty"
}

type ContainerItem struct {
Expand Down

0 comments on commit 8dfa7b8

Please sign in to comment.