-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAnsibleProject.go
136 lines (106 loc) · 3.6 KB
/
CreateAnsibleProject.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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/urfave/cli/v2"
)
func CreateAnsibleProjectAction(c *cli.Context) error {
if c.Args().Len() > 0 {
return errors.New("No arguments is expected, use flags")
}
if !c.IsSet("name") {
return errors.New("A name for the project is required")
}
// Create main project directory structure
if err := createAnsibleProject(c); err != nil {
return errors.New("Can't create project with provided name. " +
"Please check if the directory name already exists.")
}
// Prompt and create role(s)
doesUserNeedRoleDirectory(c.String("name"))
fmt.Println("Project created successfully.")
return nil
}
func createAnsibleProject(c *cli.Context) error {
projectName := c.String("name")
if err := os.Mkdir(fmt.Sprint(projectName), os.ModePerm); err != nil {
log.Fatal(err)
return errors.New(fmt.Sprint("Can't create "))
}
for _, destination := range mainDirectoryStructure {
if destination.Type == "directory" {
if err := os.Mkdir(fmt.Sprint(projectName, destination.Path, "/",
destination.Name), os.ModePerm); err != nil {
log.Fatal(err)
return errors.New("Can't create " + destination.Type + " at " +
destination.Path + "/" + destination.Name)
}
} else {
pathToFile := fmt.Sprint(projectName, destination.Path, "/")
fullFileToCreatePath := fmt.Sprint(projectName, destination.Path, "/",
destination.Name)
if _, err := os.Stat(pathToFile); os.IsNotExist(err) {
os.MkdirAll(pathToFile, 0777)
}
// Create file
if _, err := os.OpenFile(fullFileToCreatePath, os.O_RDONLY|os.O_CREATE, 0666); err != nil {
log.Fatal(err)
return errors.New("Can't create " + destination.Type + " at " +
destination.Path + "/" + destination.Name)
}
}
}
return nil
}
func doesUserNeedRoleDirectory(projectName string) string {
for {
fmt.Print("Do you want to create Ansible role in this project? Y/n ")
wantRole, _ := bufio.NewReader(os.Stdin).ReadString('\n')
wantRoleCaseInsensitive := strings.ToLower(strings.TrimSpace(wantRole))
if wantRoleCaseInsensitive == "n" {
return ""
}
if wantRoleCaseInsensitive == "y" {
fmt.Print("Enter the name of role: ")
roleName, _ := bufio.NewReader(os.Stdin).ReadString('\n')
roleNameTrimmed := strings.TrimSpace(roleName)
createRoleDirectory(projectName, roleNameTrimmed)
}
}
}
func createRoleDirectory(projectName string, roleName string) error {
pathToRole := fmt.Sprint(projectName, "/roles/", roleName)
fmt.Println("Creating role " + roleName + " in " + projectName)
if err := os.MkdirAll(pathToRole, os.ModePerm); err != nil {
log.Fatal(err)
return errors.New(fmt.Sprint("Can't create directory for role ", roleName))
}
for _, destination := range roleDirectoryStructure {
if destination.Type == "directory" {
if err := os.Mkdir(fmt.Sprint(projectName, destination.Path, "/", roleName,
destination.Name), os.ModePerm); err != nil {
log.Fatal(err)
return errors.New("Can't create " + destination.Type + " at " +
destination.Path + "/" + destination.Name)
}
} else {
pathToFile := fmt.Sprint(projectName, destination.Path, "/", roleName)
fullFileToCreatePath := fmt.Sprint(projectName, destination.Path, "/",
roleName, destination.Name)
if _, err := os.Stat(pathToFile); os.IsNotExist(err) {
os.MkdirAll(pathToFile, 0777)
}
// Create file
if _, err := os.OpenFile(fullFileToCreatePath, os.O_RDONLY|os.O_CREATE, 0666); err != nil {
log.Fatal(err)
return errors.New("Can't create " + destination.Type + " at " +
destination.Path + "/" + destination.Name)
}
}
}
return nil
}