| 1 | 1 | //! The `grove` command. For now it works directly on a store on this machine; the same |
| 2 | 2 | //! operations will go over HTTP once grove runs as a server. |
| 3 | 3 | //! |
| 4 | | //! Who's acting: `~/.grove/config.json` names a person and, usually, the agent acting for them. |
| 4 | //! Who's acting: `~/.grove2/config.json` names a person and, usually, the agent acting for them. |
| 5 | //! (`~/.grove2`, not `~/.grove`: the old grove CLI keeps its own config in `~/.grove`.) |
| 5 | 6 | //! Every command acts as that agent, except `land` and `decline`, which are decisions and act |
| 6 | 7 | //! as the person. Locally this rests on trust; on a server, tokens will enforce it. |
| 7 | 8 | |
| | |
| 66 | 67 | PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".into())) |
| 67 | 68 | } |
| 68 | 69 | |
| 70 | fn grove_home() -> PathBuf { |
| 71 | home().join(".grove2") |
| 72 | } |
| 73 | |
| 69 | 74 | fn config_path() -> PathBuf { |
| 70 | | home().join(".grove").join("config.json") |
| 75 | grove_home().join("config.json") |
| 71 | 76 | } |
| 72 | 77 | |
| 73 | 78 | struct Config { |
| | |
| 274 | 279 | |
| 275 | 280 | fn setup(args: &Args) -> Result<()> { |
| 276 | 281 | let username = args.pos(0).ok_or("usage: grove setup <username> [--agent NAME]")?; |
| 277 | | let store_dir = args.value(&["--store"]).map(PathBuf::from).unwrap_or_else(|| home().join(".grove").join("store")); |
| 282 | // Never overwrite a config grove didn't write. |
| 283 | if let Ok(existing) = fs::read_to_string(config_path()) { |
| 284 | let ours = json::parse(&existing).map(|v| v.get("store").is_some() && v.get("person").is_some()).unwrap_or(false); |
| 285 | if !ours { |
| 286 | return e!("{} exists and isn't a grove config; move it aside first", config_path().display()); |
| 287 | } |
| 288 | } |
| 289 | let store_dir = args.value(&["--store"]).map(PathBuf::from).unwrap_or_else(|| grove_home().join("store")); |
| 278 | 290 | let mut store = Store::open(&store_dir).map_err(|e| e.to_string())?; |
| 279 | 291 | if store.state().person(username).is_none() { |
| 280 | 292 | s(store.add_person(username, args.value(&["--display"]).unwrap_or(username)))?; |