Skip to content

Commit

Permalink
Added in config support for port
Browse files Browse the repository at this point in the history
  • Loading branch information
just-an-engineer authored and just-an-engineer committed Aug 5, 2024
1 parent 3f7e105 commit 3a25652
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ const SERVER_STARTUP_TIMEOUT: Duration = Duration::from_millis(10000);

/// Get the port on which the server should listen.
fn get_port() -> u16 {
env::var("SCCACHE_SERVER_PORT")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(DEFAULT_PORT)
let fallback = || -> u16 {
env::var("SCCACHE_SERVER_PORT")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(DEFAULT_PORT)
};
match &Config::load() {
Ok(config) => config.port.unwrap_or_else(fallback),
Err(_) => fallback(),
}
}

/// Check if ignoring all response errors
Expand Down
23 changes: 23 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ pub struct FileConfig {
pub cache: CacheConfigs,
pub dist: DistConfig,
pub server_startup_timeout_ms: Option<u64>,
pub port: Option<u16>,
}

// If the file doesn't exist or we can't read it, log the issue and proceed. If the
Expand Down Expand Up @@ -946,6 +947,7 @@ pub struct Config {
pub fallback_cache: DiskCacheConfig,
pub dist: DistConfig,
pub server_startup_timeout: Option<std::time::Duration>,
pub port: Option<u16>,
}

impl Config {
Expand All @@ -967,6 +969,7 @@ impl Config {
cache,
dist,
server_startup_timeout_ms,
port,
} = file_conf;
conf_caches.merge(cache);

Expand All @@ -982,6 +985,7 @@ impl Config {
fallback_cache,
dist,
server_startup_timeout,
port,
}
}
}
Expand Down Expand Up @@ -1281,6 +1285,7 @@ fn config_overrides() {
},
dist: Default::default(),
server_startup_timeout_ms: None,
port: None,
};

assert_eq!(
Expand All @@ -1303,6 +1308,7 @@ fn config_overrides() {
},
dist: Default::default(),
server_startup_timeout: None,
port: None,
}
);
}
Expand Down Expand Up @@ -1578,6 +1584,23 @@ no_credentials = true
rewrite_includes_only: false,
},
server_startup_timeout_ms: Some(10000),
port: None,
}
)
}

#[test]
fn test_port_config() {
// just set up a config file with just port, then have it read it in, and ensure port is defined in the struct
const CONFIG_STR: &str = "port = 8080";
let file_config: FileConfig = toml::from_str(CONFIG_STR).expect("Is valid toml.");
assert_eq!(
file_config,
FileConfig {
cache: Default::default(),
dist: Default::default(),
server_startup_timeout_ms: None,
port: Some(8080),
}
)
}
1 change: 1 addition & 0 deletions tests/harness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub fn sccache_client_cfg(
rewrite_includes_only: false, // TODO
},
server_startup_timeout_ms: None,
port: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn config_with_dist_auth(
rewrite_includes_only: true,
},
server_startup_timeout_ms: None,
port: None,
}
}

Expand Down

0 comments on commit 3a25652

Please sign in to comment.