-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautomate.go
128 lines (104 loc) · 2.67 KB
/
automate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
// Initialize Git repository
runCommand("git", "init")
// Add all files
runCommand("git", "add", ".")
// Commit changes
runCommand("git", "commit", "-m", "Initial commit: Added API Security Scanner project files")
// Check if remote origin already exists
if !remoteExists("origin") {
// Add remote repository
runCommand("git", "remote", "add", "origin", "git@github.com:elliotsecops/api-security-scanner.git")
} else {
fmt.Println("Remote origin already exists. Skipping remote addition.")
}
// Push changes
runCommand("git", "push", "-u", "origin", "master")
// Create GitHub Actions workflow
createGitHubActionsWorkflow()
// Create a new branch
runCommand("git", "checkout", "-b", "feature/new-feature")
// Commit changes to the new branch
runCommand("git", "add", ".")
runCommand("git", "commit", "-m", "Add new feature")
// Push the new branch
runCommand("git", "push", "origin", "feature/new-feature")
fmt.Println("Automation completed successfully!")
}
func runCommand(name string, args ...string) {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Printf("Command execution failed: %v\n", err)
os.Exit(1)
}
}
func remoteExists(remoteName string) bool {
cmd := exec.Command("git", "remote")
output, err := cmd.Output()
if err != nil {
fmt.Printf("Failed to check remote: %v\n", err)
os.Exit(1)
}
remotes := strings.Split(string(output), "\n")
for _, remote := range remotes {
if strings.TrimSpace(remote) == remoteName {
return true
}
}
return false
}
func createGitHubActionsWorkflow() {
workflowDir := ".github/workflows"
workflowFile := "ci.yml"
// Create the directory if it doesn't exist
err := os.MkdirAll(workflowDir, 0755)
if err != nil {
fmt.Printf("Failed to create directory: %v\n", err)
os.Exit(1)
}
// Create the workflow file
file, err := os.Create(workflowDir + "/" + workflowFile)
if err != nil {
fmt.Printf("Failed to create file: %v\n", err)
os.Exit(1)
}
defer file.Close()
// Write the workflow content
workflowContent := `name: CI
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Run tests
run: go test ./...
`
_, err = file.WriteString(workflowContent)
if err != nil {
fmt.Printf("Failed to write to file: %v\n", err)
os.Exit(1)
}
fmt.Println("GitHub Actions workflow created successfully!")
}