template / files / internal / core / profile.go
profile.go
37160b89 2026-05-01 Pavel Safronov | 1 | package core |
d026838a 2022-09-22 Pavel Safronov | 2 | |
| 3 | import ( | |
bbd011d0 2022-12-10 Pavel Safronov | 4 | "bufio" |
ae89a42a 2022-11-02 Pavel Safronov | 5 | "bytes" |
6543e947 2022-12-29 Pavel Safronov | 6 | "errors" |
d026838a 2022-09-22 Pavel Safronov | 7 | "fmt" |
| 8 | "os" | |
05f9e8ef 2022-10-06 Pavel Safronov | 9 | "os/user" |
319b8b3f 2022-09-22 Pavel Safronov | 10 | "path/filepath" |
2bc4bd60 2022-09-23 Pavel Safronov | 11 | "runtime" |
230da3be 2022-09-22 Pavel Safronov | 12 | "strings" |
| 13 | "time" | |
763c260d 2022-10-06 Pavel Safronov | 14 | |
37160b89 2026-05-01 Pavel Safronov | 15 | "slices" |
763c260d 2022-10-06 Pavel Safronov | 16 | ) |
| 17 | ||
| 18 | type ProfileType string | |
| 19 | ||
| 20 | const ( | |
| 21 | FileType ProfileType = "file" | |
| 22 | FolderType ProfileType = "folder" | |
| 23 | ScriptType ProfileType = "script" | |
| 24 | ) | |
| 25 | ||
873846b9 2022-12-09 Pavel Safronov | 26 | var allProfileTypes = []ProfileType{ |
| 27 | FileType, | |
| 28 | FolderType, | |
| 29 | ScriptType, | |
| 30 | } | |
| 31 | ||
763c260d 2022-10-06 Pavel Safronov | 32 | type OSType string |
| 33 | ||
| 34 | const ( | |
| 35 | Windows OSType = "Windows" | |
| 36 | Mac OSType = "Mac" | |
| 37 | All OSType = "All" | |
d026838a 2022-09-22 Pavel Safronov | 38 | ) |
| 39 | ||
1a02ef4e 2022-10-21 Pavel Safronov | 40 | type ParameterizedText string |
| 41 | ||
5e7795dc 2022-10-21 Pavel Safronov | 42 | type FileInFolder struct { |
873846b9 2022-12-09 Pavel Safronov | 43 | Name ParameterizedText |
| 44 | FileTemplate ParameterizedText `yaml:"file,omitempty"` | |
5e7795dc 2022-10-21 Pavel Safronov | 45 | } |
| 46 | ||
b0586433 2022-10-21 Pavel Safronov | 47 | type Profile struct { |
687d5626 2022-10-28 Pavel Safronov | 48 | Name string |
e86c26ae 2022-11-02 Pavel Safronov | 49 | Description string `yaml:"description,omitempty"` |
51fece6b 2026-05-01 Pavel Safronov | 50 | IsDefault bool `yaml:"isdefault,omitempty"` |
| 51 | IsDisabled bool `yaml:"isdisabled,omitempty"` | |
687d5626 2022-10-28 Pavel Safronov | 52 | Type ProfileType |
873846b9 2022-12-09 Pavel Safronov | 53 | OS OSType `yaml:"os,omitempty"` |
e86c26ae 2022-11-02 Pavel Safronov | 54 | RunPostscriptOnFailure bool `yaml:"runpostscriptonfailure,omitempty"` |
| 55 | PostScripts []ParameterizedText `yaml:"postscripts,omitempty"` | |
873846b9 2022-12-09 Pavel Safronov | 56 | |
| 57 | NamePatterns []ParameterizedText | |
| 58 | ||
| 59 | TargetDirectory string `yaml:"targetdirectory,omitempty"` | |
| 60 | FileTemplate ParameterizedText `yaml:"filetemplate,omitempty"` | |
| 61 | Files []FileInFolder `yaml:"files,omitempty"` | |
763c260d 2022-10-06 Pavel Safronov | 62 | } |
| 63 | ||
| 64 | var forbiddenProfileNames = []string{ | |
| 65 | "list", | |
| 66 | "get", | |
| 67 | } | |
| 68 | ||
bbd011d0 2022-12-10 Pavel Safronov | 69 | func processProfile(profile Profile, projectName string, extras map[string]string, usr user.User, pipeInputScanner *bufio.Scanner) []error { |
10676da3 2022-11-06 Pavel Safronov | 70 | if profile.IsDisabled { |
9573ca02 2022-11-16 Pavel Safronov | 71 | return []error{fmt.Errorf("Profile is disabled, doing nothing")} |
10676da3 2022-11-06 Pavel Safronov | 72 | } |
| 73 | ||
da15cf49 2022-09-23 Pavel Safronov | 74 | // decide if the profile should run on this OS |
2bc4bd60 2022-09-23 Pavel Safronov | 75 | profileOs := profile.OS |
| 76 | isWindows := runtime.GOOS == "windows" | |
b65fb5e9 2022-09-23 Pavel Safronov | 77 | shouldRun := profileOs == "" || |
| 78 | profileOs == "All" || | |
2bc4bd60 2022-09-23 Pavel Safronov | 79 | (profileOs == "Windows" && isWindows) || |
| 80 | (profileOs == "Mac" && !isWindows) | |
| 81 | if !shouldRun { | |
9573ca02 2022-11-16 Pavel Safronov | 82 | return []error{ |
873846b9 2022-12-09 Pavel Safronov | 83 | fmt.Errorf("this profile is not supported on this OS, doing nothing. Profile OS: %s, system OS: %s. Supported OS values: Windows, Mac, All", profileOs, runtime.GOOS), |
9573ca02 2022-11-16 Pavel Safronov | 84 | } |
2bc4bd60 2022-09-23 Pavel Safronov | 85 | } |
| 86 | ||
873846b9 2022-12-09 Pavel Safronov | 87 | // determine if we're dealing with files or directories |
| 88 | profileType := profile.Type | |
| 89 | ||
c3fc7a22 2022-10-30 Pavel Safronov | 90 | cwd, err := monkey.OSGetwd() |
bbd011d0 2022-12-10 Pavel Safronov | 91 | if err != nil { |
687d5626 2022-10-28 Pavel Safronov | 92 | return []error{fmt.Errorf("unable to get current working directory: %v", err)} |
da15cf49 2022-09-23 Pavel Safronov | 93 | } |
873846b9 2022-12-09 Pavel Safronov | 94 | if profile.TargetDirectory != "" { |
2ef800f4 2023-01-09 Pavel Safronov | 95 | path := getFullPath(profile.TargetDirectory, usr) |
| 96 | ||
| 97 | if _, err := monkey.OSStat(path); errors.Is(err, os.ErrNotExist) { | |
| 98 | err = monkey.OSMkdir(path, 0755) | |
6543e947 2022-12-29 Pavel Safronov | 99 | if err != nil { |
| 100 | return []error{fmt.Errorf("unable to create target directory: %v", err)} | |
| 101 | } | |
| 102 | } | |
| 103 | ||
da15cf49 2022-09-23 Pavel Safronov | 104 | // change cwd to the profile's path, if one is specified |
c3fc7a22 2022-10-30 Pavel Safronov | 105 | err = monkey.OSChdir(path) |
da15cf49 2022-09-23 Pavel Safronov | 106 | if err != nil { |
687d5626 2022-10-28 Pavel Safronov | 107 | return []error{fmt.Errorf("unable to change directory (1) to %v: %v", path, err)} |
da15cf49 2022-09-23 Pavel Safronov | 108 | } |
| 109 | cwd = path | |
319b8b3f 2022-09-22 Pavel Safronov | 110 | } |
| 111 | ||
687d5626 2022-10-28 Pavel Safronov | 112 | errors := []error{} |
8c15a714 2022-11-04 Pavel Safronov | 113 | fullPath := "" |
| 114 | now := time.Now() | |
| 115 | prePatternData := createPrePatternData(now, projectName, extras) | |
873846b9 2022-12-09 Pavel Safronov | 116 | |
8c15a714 2022-11-04 Pavel Safronov | 117 | if profileType == FileType || profileType == FolderType { |
bbd011d0 2022-12-10 Pavel Safronov | 118 | if pipeInputScanner != nil && profileType != FileType { |
| 119 | errors = append(errors, fmt.Errorf("cannot use pipe input with non-file type profile")) | |
| 120 | return errors | |
| 121 | } | |
| 122 | ||
8c15a714 2022-11-04 Pavel Safronov | 123 | // get all entities in current directory |
| 124 | files, err := monkey.OSReadDir(cwd) | |
| 125 | if err != nil { | |
| 126 | return []error{err} | |
| 127 | } | |
319b8b3f 2022-09-22 Pavel Safronov | 128 | |
8c15a714 2022-11-04 Pavel Safronov | 129 | // filter entities to just files or directories |
| 130 | var entities []string | |
| 131 | for _, file := range files { | |
| 132 | isDir := file.IsDir() | |
| 133 | if isDir == (profileType == FolderType) { | |
| 134 | entities = append(entities, file.Name()) | |
| 135 | } | |
687d5626 2022-10-28 Pavel Safronov | 136 | } |
2bc4bd60 2022-09-23 Pavel Safronov | 137 | |
8c15a714 2022-11-04 Pavel Safronov | 138 | // generate a new name based on time, profile, and existing entities |
10676da3 2022-11-06 Pavel Safronov | 139 | validator := func(newName string) bool { return !slices.Contains(entities, newName) } |
| 140 | newName, err := createNewNameForPatterns(now, prePatternData, projectName, profile.NamePatterns, extras, validator) | |
8c15a714 2022-11-04 Pavel Safronov | 141 | swallowPrePostScriptErrors := profile.RunPostscriptOnFailure && profile.PostScripts != nil && len(profile.PostScripts) > 0 |
| 142 | ||
| 143 | // check newName is created | |
| 144 | if newName == "" { | |
e86c26ae 2022-11-02 Pavel Safronov | 145 | if swallowPrePostScriptErrors { |
8c15a714 2022-11-04 Pavel Safronov | 146 | fmt.Println("No new name was created, but will attempt to run post scripts") |
e86c26ae 2022-11-02 Pavel Safronov | 147 | } else { |
8c15a714 2022-11-04 Pavel Safronov | 148 | errors = append(errors, fmt.Errorf("couldn't find a good new name")) |
687d5626 2022-10-28 Pavel Safronov | 149 | return errors |
fe139e41 2022-09-25 Pavel Safronov | 150 | } |
5e7795dc 2022-10-21 Pavel Safronov | 151 | } |
8c15a714 2022-11-04 Pavel Safronov | 152 | |
| 153 | if err == nil && newName != "" { | |
bbd011d0 2022-12-10 Pavel Safronov | 154 | fullPath, err = createEntity(now, profile, cwd, newName, projectName, extras, prePatternData, pipeInputScanner) |
8c15a714 2022-11-04 Pavel Safronov | 155 | if err != nil { |
| 156 | if swallowPrePostScriptErrors { | |
| 157 | fmt.Printf("Error creating entity, but will attempt to run post scripts: %v", err) | |
| 158 | } else { | |
| 159 | errors = append(errors, fmt.Errorf("unable to create entity: %v", err)) | |
| 160 | return errors | |
| 161 | } | |
| 162 | } | |
| 163 | } | |
319b8b3f 2022-09-22 Pavel Safronov | 164 | } |
| 165 | ||
| 166 | // execute PostScript, if present | |
83f12389 2026-01-15 Pavel Safronov | 167 | if len(profile.PostScripts) > 0 { |
9573ca02 2022-11-16 Pavel Safronov | 168 | postPatternData := createPostPatternData(fullPath, prePatternData) |
| 169 | postScripts, err := createPostScriptArrays(profile, postPatternData) | |
ae89a42a 2022-11-02 Pavel Safronov | 170 | if err != nil { |
9573ca02 2022-11-16 Pavel Safronov | 171 | errors = append(errors, fmt.Errorf("unable to create post scripts: %v", err)) |
| 172 | return errors | |
| 173 | } | |
| 174 | for _, postScript := range postScripts { | |
| 175 | ||
| 176 | shell, args := shellCommandAndArgs(postScript) | |
| 177 | var out bytes.Buffer | |
| 178 | fmt.Printf("Executing post script: %v %v\n", shell, strings.Join(args, " ")) | |
| 179 | cmd := monkey.ExecCommand(shell, args...) | |
| 180 | cmd.Stdout = &out | |
| 181 | err := cmd.Run() | |
| 182 | output := out.String() | |
| 183 | if output != "" { | |
| 184 | fmt.Printf("Post script output: %v\n", output) | |
| 185 | } else { | |
| 186 | fmt.Printf("Post script had no output\n") | |
| 187 | } | |
| 188 | if err != nil { | |
| 189 | // post scripts can fail (TODO: investigate why), so for now we'll just log the error | |
2bc4bd60 2022-09-23 Pavel Safronov | 190 | |
9573ca02 2022-11-16 Pavel Safronov | 191 | fmt.Printf("Error executing post script '%v': %v\n", |
| 192 | fmt.Sprintf("%v %v", shell, strings.Trim(fmt.Sprint(args), "[]")), err) | |
| 193 | } | |
319b8b3f 2022-09-22 Pavel Safronov | 194 | } |
| 195 | } | |
| 196 | ||
| 197 | return nil | |
| 198 | } | |
763c260d 2022-10-06 Pavel Safronov | 199 | |
52e9ad7c 2022-11-08 Pavel Safronov | 200 | func shellCommandAndArgs(command string) (shell string, args []string) { |
| 201 | if runtime.GOOS == "windows" { | |
| 202 | shell = "cmd" | |
| 203 | args = []string{"/c", command} | |
| 204 | return | |
| 205 | } else { | |
| 206 | shell = "sh" | |
| 207 | args = []string{"-c", command} | |
| 208 | return | |
| 209 | } | |
| 210 | } | |
| 211 | ||
687d5626 2022-10-28 Pavel Safronov | 212 | func createEntity( |
| 213 | now time.Time, | |
| 214 | profile Profile, | |
| 215 | cwd string, | |
| 216 | newName string, | |
| 217 | projectName string, | |
| 218 | extras map[string]string, | |
bbd011d0 2022-12-10 Pavel Safronov | 219 | prePatternData PrePatternData, |
| 220 | pipeInputScanner *bufio.Scanner) (string, error) { | |
687d5626 2022-10-28 Pavel Safronov | 221 | |
c3fc7a22 2022-10-30 Pavel Safronov | 222 | profileType := profile.Type |
687d5626 2022-10-28 Pavel Safronov | 223 | fullPath := filepath.Join(cwd, newName) |
83f12389 2026-01-15 Pavel Safronov | 224 | switch profileType { |
| 225 | case FolderType: | |
e86c26ae 2022-11-02 Pavel Safronov | 226 | fmt.Printf("Creating folder: %v\n", fullPath) |
c3fc7a22 2022-10-30 Pavel Safronov | 227 | err := monkey.OSMkdir(fullPath, os.ModePerm) |
687d5626 2022-10-28 Pavel Safronov | 228 | if err != nil { |
| 229 | return "", fmt.Errorf("error creating directory: %v", err) | |
| 230 | } | |
c3fc7a22 2022-10-30 Pavel Safronov | 231 | err = monkey.OSChdir(fullPath) |
687d5626 2022-10-28 Pavel Safronov | 232 | if err != nil { |
| 233 | return "", fmt.Errorf("unable to change directory (2) to %v: %v", fullPath, err) | |
| 234 | } | |
| 235 | ||
| 236 | // create files in the new folder | |
| 237 | if len(profile.Files) > 0 { | |
e86c26ae 2022-11-02 Pavel Safronov | 238 | fmt.Printf("Creating %v files in folder: %v\n", len(profile.Files), fullPath) |
10676da3 2022-11-06 Pavel Safronov | 239 | existingFiles := []string{} |
687d5626 2022-10-28 Pavel Safronov | 240 | for index, file := range profile.Files { |
8c15a714 2022-11-04 Pavel Safronov | 241 | filePrePatternData := createPrePatternData(now, projectName, extras) |
10676da3 2022-11-06 Pavel Safronov | 242 | validator := func(newName string) bool { return !slices.Contains(existingFiles, newName) } |
| 243 | fileName, err := createNewNameForPatterns(now, filePrePatternData, projectName, []ParameterizedText{file.Name}, extras, validator) | |
687d5626 2022-10-28 Pavel Safronov | 244 | if err != nil { |
| 245 | return "", fmt.Errorf("error creating file name for file index %v : %v", index, err) | |
| 246 | } | |
10676da3 2022-11-06 Pavel Safronov | 247 | existingFiles = append(existingFiles, fileName) |
687d5626 2022-10-28 Pavel Safronov | 248 | subFilePath := filepath.Join(fullPath, fileName) |
5e424f1b 2022-11-12 Pavel Safronov | 249 | subDirPath := filepath.Dir(subFilePath) |
| 250 | ||
| 251 | if _, err := os.Stat(subDirPath); os.IsNotExist(err) { | |
| 252 | fmt.Printf("Creating subdirectory: %v\n", subDirPath) | |
| 253 | err = monkey.OSMkdir(subDirPath, os.ModePerm) | |
| 254 | if err != nil { | |
| 255 | return "", fmt.Errorf("error creating sub directory for file index %v : %v", index, err) | |
| 256 | } | |
| 257 | } | |
e86c26ae 2022-11-02 Pavel Safronov | 258 | fmt.Printf("Creating sub file: %v\n", subFilePath) |
bbd011d0 2022-12-10 Pavel Safronov | 259 | err = createFile(filePrePatternData, file.FileTemplate, subFilePath, nil) |
687d5626 2022-10-28 Pavel Safronov | 260 | if err != nil { |
886f7fa2 2022-11-02 Pavel Safronov | 261 | return "", fmt.Errorf("error creating file %v: %v", subFilePath, err) |
687d5626 2022-10-28 Pavel Safronov | 262 | } |
| 263 | } | |
| 264 | } | |
83f12389 2026-01-15 Pavel Safronov | 265 | case FileType: |
e86c26ae 2022-11-02 Pavel Safronov | 266 | fmt.Printf("Creating file: %v\n", fullPath) |
bbd011d0 2022-12-10 Pavel Safronov | 267 | err := createFile(prePatternData, profile.FileTemplate, fullPath, pipeInputScanner) |
5e7795dc 2022-10-21 Pavel Safronov | 268 | if err != nil { |
687d5626 2022-10-28 Pavel Safronov | 269 | return "", err |
5e7795dc 2022-10-21 Pavel Safronov | 270 | } |
83f12389 2026-01-15 Pavel Safronov | 271 | default: |
e86c26ae 2022-11-02 Pavel Safronov | 272 | fmt.Printf("Profile type is script, doing nothing during main phase\n") |
5e7795dc 2022-10-21 Pavel Safronov | 273 | } |
687d5626 2022-10-28 Pavel Safronov | 274 | return fullPath, nil |
5e7795dc 2022-10-21 Pavel Safronov | 275 | } |
| 276 | ||
b0586433 2022-10-21 Pavel Safronov | 277 | func validateProfile(profile *Profile) error { |
873846b9 2022-12-09 Pavel Safronov | 278 | // check name |
763c260d 2022-10-06 Pavel Safronov | 279 | if profile.Name == "" { |
873846b9 2022-12-09 Pavel Safronov | 280 | return fmt.Errorf("profile name is required") |
763c260d 2022-10-06 Pavel Safronov | 281 | } |
| 282 | if slices.Contains(forbiddenProfileNames, profile.Name) { | |
| 283 | return fmt.Errorf("forbidden profile name '%s'", profile.Name) | |
| 284 | } | |
873846b9 2022-12-09 Pavel Safronov | 285 | |
| 286 | // check name patterns | |
83f12389 2026-01-15 Pavel Safronov | 287 | if len(profile.NamePatterns) == 0 { |
10676da3 2022-11-06 Pavel Safronov | 288 | return fmt.Errorf("namePatterns is required") |
| 289 | } | |
| 290 | for index, namePattern := range profile.NamePatterns { | |
| 291 | if namePattern == "" { | |
| 292 | return fmt.Errorf("name pattern is required for pattern index %v of profile %v", index, profile.Name) | |
| 293 | } | |
| 294 | } | |
873846b9 2022-12-09 Pavel Safronov | 295 | |
| 296 | // check type | |
| 297 | if profile.Type == "" || !slices.Contains(allProfileTypes, profile.Type) { | |
763c260d 2022-10-06 Pavel Safronov | 298 | return fmt.Errorf("invalid profile type %s for profile %s", profile.Type, profile.Name) |
| 299 | } | |
873846b9 2022-12-09 Pavel Safronov | 300 | |
| 301 | // check FileType profile | |
| 302 | if profile.Type == FileType { | |
| 303 | if profile.TargetDirectory == "" { | |
| 304 | return fmt.Errorf("targetDirectory is not specified for profile %s, which has Type=%s", profile.Name, profile.Type) | |
| 305 | } | |
05f9e8ef 2022-10-06 Pavel Safronov | 306 | } |
873846b9 2022-12-09 Pavel Safronov | 307 | |
| 308 | // check FolderType profile | |
| 309 | if profile.Type == FolderType { | |
| 310 | if profile.TargetDirectory == "" { | |
| 311 | return fmt.Errorf("targetDirectory is not specified for profile %s, which has Type=%s", profile.Name, profile.Type) | |
| 312 | } | |
8c15a714 2022-11-04 Pavel Safronov | 313 | } |
873846b9 2022-12-09 Pavel Safronov | 314 | |
| 315 | // check ScriptType profile | |
| 316 | if profile.Type == ScriptType { | |
83f12389 2026-01-15 Pavel Safronov | 317 | if len(profile.NamePatterns) > 1 { |
873846b9 2022-12-09 Pavel Safronov | 318 | return fmt.Errorf("multiple name patterns are not supported for profile %s, which has Type=%s", profile.Name, profile.Type) |
| 319 | } | |
8c15a714 2022-11-04 Pavel Safronov | 320 | } |
873846b9 2022-12-09 Pavel Safronov | 321 | |
| 322 | // set files to empty array if nil | |
b0586433 2022-10-21 Pavel Safronov | 323 | if profile.Files == nil { |
5e7795dc 2022-10-21 Pavel Safronov | 324 | profile.Files = []FileInFolder{} |
b0586433 2022-10-21 Pavel Safronov | 325 | } |
873846b9 2022-12-09 Pavel Safronov | 326 | |
763c260d 2022-10-06 Pavel Safronov | 327 | return nil |
| 328 | } |