╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 //! start a server with some fake data and then launch 2 //! the TUI. this provides a nice way to test things 3 //! out safely. 4 5 const std = @import("std"); 6 const hx = @import("haxy"); 7 const srv = hx.serve; 8 const evt = hx.event; 9 const xit = hx.xit; 10 const rp = xit.repo; 11 const ui = hx.ui; 12 13 pub fn main(init: std.process.Init) !void { 14 var gpa: std.heap.DebugAllocator(.{}) = .init; 15 defer _ = gpa.deinit(); 16 const allocator = gpa.allocator(); 17 18 var threaded = std.Io.Threaded.init(allocator, .{ 19 .environ = init.minimal.environ, 20 }); 21 defer threaded.deinit(); 22 const io = threaded.io(); 23 24 const temp_dir_name = "temp-try"; 25 26 // create the temp dir 27 const cwd = std.Io.Dir.cwd(); 28 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 29 if (temp_dir_or_err) |*temp_dir| { 30 temp_dir.close(io); 31 try cwd.deleteTree(io, temp_dir_name); 32 } else |_| {} 33 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 34 defer cwd.deleteTree(io, temp_dir_name) catch {}; 35 defer temp_dir.close(io); 36 37 const cwd_path = try std.process.currentPathAlloc(io, allocator); 38 defer allocator.free(cwd_path); 39 40 const work_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server", "admin" }); 41 defer allocator.free(work_path); 42 43 const Repo = rp.Repo(.xit, evt.admin_repo_opts); 44 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 45 defer repo.deinit(io, allocator); 46 47 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 48 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 49 50 var session_arena = std.heap.ArenaAllocator.init(allocator); 51 defer session_arena.deinit(); 52 53 var session: ui.Session = blk: { 54 var arena = std.heap.ArenaAllocator.init(allocator); 55 defer arena.deinit(); 56 57 // define test events 58 59 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 60 61 const user_data = [_]struct { 62 name: []const u8, 63 display_name: []const u8, 64 email: []const u8, 65 }{ 66 .{ .name = "admin", .display_name = "Admin", .email = "admin@example.test" }, 67 .{ .name = "alice", .display_name = "Alice Tulley", .email = "alice@example.test" }, 68 .{ .name = "bob", .display_name = "Bob Smith", .email = "bob@example.test" }, 69 .{ .name = "carol", .display_name = "Carol Johnson", .email = "carol@example.test" }, 70 .{ .name = "dave", .display_name = "Dave Wilson", .email = "dave@example.test" }, 71 .{ .name = "eve", .display_name = "Eve Anderson", .email = "eve@example.test" }, 72 .{ .name = "frank", .display_name = "Frank Miller", .email = "frank@example.test" }, 73 .{ .name = "grace", .display_name = "Grace Lee", .email = "grace@example.test" }, 74 .{ .name = "henry", .display_name = "Henry Davis", .email = "henry@example.test" }, 75 .{ .name = "ivy", .display_name = "Ivy Martinez", .email = "ivy@example.test" }, 76 .{ .name = "jack", .display_name = "Jack Thompson", .email = "jack@example.test" }, 77 .{ .name = "kate", .display_name = "Kate Robinson", .email = "kate@example.test" }, 78 .{ .name = "liam", .display_name = "Liam Walker", .email = "liam@example.test" }, 79 .{ .name = "mona", .display_name = "Mona Patel", .email = "mona@example.test" }, 80 .{ .name = "noah", .display_name = "Noah Garcia", .email = "noah@example.test" }, 81 .{ .name = "olivia", .display_name = "Olivia Hernandez", .email = "olivia@example.test" }, 82 .{ .name = "peter", .display_name = "Peter Wright", .email = "peter@example.test" }, 83 .{ .name = "quinn", .display_name = "Quinn Foster", .email = "quinn@example.test" }, 84 .{ .name = "rachel", .display_name = "Rachel Bennett", .email = "rachel@example.test" }, 85 .{ .name = "sam", .display_name = "Sam Brooks", .email = "sam@example.test" }, 86 .{ .name = "tina", .display_name = "Tina Cooper", .email = "tina@example.test" }, 87 }; 88 89 const repo_data = [_]struct { 90 user_index: usize, 91 name: []const u8, 92 description: []const u8, 93 }{ 94 .{ .user_index = 0, .name = "ziglings", .description = "Learn the Zig programming language by fixing tiny broken programs" }, 95 .{ .user_index = 1, .name = "linux", .description = "Linux kernel source tree" }, 96 .{ .user_index = 2, .name = "kubernetes", .description = "Production-grade container orchestration" }, 97 .{ .user_index = 3, .name = "react", .description = "A declarative, efficient, and flexible JavaScript library for building user interfaces" }, 98 .{ .user_index = 4, .name = "typescript", .description = "TypeScript is a superset of JavaScript that compiles to clean JavaScript output" }, 99 .{ .user_index = 5, .name = "rust", .description = "Empowering everyone to build reliable and efficient software" }, 100 .{ .user_index = 6, .name = "go", .description = "The Go programming language" }, 101 .{ .user_index = 7, .name = "nodejs", .description = "Node.js JavaScript runtime" }, 102 .{ .user_index = 8, .name = "cpython", .description = "The Python programming language" }, 103 .{ .user_index = 9, .name = "docker", .description = "Container platform for developing, shipping, and running applications" }, 104 .{ .user_index = 0, .name = "vim", .description = "The ubiquitous text editor" }, 105 .{ .user_index = 1, .name = "neovim", .description = "Hyperextensible Vim-based text editor" }, 106 .{ .user_index = 2, .name = "emacs", .description = "GNU Emacs source code mirror" }, 107 .{ .user_index = 3, .name = "tmux", .description = "Terminal multiplexer" }, 108 .{ .user_index = 4, .name = "zsh", .description = "Mirror of the Z shell source code repository" }, 109 .{ .user_index = 5, .name = "git", .description = "Distributed version control system" }, 110 .{ .user_index = 6, .name = "mercurial", .description = "Source-control management tool" }, 111 .{ .user_index = 7, .name = "tensorflow", .description = "An end-to-end open source machine learning platform" }, 112 .{ .user_index = 8, .name = "pytorch", .description = "Tensors and dynamic neural networks in Python with strong GPU acceleration" }, 113 .{ .user_index = 9, .name = "numpy", .description = "The fundamental package for scientific computing with Python" }, 114 .{ .user_index = 0, .name = "pandas", .description = "Flexible and powerful data analysis and manipulation library for Python" }, 115 .{ .user_index = 1, .name = "scikit-learn", .description = "Machine learning in Python" }, 116 .{ .user_index = 2, .name = "nginx", .description = "High performance HTTP server and reverse proxy" }, 117 .{ .user_index = 3, .name = "redis", .description = "In-memory data structure store, used as a database, cache, and message broker" }, 118 .{ .user_index = 4, .name = "postgres", .description = "The world's most advanced open source relational database" }, 119 .{ .user_index = 5, .name = "sqlite", .description = "Self-contained, serverless, zero-configuration SQL database engine" }, 120 .{ .user_index = 6, .name = "mongodb", .description = "The MongoDB Database" }, 121 .{ .user_index = 7, .name = "elasticsearch", .description = "Free and open, distributed, RESTful search engine" }, 122 .{ .user_index = 8, .name = "kafka", .description = "Distributed event streaming platform" }, 123 .{ .user_index = 9, .name = "terraform", .description = "Infrastructure as code tool" }, 124 .{ .user_index = 10, .name = "svelte", .description = "Cybernetically enhanced web apps" }, 125 .{ .user_index = 11, .name = "vue", .description = "The progressive JavaScript framework" }, 126 .{ .user_index = 12, .name = "flask", .description = "The Python micro framework for building web applications" }, 127 .{ .user_index = 13, .name = "django", .description = "The web framework for perfectionists with deadlines" }, 128 .{ .user_index = 14, .name = "rails", .description = "Ruby on Rails web framework" }, 129 .{ .user_index = 15, .name = "phoenix", .description = "Peace of mind from prototype to production for Elixir web apps" }, 130 .{ .user_index = 16, .name = "laravel", .description = "The PHP framework for web artisans" }, 131 .{ .user_index = 17, .name = "prometheus", .description = "The Prometheus monitoring system and time series database" }, 132 .{ .user_index = 18, .name = "grafana", .description = "The open and composable observability and data visualization platform" }, 133 .{ .user_index = 19, .name = "ansible", .description = "Simple, agentless IT automation" }, 134 }; 135 136 var user_ids: [user_data.len][evt.event_id_size]u8 = undefined; 137 for (&user_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 138 139 var repo_event_ids: [repo_data.len][evt.event_id_size]u8 = undefined; 140 for (&repo_event_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 141 142 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 143 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 144 145 var events_to_consume: [user_data.len + repo_data.len]evt.EventWithId = undefined; 146 for (user_data, 0..) |u, i| { 147 events_to_consume[i] = .{ 148 .id = std.fmt.bytesToHex(user_ids[i], .lower), 149 .event = .{ 150 .user = .{ 151 .name = u.name, 152 .display_name = u.display_name, 153 .email = u.email, 154 .password_hash = password_hash, 155 }, 156 }, 157 }; 158 } 159 for (repo_data, 0..) |r, i| { 160 events_to_consume[user_data.len + i] = .{ 161 .id = std.fmt.bytesToHex(repo_event_ids[i], .lower), 162 .event = .{ 163 .repo = .{ 164 .user_id = &user_ids[r.user_index], 165 .name = r.name, 166 .description = r.description, 167 .enable_issue = true, 168 }, 169 }, 170 }; 171 } 172 173 // commit the seed events and consume them into the database 174 try evt.commitAndConsume(evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &events_to_consume); 175 176 // create the actual repos on disk, named by their hex-encoded event id 177 for (repo_data, 0..) |r, i| { 178 const repo_id = std.fmt.bytesToHex(repo_event_ids[i], .lower); 179 const repo_path = try std.fs.path.join(arena.allocator(), &.{ cwd_path, temp_dir_name, "server", "repos", &repo_id }); 180 181 var repo_i = try rp.Repo(.xit, .{}).init(io, allocator, .{ .path = repo_path }); 182 defer repo_i.deinit(io, allocator); 183 184 try repo_i.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 185 try repo_i.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 186 187 // write the repo's name and description into a README, plus a 188 // nested doc so the file tree has a directory to descend into 189 { 190 var repo_dir = try cwd.openDir(io, repo_path, .{}); 191 defer repo_dir.close(io); 192 193 const readme = try repo_dir.createFile(io, "README.md", .{}); 194 defer readme.close(io); 195 const readme_content = try std.fmt.allocPrint(arena.allocator(), "# {s}\n\n{s}\n", .{ r.name, r.description }); 196 try readme.writeStreamingAll(io, readme_content); 197 198 try repo_dir.createDirPath(io, "docs/dev"); 199 const doc = try repo_dir.createFile(io, "docs/dev/contribute.md", .{}); 200 defer doc.close(io); 201 try doc.writeStreamingAll(io, "To contribute, please make a pull request"); 202 } 203 204 try repo_i.add(io, allocator, &.{ "README.md", "docs/dev/contribute.md" }); 205 _ = try repo_i.commit(io, allocator, .{ .message = "let there be light" }); 206 207 // a batch of empty commits so the commits tab has more than one page 208 // to paginate through. stepped timestamps vary the date column. 209 const base_ts: u64 = 1_700_000_000; // 2023-11-14 210 var c: usize = 0; 211 while (c < 30) : (c += 1) { 212 const message = try std.fmt.allocPrint(arena.allocator(), "empty commit {d}", .{c + 1}); 213 _ = try repo_i.commit(io, allocator, .{ .message = message, .allow_empty = true, .timestamp = base_ts + c * std.time.s_per_day }); 214 } 215 } 216 217 break :blk try ui.Session.init(&session_arena, &repo, .{}); 218 }; 219 session.is_terminal = true; 220 221 // start the server 222 223 var cli = false; 224 225 var arg_it = try init.minimal.args.iterateAllocator(allocator); 226 defer arg_it.deinit(); 227 _ = arg_it.skip(); 228 while (arg_it.next()) |arg| { 229 if (std.mem.eql(u8, "--cli", arg)) { 230 cli = true; 231 } 232 } 233 234 const server_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }); 235 defer allocator.free(server_path); 236 237 // let the native TUI's page builders open the on-disk repos for the file tree 238 session.io = io; 239 session.repos_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "repos" }); 240 241 if (cli) { 242 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 243 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 244 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 245 246 const Runnable = struct { 247 io: std.Io, 248 249 pub fn run(self: @This()) !void { 250 std.debug.print( 251 \\ 252 \\connect to the TUI with: 253 \\ ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 254 \\ 255 \\create a git repo and push over SSH: 256 \\ mkdir -p temp-try/client/test 257 \\ cd temp-try/client/test 258 \\ git init 259 \\ echo "hello" > hello.txt 260 \\ git add hello.txt 261 \\ git commit -m "let there be light" 262 \\ GIT_SSH_COMMAND='ssh -p 8022 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' git push localhost:admin/test HEAD:master 263 \\ 264 \\to quit, press enter. 265 \\ 266 , .{}); 267 // portable stdin read via std.Io — equivalent to the 268 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 269 // on windows. 270 var buf: [1]u8 = undefined; 271 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 272 _ = stdin_reader.interface.takeByte() catch {}; 273 } 274 }; 275 276 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 277 .data_dir = server_path, 278 }, run_opts.err, Runnable{ .io = io }); 279 } else { 280 var null_writer = std.Io.Writer.Discarding.init(&.{}); 281 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 282 283 const Runnable = struct { 284 io: std.Io, 285 allocator: std.mem.Allocator, 286 session: *ui.Session, 287 288 pub fn run(self: @This()) !void { 289 // launch the TUI 290 try hx.ui.run(self.io, self.allocator, self.session); 291 } 292 }; 293 294 try srv.run(.xit, .{}, io, allocator, cwd_path, .{ 295 .data_dir = server_path, 296 }, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session }); 297 } 298 } 299