-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExportComputerExtensionAttributes.go
123 lines (101 loc) · 4.09 KB
/
ExportComputerExtensionAttributes.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/http_client"
"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/jamfpro"
)
// path to export the scripts
const exportPath = "/Users/dafyddwatkins/GitHub/deploymenttheory/go-api-sdk-jamfpro/examples/support_files/export"
func sanitizeFileName(fileName string) string {
// Remove any characters that are not letters, numbers, hyphens, or underscores
reg := regexp.MustCompile("[^a-zA-Z0-9-_]+")
sanitized := reg.ReplaceAllString(fileName, "_")
return strings.Trim(sanitized, "_")
}
func main() {
// Define the path to the JSON configuration file inside the main function
configFilePath := "/Users/dafyddwatkins/GitHub/deploymenttheory/go-api-sdk-jamfpro/clientauth.json"
// Load the client OAuth credentials from the configuration file
authConfig, err := jamfpro.LoadClientAuthConfig(configFilePath)
if err != nil {
log.Fatalf("Failed to load client OAuth configuration: %v", err)
}
// Instantiate the default logger and set the desired log level
logger := http_client.NewDefaultLogger()
logLevel := http_client.LogLevelDebug // LogLevelNone // LogLevelWarning // LogLevelInfo // LogLevelDebug
// Configuration for the jamfpro
config := jamfpro.Config{
InstanceName: authConfig.InstanceName,
LogLevel: logLevel,
Logger: logger,
ClientID: authConfig.ClientID,
ClientSecret: authConfig.ClientSecret,
}
// Create a new jamfpro client instance
client, err := jamfpro.NewClient(config)
if err != nil {
log.Fatalf("Failed to create Jamf Pro client: %v", err)
}
// Ensure that the export directory exists
if _, err := os.Stat(exportPath); os.IsNotExist(err) {
err = os.MkdirAll(exportPath, 0755)
if err != nil {
log.Fatalf("Failed to create export directory: %v", err)
}
}
// Get a list of all computer extension attributes
attributesList, err := client.GetComputerExtensionAttributes()
if err != nil {
log.Fatalf("Failed to fetch Computer Extension Attributes: %v", err)
}
for _, attributeItem := range attributesList.Results {
// Log to indicate which extension attribute is being processed
log.Printf("Processing extension attribute '%s' (ID: %d)\n", attributeItem.Name, attributeItem.ID)
// Get details for each attribute
attribute, err := client.GetComputerExtensionAttributeByID(attributeItem.ID)
if err != nil {
log.Printf("Failed to fetch details for attribute %s (ID: %d): %v", attributeItem.Name, attributeItem.ID, err)
continue
}
// Log the type of the attribute for debugging
log.Printf("Attribute '%s' is of type '%s'\n", attribute.Name, attribute.InputType.Type)
// Check if the type is "Script"
if strings.ToLower(attribute.InputType.Type) == "script" {
// Sanitize the attribute name to be used as a filename
sanitizedFileName := sanitizeFileName(attribute.Name)
// Export the script content
scriptFileName := filepath.Join(exportPath, sanitizedFileName+".sh")
// Log to indicate where the script intends to export the extension attribute
log.Printf("Exporting computer extension attribute '%s' to: %s", attribute.Name, scriptFileName)
// Check if file already exists
if _, err := os.Stat(scriptFileName); err == nil {
log.Printf("File %s already exists!", scriptFileName)
} else if !os.IsNotExist(err) {
log.Printf("Error checking if file exists: %v", err)
}
// Use os.Create to create or truncate the file for writing
file, err := os.Create(scriptFileName)
if err != nil {
log.Printf("Failed to create file for attribute '%s': %v", attribute.Name, err)
continue
}
defer file.Close()
_, err = file.WriteString(attribute.InputType.Script)
if err != nil {
log.Printf("Failed to write script for attribute '%s' to file '%s': %v", attribute.Name, scriptFileName, err)
continue
}
fmt.Printf("Exported script for attribute '%s' to file '%s'\n", attribute.Name, scriptFileName)
} else {
log.Printf("Attribute '%s' is not of type 'Script', skipping export.\n", attribute.Name)
}
if err != nil {
log.Printf("Error encountered: %v", err)
}
}
}