Use Bytes instead of String for mutable byte array.
[goaljobs.git] / goaljobs_memory.ml
1 (* goaljobs
2  * Copyright (C) 2013-2014 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Unix
20 open Printf
21
22 open Goaljobs
23
24 (* Stand-alone tool to read and modify the user's goaljobs memory. *)
25
26 let usage ret =
27     printf "  %s list\n" Sys.executable_name;
28     printf "  %s [exists|get|delete] key\n" Sys.executable_name;
29     printf "  %s set key value\n" Sys.executable_name;
30     printf "Subcommands:\n";
31     printf "  exists    - Check if key exists, exit code 0 if exists, 1 if not\n";
32     printf "  get       - Print the current value of the key\n";
33     printf "  set       - Set the key to a new value\n";
34     printf "  delete    - Delete the key\n";
35     printf "  list      - List the names and values of all keys\n";
36     exit ret
37
38 let () =
39   match Array.to_list Sys.argv with
40   | [] -> usage 1
41   | [ ("--help" | "-h" | "-?") ] -> usage 0
42   | [ _; "exists"; key ] ->
43     if memory_exists key then exit 0 else exit 1
44   | [ _; "get"; key ] ->
45     (match memory_get key with
46     | None ->
47       eprintf "%s: key '%s' not found in memory\n" Sys.executable_name key;
48       exit 1
49     | Some v -> print_endline v
50     )
51   | [ _; "set"; key; value ] ->
52     memory_set key value
53   | [ _; "delete"; key ] ->
54     memory_delete key
55   | [ _; "list" ] ->
56     let mem = memory_list () in
57     List.iter (fun (key, value) -> printf "%s\t%s\n" key value) mem
58   | _ -> usage 1