Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #349 from subspace/fix/create-config-from-backend
Browse files Browse the repository at this point in the history
feat(config): create from backend
  • Loading branch information
dnoishi authored Oct 5, 2022
2 parents 704f5fc + 05880fb commit e9f0ba9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
2 changes: 2 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async fn main() -> Result<()> {
utils::open_folder,
utils::get_disk_stats,
utils::get_this_binary,
utils::create_file,
],
#[cfg(target_os = "windows")]
tauri::generate_handler![
Expand All @@ -120,6 +121,7 @@ async fn main() -> Result<()> {
utils::get_disk_stats,
utils::get_this_binary,
utils::open_folder,
utils::create_file,
],
)
.build(ctx)
Expand Down
25 changes: 25 additions & 0 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use serde::Serialize;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::process::Command;
use tauri::{api, Env};
use tracing::{debug, error, info};

#[cfg(not(target_os = "windows"))]
use std::os::unix::fs::PermissionsExt;

#[derive(Serialize)]
pub(crate) struct DiskStats {
free_bytes: u64,
Expand Down Expand Up @@ -77,3 +82,23 @@ pub(crate) fn custom_log_dir(id: &str) -> PathBuf {

path.expect("log path generation should succeed")
}

#[tauri::command]
pub(crate) fn create_file(path: &str, content: String) {
let mut file = File::create(path).expect("can't create file");
file.write_all(content.as_bytes())
.expect("couldn't write file");

// config file is created under the current user's folder, in Windows, other users do not have read/write access to these
#[cfg(not(target_os = "windows"))]
{
let mut perms = file
.metadata()
.expect("could not get metadata")
.permissions();
perms.set_mode(0o600);

file.set_permissions(perms)
.expect("failed to set permissions for the config");
}
}
16 changes: 8 additions & 8 deletions src/boot/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createApi } from '../lib/util';
import AutoLauncher, { MacOSAutoLauncher, LinuxAutoLauncher, WindowsAutoLauncher } from '../lib/autoLauncher';
import Config from '../lib/config';
import { useStore } from '../stores/store';
import { appName, errorLogger, infoLogger } from '../lib/util';
import { appName, errorLogger, infoLogger, writeFile } from '../lib/util';
import * as native from '../lib/native';
import { initUpdater } from '../lib/updater';

Expand All @@ -25,10 +25,10 @@ declare module '@vue/runtime-core' {

export default boot(async ({ app }) => {
const store = useStore();

// create Config instance and initialize it
const configDir = await tauri.path.configDir();
const config = new Config({ fs: tauri.fs, appName, configDir, errorLogger });
const config = new Config({ fs: tauri.fs, appName, configDir, writeFile });

try {
await config.init();
Expand Down Expand Up @@ -56,15 +56,15 @@ export default boot(async ({ app }) => {
// make client available as global prop
app.config.globalProperties.$client = client;

// create AutoLauncher instance and initialize it
// create AutoLauncher instance and initialize it
try {
const osType = await tauri.os.type();
infoLogger('OS TYPE: ' + osType);
const appPath = await tauri.invoke('get_this_binary') as string; // this is not the same as appDir: appPath -> appDir
infoLogger('get_this_binary : ' + appPath);

let osAutoLauncher;

if (osType === 'Darwin') {
osAutoLauncher = new MacOSAutoLauncher({ appName, appPath, native });
} else if (osType === 'Windows_NT') {
Expand All @@ -74,10 +74,10 @@ export default boot(async ({ app }) => {
} else {
osAutoLauncher = new LinuxAutoLauncher({ appName, appPath, configDir, fs: tauri.fs });
}

const autoLauncher = new AutoLauncher({ config, osAutoLauncher });
await autoLauncher.init();

// make autoLauncher available as global prop
app.config.globalProperties.$autoLauncher = autoLauncher;
} catch (error) {
Expand Down
15 changes: 6 additions & 9 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ interface FilesParams {
fs: typeof fs;
configDir: string;
appName: string;
errorLogger: (error: unknown) => Promise<void>;
writeFile: (configFullPath: string, config: IConfig) => Promise<void>;
}

export interface Plot {
location: string
sizeGB: number
}

interface IConfig {
export interface IConfig {
plot: Plot
rewardAddress: string,
launchOnBoot: boolean,
Expand Down Expand Up @@ -45,13 +45,13 @@ class Config {
private fs: typeof fs;
private configPath: string;
private configFullPath: string;
private errorLogger: (error: unknown) => Promise<void>;
private writeFile: (configFullPath: string, config: IConfig) => Promise<void>;

constructor({ fs, configDir, appName, errorLogger }: FilesParams) {
constructor({ fs, configDir, appName, writeFile }: FilesParams) {
this.fs = fs;
this.configPath = `${configDir}${appName}`;
this.configFullPath = `${this.configPath}/${appName}.cfg`;
this.errorLogger = errorLogger;
this.writeFile = writeFile;
}

/**
Expand Down Expand Up @@ -115,10 +115,7 @@ class Config {
* @param {IConfig} - config object to store as config file
*/
private async write(config: IConfig): Promise<void> {
return this.fs.writeFile({
path: this.configFullPath,
contents: JSON.stringify(config, null, 2)
});
return this.writeFile(this.configFullPath, config);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/lib/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export {
errorLogger,
infoLogger,
resetAndClear,
writeFile,
} from './tauri';
12 changes: 11 additions & 1 deletion src/lib/util/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { invoke } from '@tauri-apps/api/tauri';
import { LocalStorage } from 'quasar';

import { IPlotDir } from '../plotDir';
import Config from '../config';
import Config, { IConfig } from '../config';
import { getErrorMessage } from './';

export const appName: string = process.env.APP_NAME || 'subspace-desktop';
Expand Down Expand Up @@ -50,3 +50,13 @@ export async function resetAndClear({
// this may throw error that file does not exist if config file is in the same directory as plot files (removed on the line above)
await config.remove();
}

export async function writeFile(
configFullPath: string,
config: IConfig
): Promise<void> {
await invoke('create_file', {
path: configFullPath,
content: JSON.stringify(config, null, 2)
});
}
1 change: 1 addition & 0 deletions src/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const params = {
appName,
configDir,
errorLogger: jest.fn(),
writeFile: jest.fn(),
};

// TODO:
Expand Down

0 comments on commit e9f0ba9

Please sign in to comment.