From 8040ba52dd3fd19549f8dcdd0dfd81be728116ee Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Tue, 13 Jul 2021 17:42:59 +0200 Subject: [PATCH 1/7] fix: use `LOCALAPPDATA` env on windows --- CHANGELOG.md | 5 +++++ Cargo.lock | 2 +- Cargo.toml | 2 +- src/state.rs | 14 ++++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ce28a5..a5c9f10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.19.1] - 2021-07-13 + +### Fixed +- Use `LOCALAPPDATA` on windows because `dirs::data_local_dir()` is empty on windows nano server + ## [0.19.0] - 2021-07-13 ### Changed diff --git a/Cargo.lock b/Cargo.lock index 0e68379..44e6bdc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,7 +120,7 @@ dependencies = [ [[package]] name = "docuum" -version = "0.19.0" +version = "0.19.1" dependencies = [ "atty", "byte-unit", diff --git a/Cargo.toml b/Cargo.toml index 16a8d6f..7f5f8fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "docuum" -version = "0.19.0" +version = "0.19.1" authors = ["Stephan Boyer "] edition = "2018" description = "LRU eviction of Docker images." diff --git a/src/state.rs b/src/state.rs index ad9ae8c..d9c8c50 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,9 +2,10 @@ use crate::format::CodeStr; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, + env, fs::{create_dir_all, read_to_string}, io::{self, Write}, - path::PathBuf, + path::{Path, PathBuf}, time::Duration, }; use tempfile::NamedTempFile; @@ -31,8 +32,17 @@ pub struct State { // Where the program state is persisted on disk fn path() -> Option { + let mut base_path = dirs::data_local_dir(); + + // Overwrite the directory on Windows because it's empty in nanoserver + if cfg!(windows) { + if let Ok(local_dir) = env::var("LOCALAPPDATA") { + base_path = Option::Some(Path::new(&local_dir).to_path_buf()) + } + } + // [tag:state_path_has_parent] - dirs::data_local_dir().map(|path| path.join("docuum/state.yml")) + base_path.map(|path| path.join("docuum/state.yml")) } // Return the state in which the program starts, if no state was loaded from disk. From 5cdbc9f28a9669f8ea404fb8586b838e004ffa3d Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Tue, 13 Jul 2021 18:09:42 +0200 Subject: [PATCH 2/7] fix: style --- src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state.rs b/src/state.rs index d9c8c50..509204f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -34,7 +34,7 @@ pub struct State { fn path() -> Option { let mut base_path = dirs::data_local_dir(); - // Overwrite the directory on Windows because it's empty in nanoserver + // Overwrite the directory on Windows because it's empty in nanoserver. if cfg!(windows) { if let Ok(local_dir) = env::var("LOCALAPPDATA") { base_path = Option::Some(Path::new(&local_dir).to_path_buf()) From 91291249acfce32eeef06e4e397e6093df9f7156 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 14 Jul 2021 06:35:32 +0200 Subject: [PATCH 3/7] fix: only override if `data_local_dir` is `None` --- CHANGELOG.md | 2 +- src/state.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5c9f10..039b29b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.19.1] - 2021-07-13 ### Fixed -- Use `LOCALAPPDATA` on windows because `dirs::data_local_dir()` is empty on windows nano server +- Use `LOCALAPPDATA` on windows when `dirs::data_local_dir()` is `None` which happens on `mcr.microsoft.com/windows/nanoserver` Docker image. ## [0.19.0] - 2021-07-13 diff --git a/src/state.rs b/src/state.rs index 509204f..a3de43f 100644 --- a/src/state.rs +++ b/src/state.rs @@ -35,7 +35,7 @@ fn path() -> Option { let mut base_path = dirs::data_local_dir(); // Overwrite the directory on Windows because it's empty in nanoserver. - if cfg!(windows) { + if base_path.is_none() && cfg!(windows) { if let Ok(local_dir) = env::var("LOCALAPPDATA") { base_path = Option::Some(Path::new(&local_dir).to_path_buf()) } From f09eb707682a97809f5b50e09301920750ae2460 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 14 Jul 2021 17:12:50 +0200 Subject: [PATCH 4/7] chore: bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44e6bdc..ea94e4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,7 +120,7 @@ dependencies = [ [[package]] name = "docuum" -version = "0.19.1" +version = "0.19.2" dependencies = [ "atty", "byte-unit", diff --git a/Cargo.toml b/Cargo.toml index 7f5f8fd..3e85cbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "docuum" -version = "0.19.1" +version = "0.19.2" authors = ["Stephan Boyer "] edition = "2018" description = "LRU eviction of Docker images." From 594ab27e30818ff203472a979c6338a1c41a1346 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 14 Jul 2021 17:13:40 +0200 Subject: [PATCH 5/7] chore: apply suggestion --- src/state.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/state.rs b/src/state.rs index a3de43f..4bccfdb 100644 --- a/src/state.rs +++ b/src/state.rs @@ -32,17 +32,15 @@ pub struct State { // Where the program state is persisted on disk fn path() -> Option { - let mut base_path = dirs::data_local_dir(); - - // Overwrite the directory on Windows because it's empty in nanoserver. - if base_path.is_none() && cfg!(windows) { - if let Ok(local_dir) = env::var("LOCALAPPDATA") { - base_path = Option::Some(Path::new(&local_dir).to_path_buf()) - } - } - // [tag:state_path_has_parent] - base_path.map(|path| path.join("docuum/state.yml")) + dirs::data_local_dir() + .map(|path| path.join("docuum/state.yml")) + .or_else(|| { + // In the `mcr.microsoft.com/windows/nanoserver` Docker image, `dirs::data_local_dir()` + // returns `None` (see https://github.com/dirs-dev/dirs-rs/issues/34 for details). So + // we fall back to the value of the `LOCALAPPDATA` environment variable in that case. + env::var("LOCALAPPDATA").ok().map(Into::into) + }) } // Return the state in which the program starts, if no state was loaded from disk. From dc7ff335554e19e81f77dbe7b059af49d5398c82 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 14 Jul 2021 17:18:45 +0200 Subject: [PATCH 6/7] fix: remove unused import --- src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state.rs b/src/state.rs index 4bccfdb..2865238 100644 --- a/src/state.rs +++ b/src/state.rs @@ -5,7 +5,7 @@ use std::{ env, fs::{create_dir_all, read_to_string}, io::{self, Write}, - path::{Path, PathBuf}, + path::PathBuf, time::Duration, }; use tempfile::NamedTempFile; From 09e077ff8d555a2df8222163df7a585f8e0c0819 Mon Sep 17 00:00:00 2001 From: Stephan Boyer Date: Wed, 14 Jul 2021 09:17:57 -0700 Subject: [PATCH 7/7] Update src/state.rs Co-authored-by: Michael Kriese --- src/state.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/state.rs b/src/state.rs index 2865238..ba75fc6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -34,13 +34,13 @@ pub struct State { fn path() -> Option { // [tag:state_path_has_parent] dirs::data_local_dir() - .map(|path| path.join("docuum/state.yml")) .or_else(|| { // In the `mcr.microsoft.com/windows/nanoserver` Docker image, `dirs::data_local_dir()` // returns `None` (see https://github.com/dirs-dev/dirs-rs/issues/34 for details). So // we fall back to the value of the `LOCALAPPDATA` environment variable in that case. env::var("LOCALAPPDATA").ok().map(Into::into) }) + .map(|path| path.join("docuum/state.yml")) } // Return the state in which the program starts, if no state was loaded from disk.