setConfig method

void setConfig(
  1. String parent,
  2. String key,
  3. String value
)

Set wslconfig setting

Implementation

void setConfig(String parent, String key, String value) async {
  File file = File(getWslConfigPath());
  if (!file.existsSync()) {
    file.createSync();
  }
  String text = file.readAsStringSync();

  // Check if parent exists
  if (text.contains('[$parent]')) {
    // Check if key exists with regeex
    RegExp regex = RegExp('$key[ ]*=');
    if (regex.hasMatch(text)) {
      // Replace key value
      text = text.replaceAll(RegExp('$key[ ]*=(.*)'), '$key = $value');
    } else {
      // Add key value
      text = text.replaceAll('[$parent]', '[$parent]\n$key = $value');
    }
  } else {
    // Add parent and key value
    text += '\n[$parent]\n$key = $value';
  }

  // Write to file
  file.writeAsStringSync(text);
}