Skip to content

Commit

Permalink
Rewrite and Scoop integration (#4)
Browse files Browse the repository at this point in the history
- Adding Scoop support
- Complete rewrite of the code
- Working with entities and controllers
- Removing installation as it now useless
- Removing Powershell-YAML dependency
- Adding more commands (help, version, etc.)
  • Loading branch information
Nyuwb authored Nov 3, 2022
1 parent b99bea3 commit 257048d
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 155 deletions.
57 changes: 25 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,54 @@

Allows you to switch the activation of certain services from one WSL instance to another.

The following services are managed :
The following services are currently managed, but you can custom it in the `config.json` file :

- apache
- mysql
- php

## Requirements
## Installation

### From Scoop

You need to install the Powershell-YAML module to get this app to work.
We don't need to install it globally so we're using the `CurrentUser` scope.
Add the bucket `scoop-srsrns`.
Then install it directly from this bucket :

```
Install-Module Powershell-YAML -Scope CurrentUser
scoop bucket add srsrns https://github.com/mbl-35/scoop-srsrns
scoop install wsl-switch
```

## Installation

Clone this repository : `git clone https://github.com/Nyuwb/wsl-switch`

Then edit the content of the `config.yaml` file to adapt to your configuration.
To uninstall it, simply use `scoop uninstall wsl-switch`

By default for the `instances`, we're using the same value as the hostname, but the `key` can be the word you wants.
### From GitHub

The `hostname` parameter should be the name of the distribution name on WSL.
Clone this repository : `git clone https://github.com/Nyuwb/wsl-switch`

Then, you'll need to execute the `installer.ps1` file to generate all the aliases and to make the configuration working.
After that, you'll need to include your `$Profile` file as it stores all aliases and functions for this application.
Add the path to the local repository on your Powershell profile :

```
path_to\wsl-switch\config\installer.ps1
. $Profile
Invoke-Item $Profile
# Set-Alias wsl-switch _path_to_repository_\wsl-switch.ps1
```

## Run
## Edit the configuration file

To enable the service `$service` on `$hostname`, you'll need to type the following command :
You need to run the following command to edit the configuration file.

```
switch-wsl $service $hostname
wsl-switch config
```

The `$hostname` parameter is the `key` stored in the `config.yaml`.

To make it easier to use, a list of aliases are created and is available below.
It will open the `config.json` file that you can edit.
Save it, and it will direcly be used by the application.

## Alias list
## Run the application

Because of Powershell, we cannot create easy aliases like with Bash (Powershell don't accept arguments).
To enable the service `$service` on `$hostname`, you'll need to type the following command :

- switch-apache : `switch-wsl apache`
- switch-mysql : `switch-wsl mysql`
- switch-php : `switch-wsl php`
- switch-apache-mysql : `switch-wsl apache,mysql`
- switch-apache-php : `switch-wsl apache,php`
- switch-mysql-php : `switch-wsl mysql,php`
- switch-all : `switch-wsl apache,mysql,php`
```
wsl-switch $service $hostname
```

Every alias is linked to a function stored in your Powershell profile in : `$Profile`.
The `$hostname` parameter is the `key` stored in the `config.json`.
39 changes: 0 additions & 39 deletions app.ps1

This file was deleted.

25 changes: 25 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"services": [
"apache",
"mysql",
"php"
],
"instances": {
"trusty": {
"hostname": "trusty",
"services": {
"apache": "apache2",
"mysql": "mysql",
"php": "php5-fpm"
}
},
"focal": {
"hostname": "focal",
"services": {
"apache": "apache2",
"mysql": "mysql",
"php": "php7.4-fpm"
}
}
}
}
17 changes: 0 additions & 17 deletions config.yaml

This file was deleted.

14 changes: 0 additions & 14 deletions config/alias-list.txt

This file was deleted.

6 changes: 0 additions & 6 deletions config/installer.ps1

This file was deleted.

32 changes: 0 additions & 32 deletions src/Config/Installer.psm1

This file was deleted.

66 changes: 57 additions & 9 deletions src/Config/Loader.psm1
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
Using Module '..\Controller\ConfigController.psm1'
Using Module '..\Entity\Instance.psm1'
Using Module '..\Utils\Console.psm1'

Class Loader
{
[System.Object] $Instances
[String] $ProjectPath
[Hashtable] $Instances = @{}
[Array] $Services
[String] $Version = '0.3'

Loader()
{
# Get the root path
$RootPath = (Get-Item $PSScriptRoot).Parent.Parent.FullName
# Load of the configuration
$Config = Get-Content ($RootPath +'\config.yaml') | ConvertFrom-YAML
$Config = Get-Content ($RootPath +'\config.json') -Raw | ConvertFrom-JSON
# Saving the configuration in the current class
$This.Services = $Config.Services
$This.Instances = $Config.Instances
$This.ProjectPath = $RootPath
$This.SetInstances($Config.Instances)
}

[System.Object] GetInstances()
# Checking if an instance name exists
[Boolean] InstanceExists([String] $InstanceName)
{
Return $This.Instances
Return ($This.GetInstances().Keys -Contains $InstanceName)
}

[System.Object] GetByInstanceName($InstanceName)
[System.Object] GetByInstanceName([String] $InstanceName)
{
Return $This.GetInstances()[$InstanceName]
}

[System.Object] GetOtherInstanceThan($InstanceName)
[System.Object] GetOtherInstanceThan([String] $InstanceName)
{
$Instance = $null
$This.GetInstances().Keys | Foreach-Object {
Expand All @@ -38,4 +40,50 @@ Class Loader
}
Return $Instance
}

# Associate every service together to generate a string that will be used as a regex for the command
[String] FormatServicesToString()
{
$ServiceList = New-Object System.Collections.Generic.List[System.Object]
For ($i = 0; $i -lt $This.Services.Length; $i++) {
$ServiceList.Add($This.Services[$i])
# Getting associated service list
If ($i -ne $This.Services.Length -1) {
$SubServiceList = New-Object System.Collections.Generic.List[System.Object]
$SubServiceList.Add($This.Services[$i])
For ($j = ($i + 1); $j -lt $This.Services.Length; $j++) {
$ServiceList.Add($This.Services[$i] +','+ $This.Services[$j])
$SubServiceList.Add($This.Services[$j])
}
$ServiceList.Add($SubServiceList.ToArray() -Join ',')
}
}
$ServicesArray = $ServiceList.ToArray() | Select -Unique
Return ($ServicesArray -Join '|')
}

# Setter list
[Void] SetInstances($InstanceList)
{
$InstanceList.PSObject.Properties | Foreach-Object {
$Instance = [Instance]::New($_.Name, $_.Value.Hostname, $_.Value.Services)
$This.Instances[$_.Name] = $Instance
}
}

# Getter list
[Hashtable] GetInstances()
{
Return $This.Instances
}

[System.Object] GetServices()
{
Return $This.Services
}

[String] GetVersion()
{
Return $This.Version
}
}
Loading

0 comments on commit 257048d

Please sign in to comment.