-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (99 loc) · 3.21 KB
/
main.go
File metadata and controls
109 lines (99 loc) · 3.21 KB
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
package main
import (
"flag"
"fmt"
"io"
"os"
"strings"
"ns8-module-generator/commands"
"ns8-module-generator/config"
"ns8-module-generator/http"
"ns8-module-generator/processors"
)
func main() {
cfg := config.New()
config.Cfg = cfg
configPath := flag.String("config", "config.yaml", "Path to the configuration file (YAML)")
flag.Parse()
// Check if Template Dir exists
_, err := os.Stat(cfg.TemplateDir)
if os.IsNotExist(err) {
err = http.DownloadTemplate()
if err != nil {
fmt.Fprintf(os.Stderr, "Error downloading template: %v\n", err)
os.Exit(1)
}
}
// Try to load configuration from config.yaml
appConfig, err := config.LoadAppConfig(*configPath)
if err == nil {
// If config.yaml loaded successfully, use its values
cfg.DockerComposePath = appConfig.DockerComposePath
cfg.AppName = appConfig.AppName
cfg.OutputDir = appConfig.OutputDir
cfg.AppGitInit = appConfig.AppGitInit
cfg.GithubOrganizationName = appConfig.GithubOrganizationName
cfg.GithubUsername = appConfig.GithubUsername
cfg.GithubToken = appConfig.GithubToken
cfg.GitAuthMethod = appConfig.GitAuthMethod
cfg.IsRemoteDockerCompose = appConfig.IsRemoteDockerCompose
} else if !os.IsNotExist(err) {
// If config.yaml exists but there was an error loading it (e.g., malformed YAML)
fmt.Fprintf(os.Stderr, "Error loading config.yaml: %v\n", err)
os.Exit(1)
} else {
// config.yaml does not exist, fall back to interactive prompts
err = commands.InputPrompts(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error during input prompts: %v\n", err)
os.Exit(1)
}
}
var composeFileContent []byte
if cfg.IsRemoteDockerCompose {
if strings.Contains(cfg.DockerComposePath, "github.com") {
cfg.DockerComposePath = convertToGithubBlobToRaw(cfg.DockerComposePath)
}
// Download the file from the URL to a temporary file
tempFile, err := os.Create("remote.yaml")
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating temporary file for remote Docker Compose: %v\n", err)
os.Exit(1)
}
defer os.Remove(tempFile.Name()) // Clean up the temporary file
defer tempFile.Close()
err = http.DownloadFile(cfg.DockerComposePath, tempFile.Name())
if err != nil {
fmt.Fprintf(os.Stderr, "Error downloading remote Docker Compose file: %v\n", err)
os.Exit(1)
}
composeFileContent, err = io.ReadAll(tempFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading downloaded Docker Compose content: %v\n", err)
os.Exit(1)
}
} else {
// Open the local file and read the content
composeFile, err := os.Open(cfg.DockerComposePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening local Docker Compose file: %v\n", err)
os.Exit(1)
}
defer composeFile.Close()
composeFileContent, err = io.ReadAll(composeFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading local Docker Compose content: %v\n", err)
os.Exit(1)
}
}
err = processors.ProcessNs8Module(cfg, composeFileContent)
if err != nil {
fmt.Fprintf(os.Stderr, "Error processing NS8 module: %v\n", err)
os.Exit(1)
}
}
func convertToGithubBlobToRaw(blobURL string) string {
rawURL := strings.Replace(blobURL, "github.com", "raw.githubusercontent.com", 1)
rawURL = strings.Replace(rawURL, "/blob", "", 1)
return rawURL
}