|
20 | 20 | -module(yz_console). |
21 | 21 | -include("yokozuna.hrl"). |
22 | 22 | -export([aae_status/1, |
23 | | - switch_to_new_search/1]). |
| 23 | + switch_to_new_search/1, |
| 24 | + create_schema/1, |
| 25 | + show_schema/1, |
| 26 | + add_to_schema/1, |
| 27 | + remove_from_schema/1]). |
24 | 28 |
|
25 | 29 | %% @doc Print the Active Anti-Entropy status to stdout. |
26 | 30 | -spec aae_status([]) -> ok. |
@@ -51,3 +55,64 @@ switch_to_new_search([]) -> |
51 | 55 | io:format(standard_error, "The following nodes could not be reached: ~s", [DownStr]), |
52 | 56 | {error, {nodes_down, Down}} |
53 | 57 | end. |
| 58 | + |
| 59 | +%% @doc Creates (and overrides) schema for name and file path. |
| 60 | +-spec create_schema([string()|string()]) -> ok | schema_err(). |
| 61 | +create_schema([Name, Path]) -> |
| 62 | + try |
| 63 | + RawSchema = read_schema(Path), |
| 64 | + FMTName = list_to_atom(Name), |
| 65 | + case yz_schema:store(list_to_binary(Name), RawSchema) of |
| 66 | + ok -> |
| 67 | + io:format("~p schema created~n", [FMTName]), |
| 68 | + ok; |
| 69 | + {error, _} -> |
| 70 | + io:format("Error creating schema ~p", [FMTName]), |
| 71 | + error |
| 72 | + end |
| 73 | + catch fileReadError:exitError -> |
| 74 | + exitError |
| 75 | + end. |
| 76 | + |
| 77 | +%% @doc Shows solr schema for name passed in. |
| 78 | +-spec show_schema([string()]) -> ok | schema_err(). |
| 79 | +show_schema([Name]) -> |
| 80 | + FMTName = list_to_atom(Name), |
| 81 | + case yz_schema:get(list_to_binary(Name)) of |
| 82 | + {ok, R} -> |
| 83 | + io:format("Schema ~p:~n~s", [FMTName, binary_to_list(R)]), |
| 84 | + ok; |
| 85 | + {error, notfound} -> |
| 86 | + io:format("Schema ~p doesn't exist~n", [FMTName]), |
| 87 | + error |
| 88 | + end. |
| 89 | + |
| 90 | +%% @doc |
| 91 | +-spec add_to_schema([string()|string()]) -> ok | schema_err(). |
| 92 | +add_to_schema([Name|Opts]) -> |
| 93 | + {Name, Opts}. |
| 94 | + |
| 95 | +%% @doc |
| 96 | +-spec remove_from_schema([string()|string()]) -> ok | schema_err(). |
| 97 | +remove_from_schema([Name, FieldName]) -> |
| 98 | + {Name, FieldName}. |
| 99 | + |
| 100 | +%%%=================================================================== |
| 101 | +%%% Private |
| 102 | +%%%=================================================================== |
| 103 | + |
| 104 | +%% @doc Reads and returns `RawSchema` from file path. |
| 105 | +-spec read_schema(string()) -> raw_schema() | schema_err(). |
| 106 | +read_schema(Path) -> |
| 107 | + AbsPath = filename:absname(Path), |
| 108 | + case file:read_file(AbsPath) of |
| 109 | + {ok, RawSchema} -> |
| 110 | + RawSchema; |
| 111 | + {error, enoent} -> |
| 112 | + io:format("No such file or directory: ~s~n", [Path]), |
| 113 | + throw({fileReadError, enoent}); |
| 114 | + {error, Reason} -> |
| 115 | + ?ERROR("Error reading file ~s:~p", [Path, Reason]), |
| 116 | + io:format("Error reading file ~s, see log for details~n", [Path]), |
| 117 | + throw({fileReadError, Reason}) |
| 118 | + end. |
0 commit comments