Skip to content

Commit

Permalink
customize elems
Browse files Browse the repository at this point in the history
  • Loading branch information
root27 committed Mar 28, 2024
1 parent 764d011 commit 81dad91
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 48 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: Deployment
name: deployment
"on":
push:
branches:
- master
pull_request:
branches: []
jobs:
deploy:
runs-on: ubuntu-latest
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module github.com/root27/yml-parser

go 1.22.1
go 1.22

require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require gopkg.in/yaml.v2 v2.4.0
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
89 changes: 49 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"fmt"
"os"

"gopkg.in/yaml.v2"
"reflect"
)

type Workflow struct {
Expand All @@ -15,6 +13,11 @@ type Workflow struct {

type On struct {
Push Push `yaml:"push"`
Pull Pull `yaml:"pull_request"`
}

type Pull struct {
Branches []string `yaml:"branches"`
}

type Push struct {
Expand Down Expand Up @@ -45,9 +48,9 @@ func main() {
workflow := Workflow{}

// Gather user inputs
fmt.Print("Enter your repository name: ")
var repoName string
fmt.Scanln(&repoName)
fmt.Print("Enter the workflow name: ")
var workFlowName string
fmt.Scanln(&workFlowName)

fmt.Print("Enter the trigger event (e.g., push, pull_request): ")
var triggerEvent string
Expand All @@ -58,40 +61,46 @@ func main() {
fmt.Scanln(&onBranch)

// Populate the Workflow struct
workflow.Name = "Deployment"
workflow.On.Push.Branches = []string{onBranch}
workflow.Jobs.Deploy.RunsOn = "ubuntu-latest"
workflow.Jobs.Deploy.Steps = []Step{
{
Uses: "actions/checkout@v2",
},
{
Name: "Deploy to server",
Env: Env{
ServerKey: "${{ secrets.SERVER_KEY }}",
},
Run: `echo "$SERVER_KEY" > secret && chmod 600 secret && ssh -o StrictHostKeyChecking=no -i secret [email protected] -p 8357 'ls -la'`,
},
}

// Set environment variables
// workflow.Jobs.Deploy.Steps[0].Env = Env{
// ServerKey: "${{ secrets.SERVER_KEY }}",

flowType := reflect.TypeOf(workflow)

newStruct := reflect.New(flowType)

newStruct.Elem().FieldByName("Name").SetString(workFlowName)

newStruct.Elem().FieldByName("On").FieldByName(triggerEvent).FieldByName("Branches").Set(reflect.ValueOf([]string{onBranch}))

fmt.Print(newStruct.Interface())

// workflow.On.Push.Branches = []string{onBranch}

// workflow.Jobs.Deploy.RunsOn = "ubuntu-latest"
// workflow.Jobs.Deploy.Steps = []Step{
// {
// Uses: "actions/checkout@v2",
// },
// {
// Name: "Deploy to server",
// Env: Env{
// ServerKey: "${{ secrets.SERVER_KEY }}",
// },
// Run: `echo "$SERVER_KEY" > secret && chmod 600 secret && ssh -o StrictHostKeyChecking=no -i secret [email protected] -p 8357 'ls -la'`,
// },
// }

// // 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)
// if err != nil {
// fmt.Printf("Error writing YAML file: %v\n", err)
// return
// }

// 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)
if err != nil {
fmt.Printf("Error writing YAML file: %v\n", err)
return
}

fmt.Println("Deployment YAML file generated successfully.")
// fmt.Println("Deployment YAML file generated successfully.")
}

0 comments on commit 81dad91

Please sign in to comment.