From 1f862145ea7aceded00940c92014ed071ff55344 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Mon, 31 Oct 2022 18:42:41 +0100
Subject: [PATCH] Added winget install

---
 config.windows.toml |  6 +++++
 config/__main__.py  | 66 +++++++++++++++++++++++++++------------------
 2 files changed, 46 insertions(+), 26 deletions(-)

diff --git a/config.windows.toml b/config.windows.toml
index 1a8140e..d2b211d 100644
--- a/config.windows.toml
+++ b/config.windows.toml
@@ -1,3 +1,9 @@
+[Winget]
+packages = [
+	"Git.Git",
+	"Neovim.Neovim",
+]
+
 [PowerShell]
 in="src/PowerShell/profile.ps1"
 out="~/Documents/WindowsPowerShell/profile.ps1"
diff --git a/config/__main__.py b/config/__main__.py
index d7de533..2498b45 100644
--- a/config/__main__.py
+++ b/config/__main__.py
@@ -3,6 +3,7 @@ import logging
 import logging.config
 import pathlib
 import shutil
+import subprocess
 import sys
 import tomllib
 
@@ -32,32 +33,45 @@ except FileNotFoundError:
     sys.exit(1)
 
 for _name, _conf in config.items():
-    _in, _out = get_io(_conf)
-    log.info(f"Installing {_name} configuration from <{_in}> to <{_out}>.")
-    if not _in.is_file():
-        log.error(f"{_name} configuration <{_in}> could not be found.")
-        sys.exit(1)
+    match _name.lower():
+        case "winget":
+            log.info("Installing packages using winget.")
+            for _package in _conf.get("packages"):
+                _test = subprocess.run(("winget", "list", "--id", _package))
+                if _test.returncode == 0:
+                    _yn = input(f"<{_package}> is already installed; reinstall [Yn] ? ").strip().lower()
+                    if _yn != "y" and _yn != "":
+                        log.info(f"> Skipping <{_package}> install.")
+                        continue
+                log.info(f"> Installing {_package} using winget.")
+                subprocess.run(("winget", "install", _package))
+        case _:
+            _in, _out = get_io(_conf)
+            log.info(f"Installing {_name} configuration from <{_in}> to <{_out}>.")
+            if not _in.is_file():
+                log.error(f"> {_name} configuration <{_in}> could not be found.")
+                sys.exit(1)
 
-    if _out.exists():
-        if _out.is_file():
-            _yn = input(f"<{_out}> already exists; replace [Yn] ?").strip().lower()
-            if _yn != "y" and _yn != "":
-                log.info(f"Skipping {_name} configuration.")
-                continue
-            else:
-                log.info(f"Installing {_name} configuration.")
-        else:
-            log.error(f"<{_out}> already exists and is not a file.")
-            sys.exit(1)
+            if _out.exists():
+                if _out.is_file():
+                    _yn = input(f"<{_out}> already exists; replace [Yn] ? ").strip().lower()
+                    if _yn != "y" and _yn != "":
+                        log.info(f"> Skipping {_name} configuration.")
+                        continue
+                    else:
+                        log.info(f"> Installing {_name} configuration.")
+                else:
+                    log.error(f"> <{_out}> already exists and is not a file.")
+                    sys.exit(1)
 
-    if not _out.parent.exists():
-        _out.parent.mkdir(parents=True)
-    elif not _out.parent.is_dir():
-        log.error(f"{_name} configuration could not be installed as <{_out.parent}> is not a directory.")
-        sys.exit(1)
+            if not _out.parent.exists():
+                _out.parent.mkdir(parents=True)
+            elif not _out.parent.is_dir():
+                log.error(f"> {_name} configuration could not be installed as <{_out.parent}> is not a directory.")
+                sys.exit(1)
 
-    try:
-        shutil.copyfile(_in, _out)
-    except OSError:
-        log.error(f"{_name} could not be configured: destination <{_out}> is not writable.")
-        sys.exit(1)
+            try:
+                shutil.copyfile(_in, _out)
+            except OSError:
+                log.error(f"> {_name} could not be configured: destination <{_out}> is not writable.")
+                sys.exit(1)
