-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfluent.go
More file actions
193 lines (162 loc) · 5.09 KB
/
Copy pathfluent.go
File metadata and controls
193 lines (162 loc) · 5.09 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright Epic Games, Inc. All Rights Reserved.
//go:generate go run github.com/EpicGames/lore-go/cmd/fetch-lore-lib
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"github.com/EpicGames/lore-go"
"github.com/EpicGames/lore-go/types"
)
// Configuration
const (
COMMIT_MESSAGE = "Initial commit"
LOG_FILE_PATH = "./LoreRepositories"
)
// generateID generates a random hex string for unique repository names
func generateID() string {
b := make([]byte, 16)
rand.Read(b)
return hex.EncodeToString(b)
}
var (
REPOSITORY_NAME = "EpicRepo" + generateID()
REPOSITORY_PATH = filepath.Join("./LoreRepositories", REPOSITORY_NAME)
)
// globalLogHandler handles LOG events for all Lore operations
func globalLogHandler(event *types.LoreEventFFI, userContext uint64) {
if logEvent, ok := event.GetData().(*types.LoreLogEventDataFFI); ok {
if logEvent.Level > types.LoreLogLevel_DEBUG {
fmt.Println(logEvent.Message)
}
}
}
// createFiles generates files to commit to repository
func createFiles() error {
files := []string{
filepath.Join(REPOSITORY_PATH, "file.txt"),
filepath.Join(REPOSITORY_PATH, "log.txt"),
}
content := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et"
for _, file := range files {
if err := os.WriteFile(file, []byte(content), 0644); err != nil {
return err
}
}
return nil
}
// verifyResult checks the result and exits on failure
func verifyResult(operationName string, result int32, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if result != 0 {
fmt.Printf("Lore %s failed.\n", operationName)
os.Exit(1)
}
fmt.Printf("Lore %s success.\n", operationName)
}
func main() {
fmt.Println("Lore Fluent Example (purego)")
fmt.Println("===========================")
// If a remote URL is provided as the first CLI arg, run in online mode
// (push the revision and clone the repository back). Otherwise run a
// fully offline example that only creates a local repository and commits
// a file. Authentication is not handled by this example; if the remote
// requires it, run `lore auth` before invoking this program.
online := len(os.Args) > 1
remoteUrl := ""
if online {
remoteUrl = os.Args[1]
fmt.Printf("Running in online mode against: %s\n", remoteUrl)
} else {
fmt.Println("Running in offline mode (pass a remote URL as the first arg to enable push/clone)")
}
repositoryUrl := REPOSITORY_NAME
if online {
repositoryUrl = remoteUrl + "/" + REPOSITORY_NAME
}
// Register global log handler for all Lore operations
cleanupLogHandler := lore.GlobalCallback(types.LoreEventTag_LOG, globalLogHandler)
defer cleanupLogHandler()
// Configure logging
logConfig, cleanupLogConfig := types.NewLoreLogConfig(types.LoreLogConfig{
File: true,
FilePath: LOG_FILE_PATH,
Level: types.LoreLogLevel_DEBUG,
})
defer cleanupLogConfig()
result, err := lore.LogConfigure(&logConfig)
verifyResult("Setup", result, err)
// Set up global args
globals, cleanupGlobals := types.NewLoreGlobalArgs(types.LoreGlobalArgs{
RepositoryPath: REPOSITORY_PATH,
Offline: !online,
})
defer cleanupGlobals()
// Create repository
{
repoArgs, cleanupRepo := types.NewLoreRepositoryCreateArgs(types.LoreRepositoryCreateArgs{
RepositoryUrl: repositoryUrl,
})
defer cleanupRepo()
result, err := lore.RepositoryCreate(&globals, &repoArgs).Wait()
verifyResult("Repo Create", result, err)
}
// Create files to commit to the new repository
if err := createFiles(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to create files: %v\n", err)
os.Exit(1)
}
// Stage files
{
paths := []string{
filepath.Join(REPOSITORY_PATH, "file.txt"),
filepath.Join(REPOSITORY_PATH, "log.txt"),
}
stageArgs, cleanupStage := types.NewLoreFileStageArgs(types.LoreFileStageArgs{
Paths: paths,
})
defer cleanupStage()
result, err := lore.FileStage(&globals, &stageArgs).Wait()
verifyResult("File Stage", result, err)
}
// Revision commit
{
commitArgs, cleanupCommit := types.NewLoreRevisionCommitArgs(types.LoreRevisionCommitArgs{
Message: COMMIT_MESSAGE,
})
defer cleanupCommit()
result, err := lore.RevisionCommit(&globals, &commitArgs).Wait()
verifyResult("Revision Commit", result, err)
}
if online {
// Branch push
{
pushArgs, cleanupPush := types.NewLoreBranchPushArgs(types.LoreBranchPushArgs{})
defer cleanupPush()
result, err := lore.BranchPush(&globals, &pushArgs).Wait()
verifyResult("Branch Push", result, err)
}
// Clone repository
{
clonePath := REPOSITORY_PATH + "_clone"
globalsClone, cleanupGlobalsClone := types.NewLoreGlobalArgs(types.LoreGlobalArgs{
RepositoryPath: clonePath,
})
defer cleanupGlobalsClone()
cloneArgs, cleanupClone := types.NewLoreRepositoryCloneArgs(types.LoreRepositoryCloneArgs{
RepositoryUrl: repositoryUrl,
})
defer cleanupClone()
result, err := lore.RepositoryClone(&globalsClone, &cloneArgs).Wait()
verifyResult("Repository Clone", result, err)
}
}
// Shut down the library
result, err = lore.Shutdown()
verifyResult("Shutdown", result, err)
}