My agentic slop goes here. Not intended for anyone else!
1(** Example demonstrating keyeio library usage *)
2
3open Cmdliner
4
5(** Example 1: Basic usage with single service *)
6let basic_example (_xdg, _xdg_cmd) profile =
7 Fmt.pr "=== Basic Example ===@.";
8 Fmt.pr "Service: %s@." (Keyeio.Profile.service profile);
9 Fmt.pr "Profile: %s@." (Keyeio.Profile.name profile);
10
11 (* Get required API key *)
12 let api_key = Keyeio.Profile.get_required profile ~key:"api_key" in
13 Fmt.pr "API Key loaded: %s@." (String.sub api_key 0 (min 10 (String.length api_key)) ^ "...");
14
15 (* Get optional base URL *)
16 (match Keyeio.Profile.get profile ~key:"base_url" with
17 | Some url -> Fmt.pr "Base URL: %s@." url
18 | None -> Fmt.pr "No base URL configured@.");
19
20 (* List all available keys *)
21 let keys = Keyeio.Profile.keys profile in
22 Fmt.pr "Available keys: %a@." Fmt.(list ~sep:comma string) keys;
23
24 (* Pretty print the profile *)
25 Fmt.pr "@.Profile details:@.%a@." Keyeio.Profile.pp profile;
26 0
27
28(** Example 2: List all services *)
29let list_services_example (xdg, _xdg_cmd) =
30 Fmt.pr "=== List Services Example ===@.";
31
32 let keyeio = Keyeio.create xdg in
33
34 match Keyeio.list_services keyeio with
35 | Ok services ->
36 if services = [] then
37 Fmt.pr "No services configured yet@."
38 else begin
39 Fmt.pr "Available services:@.";
40 List.iter (fun svc -> Fmt.pr " - %s@." svc) services
41 end;
42 0
43 | Error (`Msg msg) ->
44 Fmt.epr "Error listing services: %s@." msg;
45 1
46
47(** Example 3: Load service and list profiles *)
48let list_profiles_example (xdg, _xdg_cmd) service_name =
49 Fmt.pr "=== List Profiles Example ===@.";
50
51 let keyeio = Keyeio.create xdg in
52
53 match Keyeio.load_service keyeio ~service:service_name with
54 | Ok service ->
55 Fmt.pr "Service: %s@." (Keyeio.Service.name service);
56
57 let profiles = Keyeio.Service.profile_names service in
58 Fmt.pr "Available profiles:@.";
59 List.iter (fun name ->
60 Fmt.pr " - %s" name;
61 match Keyeio.Service.get_profile service name with
62 | Some prof ->
63 let keys = Keyeio.Profile.keys prof in
64 Fmt.pr " (keys: %a)" Fmt.(list ~sep:comma string) keys
65 | None -> ()
66 ) profiles;
67 Fmt.pr "@.";
68
69 (* Pretty print the service *)
70 Fmt.pr "@.Service details:@.%a@." Keyeio.Service.pp service;
71 0
72 | Error (`Msg msg) ->
73 Fmt.epr "Error loading service '%s': %s@." service_name msg;
74 1
75
76(** Example 4: Simulated API client using loaded credentials *)
77let api_client_example (_xdg, _xdg_cmd) profile =
78 Fmt.pr "=== API Client Example ===@.";
79
80 (* Simulate creating an API client with loaded credentials *)
81 let api_key = Keyeio.Profile.get_required profile ~key:"api_key" in
82 let base_url = Keyeio.Profile.get profile ~key:"base_url"
83 |> Option.value ~default:"https://api.example.com" in
84
85 Fmt.pr "Creating API client for %s@." (Keyeio.Profile.service profile);
86 Fmt.pr " Base URL: %s@." base_url;
87 Fmt.pr " Profile: %s@." (Keyeio.Profile.name profile);
88 Fmt.pr " Authenticated: yes@.";
89
90 (* Simulate making an API call *)
91 Fmt.pr "@.Simulating API request...@.";
92 Fmt.pr "GET %s/api/status@." base_url;
93 Fmt.pr "Authorization: Bearer %s@." (String.sub api_key 0 (min 8 (String.length api_key)) ^ "...");
94 Fmt.pr "@.Response: 200 OK@.";
95 0
96
97(** Main command dispatcher *)
98let () =
99 Eio_main.run @@ fun env ->
100
101 (* Common terms *)
102 let xdg_term = Xdge.Cmd.term "keyeio-example" env#fs () in
103
104 (* Command: basic - Basic usage example *)
105 let basic_cmd =
106 let profile_term = Keyeio.Cmd.term
107 ~app_name:"keyeio-example"
108 ~fs:env#fs
109 ~service:"immiche"
110 () in
111 let info = Cmd.info "basic" ~doc:"Basic keyeio usage example" in
112 Cmd.v info Term.(const basic_example $ xdg_term $ profile_term)
113 in
114
115 (* Command: list - List all services *)
116 let list_cmd =
117 let info = Cmd.info "list" ~doc:"List all configured services" in
118 Cmd.v info Term.(const list_services_example $ xdg_term)
119 in
120
121 (* Command: profiles - List profiles for a service *)
122 let profiles_cmd =
123 let service_arg =
124 let doc = "Service name to inspect" in
125 Arg.(required & pos 0 (some string) None & info [] ~docv:"SERVICE" ~doc)
126 in
127 let info = Cmd.info "profiles" ~doc:"List profiles for a service" in
128 Cmd.v info Term.(const list_profiles_example $ xdg_term $ service_arg)
129 in
130
131 (* Command: client - API client simulation *)
132 let client_cmd =
133 let profile_term = Keyeio.Cmd.term
134 ~app_name:"keyeio-example"
135 ~fs:env#fs
136 ~service:"immiche"
137 ~profile:"default"
138 () in
139 let info = Cmd.info "client" ~doc:"Simulate API client with credentials" in
140 Cmd.v info Term.(const api_client_example $ xdg_term $ profile_term)
141 in
142
143 (* Command: init - Create a keyfile *)
144 let init_cmd =
145 let default_data = [
146 ("api_key", None); (* Will prompt if not provided *)
147 ("base_url", Some "https://immich.example.com") (* Has default *)
148 ] in
149 let init_term = Keyeio.Cmd.create_term
150 ~app_name:"keyeio-example"
151 ~fs:env#fs
152 ~service:"immiche"
153 ~default_data
154 ()
155 in
156 let info = Cmd.info "init" ~doc:"Create keyeio credentials" in
157 Cmd.v info init_term
158 in
159
160 (* Main command group *)
161 let main_cmd =
162 let info = Cmd.info "keyeio-example"
163 ~version:"0.1.0"
164 ~doc:"Examples demonstrating keyeio library usage"
165 ~man:[
166 `S Manpage.s_description;
167 `P "This program demonstrates various usage patterns for the keyeio library.";
168 `P "Keyeio provides secure API key storage using XDG directories with support for multiple profiles per service.";
169 `S "EXAMPLES";
170 `P "List all configured services:";
171 `Pre " $(b,keyeio-example list)";
172 `P "Show profiles for a service:";
173 `Pre " $(b,keyeio-example profiles immiche)";
174 `P "Basic usage with default profile:";
175 `Pre " $(b,keyeio-example basic)";
176 `P "Use a specific profile:";
177 `Pre " $(b,keyeio-example basic --profile production)";
178 `P "Simulate an API client:";
179 `Pre " $(b,keyeio-example client --profile staging)";
180 ]
181 in
182 Cmd.group info [init_cmd; basic_cmd; list_cmd; profiles_cmd; client_cmd]
183 in
184
185 exit (Cmd.eval' main_cmd)