-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
142 lines (122 loc) · 3.39 KB
/
init.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"os"
"os/exec"
"strings"
"time"
"github.com/urfave/cli/v2"
"harness/defaults"
)
//var tempFilePath = "project_info.tmp"
func InitProject(c *cli.Context) error {
color.Set(color.FgYellow)
fmt.Println("Initializing project...")
color.Unset()
framework, language := detectProjectFramework()
fmt.Printf("Detected framework: %s\n", framework)
fmt.Printf("Detected language: %s\n", language)
fmt.Print("Is this correct? (y/n) : ")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
return err
}
if response != "y" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter the framework: ")
framework, _ = reader.ReadString('\n')
fmt.Print("Enter the language: ")
language, _ = reader.ReadString('\n')
framework = strings.TrimSpace(framework)
language = strings.TrimSpace(language)
}
err = saveProjectInfo(framework, language)
if err != nil {
return err
}
fmt.Println("Project initialized with the following framework and language:")
fmt.Printf("Framework: %s\n", framework)
fmt.Printf("Language: %s\n", language)
progressBar()
color.Set(color.FgGreen)
fmt.Println("\nHarness project initialized successfully!")
color.Unset()
fmt.Print("Do you want to Proceed with the deployment? (y/n) : ")
var deployment string
_, err = fmt.Scanln(&deployment)
if err != nil {
return err
}
if deployment == "y" {
err := deployPipeline()
if err != nil {
return err
}
}
return nil
}
func progressBar() {
barLength := 20
spinChars := []string{"|", "/", "-", "\\"}
for i := 0; i <= barLength; i++ {
spinner := spinChars[i%len(spinChars)]
progress := strings.Repeat("=", i) + strings.Repeat(" ", barLength-i)
color.Set(color.FgCyan)
fmt.Printf("\r[%s] %s %s", progress, spinner, "Initializing...")
color.Unset()
time.Sleep(time.Millisecond * 100)
}
}
func detectProjectFramework() (string, string) {
type Framework struct {
Name string
Language string
}
frameworks := map[string]Framework{
"pom.xml": {"Spring Boot", "Java"},
"package.json": {"Node.js", "JavaScript"},
"build.gradle": {"Spring Boot", "Java"},
"requirements.txt": {"Flask", "Python"},
"Gemfile": {"Ruby on Rails", "Ruby"},
"go.mod": {"Go Module", "Go"},
"Cargo.toml": {"Cargo", "Rust"},
"composer.json": {"Composer", "PHP"},
"CMakeLists.txt": {"CMake", "C++"},
"Makefile": {"Make", "C/C++"},
"mix.exs": {"Elixir Mix", "Elixir"},
"project.clj": {"Leiningen", "Clojure"},
"build.sbt": {"SBT", "Scala"},
"pubspec.yaml": {"Dart", "Dart"},
}
for file, framework := range frameworks {
if _, err := os.Stat(file); err == nil {
return framework.Name, framework.Language
}
}
return "Unknown", "Unknown"
}
func saveProjectInfo(framework, language string) error {
file, err := os.Create(defaults.TEMPFILEPATH)
if err != nil {
return fmt.Errorf("failed to create temp file: %v", err)
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("framework=%s\nlanguage=%s\n", framework, language))
if err != nil {
return fmt.Errorf("failed to write to temp file: %v", err)
}
return nil
}
func deployPipeline() error {
cmd := exec.Command("harness", "deploy")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("error running deploy command: %v", err)
}
return nil
}