Skip to content

Commit

Permalink
structure edited
Browse files Browse the repository at this point in the history
  • Loading branch information
root27 committed Apr 1, 2024
1 parent 81801e4 commit 27a2db6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 37 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: test
env:
- name: testdeploy
env:
SERVER_KEY: ${{ secrets.SERVER_KEY }}
run: echo hello
run: ls -la
59 changes: 59 additions & 0 deletions editParser/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package editParser

import (
"errors"
"fmt"
"os"
"strings"

"github.com/root27/yml-parser/structs"
)

func EditParser(yamlData []byte, workflow *structs.Workflow) error {

lines := strings.Split(string(yamlData), "\n")

for _, step := range workflow.Jobs.Deploy.Steps {

if step.Env != nil && step.Run != "" {

for i, line := range lines {

if strings.Contains(line, "name: "+step.Name) {

count := 0

for _, char := range lines[i] {

if char == ' ' {

count++

}

}

fmt.Printf("Count: %d\n", count)

lines[i] = strings.Repeat(" ", count-3) + "- name: " + step.Name

}
}

}

}

newFileData := strings.Join(lines, "\n")

err := os.WriteFile(".github/workflows/deployment.yml", []byte(newFileData), 0644)

if err != nil {

return errors.New("error writing to file")

}

return nil

}
44 changes: 10 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,11 @@ import (
"os"
"strings"

"github.com/root27/yml-parser/editParser"
"github.com/root27/yml-parser/structs"
"gopkg.in/yaml.v2"
)

type Workflow struct {
Name string `yaml:"name"`
On interface{} `yaml:"on"`
Jobs Jobs `yaml:"jobs"`
}

type Jobs struct {
Deploy Deploy `yaml:"deploy"`
}

type Deploy struct {
RunsOn string `yaml:"runs-on"`
Steps []Step `yaml:"steps"`
}

type Step struct {
Uses string `yaml:"uses,omitempty"`
Name string `yaml:"name,omitempty"`
Env Env `yaml:"env,omitempty"`
Run string `yaml:"run,omitempty"`
}

type Env map[string]interface{}

func main() {

// Gather user inputs
Expand Down Expand Up @@ -60,7 +38,7 @@ func main() {

// Populate the Workflow struct

workflow := Workflow{}
workflow := structs.Workflow{}

workflow.Name = workFlowName

Expand Down Expand Up @@ -102,10 +80,10 @@ func main() {

// Add step to workflow

workflow.Jobs.Deploy.Steps = append(workflow.Jobs.Deploy.Steps, Step{
workflow.Jobs.Deploy.Steps = append(workflow.Jobs.Deploy.Steps, structs.Step{
Uses: uses,
Name: stepName,
Env: Env{
Env: structs.Env{
secretName: "${{ secrets." + secretName + " }}",
},
Run: runCommand,
Expand All @@ -116,29 +94,27 @@ func main() {

// Add step to workflow

workflow.Jobs.Deploy.Steps = append(workflow.Jobs.Deploy.Steps, Step{
workflow.Jobs.Deploy.Steps = append(workflow.Jobs.Deploy.Steps, structs.Step{
Uses: uses,
Name: stepName,
})

}

fmt.Printf("Workflow steps: %v\n", workflow.Jobs.Deploy.Steps)

// Convert struct to YAML
yamlData, err := yaml.Marshal(&workflow)
if err != nil {
fmt.Printf("Error marshalling YAML: %v\n", err)
return
}

// Write YAML to file
err = os.WriteFile(".github/workflows/deployment.yml", yamlData, 0644)
err = editParser.EditParser(yamlData, &workflow)

if err != nil {
fmt.Printf("Error writing YAML file: %v\n", err)
fmt.Printf("Error editing YAML: %v\n", err)
return
}

fmt.Println("Deployment YAML file generated successfully.")
fmt.Println("Workflow created successfully")

}
25 changes: 25 additions & 0 deletions structs/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package structs

type Workflow struct {
Name string `yaml:"name"`
On interface{} `yaml:"on"`
Jobs Jobs `yaml:"jobs"`
}

type Jobs struct {
Deploy Deploy `yaml:"deploy"`
}

type Deploy struct {
RunsOn string `yaml:"runs-on"`
Steps []Step `yaml:"steps"`
}

type Step struct {
Uses string `yaml:"uses,omitempty"`
Name string `yaml:"name,omitempty"`
Env Env `yaml:"env,omitempty"`
Run string `yaml:"run,omitempty"`
}

type Env map[string]interface{}

0 comments on commit 27a2db6

Please sign in to comment.