feature(cli): add packages command and various subcommands
add commands to handle package management add -> adds packages delete -> deletes packages show -> shows config sorted by package_managers
This commit is contained in:
parent
ffd3faadcd
commit
c9bf9c9a6f
4 changed files with 240 additions and 130 deletions
24
cmd.go
24
cmd.go
|
|
@ -9,15 +9,30 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "package-installer",
|
Use: "system_setup_tool",
|
||||||
Short: "Installiert Pakete basierend auf TOML-Konfiguration",
|
Short: "Installs packages based on TOML configuration",
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
rootCmd.PersistentFlags().StringP("config", "c", "", "Pfad zur Konfigurationsdatei")
|
rootCmd.PersistentFlags().StringP("config", "c", "", "Path to the configuration file")
|
||||||
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
||||||
|
|
||||||
|
addCmd.Flags().StringP("name", "n", "", "The name of the package you want to add")
|
||||||
|
addCmd.Flags().StringP("manager", "m", "", "The package manager you want to add the package to (homebrew|cargo|flatpak|pipx|go)")
|
||||||
|
addCmd.Flags().Bool("system", false, "Add as a system package")
|
||||||
|
addCmd.Flags().Bool("headless", false, "Add as a headless system package (only used with --system)")
|
||||||
|
|
||||||
|
deleteCmd.Flags().StringP("name", "n", "", "The name of the package you want to delete")
|
||||||
|
deleteCmd.Flags().StringP("manager", "m", "", "The package manager you want to delete the package from (homebrew|cargo|flatpak|pipx|go)")
|
||||||
|
deleteCmd.Flags().Bool("system", false, "Delete from system packages")
|
||||||
|
deleteCmd.Flags().Bool("headless", false, "Delete from headless system packages (only used with --system)")
|
||||||
|
|
||||||
|
enableCmd.Flags().Bool("value", true, "Set to true to enable, false to disable")
|
||||||
|
|
||||||
|
packageCmd.AddCommand(addCmd, deleteCmd, showCmd, enableCmd)
|
||||||
|
rootCmd.AddCommand(packageCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
|
@ -25,11 +40,12 @@ func initConfig() {
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} else {
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
|
viper.SetConfigType("toml")
|
||||||
viper.AddConfigPath(".")
|
viper.AddConfigPath(".")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
fmt.Println("Fehler beim Lesen der Konfigurationsdatei:", err)
|
fmt.Println("Error reading configuration file:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
152
config.toml
152
config.toml
|
|
@ -1,129 +1,31 @@
|
||||||
|
config = ''
|
||||||
headless = true
|
headless = true
|
||||||
|
|
||||||
[packages]
|
|
||||||
headless = [
|
|
||||||
"git",
|
|
||||||
"curl",
|
|
||||||
"wget",
|
|
||||||
"biber",
|
|
||||||
"bear",
|
|
||||||
"docker",
|
|
||||||
"docker-compose",
|
|
||||||
"zsh",
|
|
||||||
"npm",
|
|
||||||
"task",
|
|
||||||
"tree-sitter-cli",
|
|
||||||
"python3-pip",
|
|
||||||
"latexmk",
|
|
||||||
"luarocks",
|
|
||||||
"typst-cli",
|
|
||||||
]
|
|
||||||
|
|
||||||
non_headless = [
|
|
||||||
"flameshot",
|
|
||||||
"fuzzel",
|
|
||||||
"hyprcursor",
|
|
||||||
"hypridle",
|
|
||||||
"hyprland",
|
|
||||||
"hyprland-qtutils",
|
|
||||||
"hyprlock",
|
|
||||||
"hyprpaper",
|
|
||||||
"hyprutils",
|
|
||||||
"kitty",
|
|
||||||
"mako",
|
|
||||||
"SwayNotificationCenter",
|
|
||||||
"SwayNotificationCenter-zsh-completion",
|
|
||||||
"mpv",
|
|
||||||
"pidgin",
|
|
||||||
"remmina",
|
|
||||||
"thunderbird-i18n-de",
|
|
||||||
"virt-manager",
|
|
||||||
"vlc",
|
|
||||||
"waybar",
|
|
||||||
"nwg-panel",
|
|
||||||
"nwg-look",
|
|
||||||
"xdg-desktop-portal-hyprland",
|
|
||||||
"zathura",
|
|
||||||
"zathura-pdf-mupdf",
|
|
||||||
]
|
|
||||||
|
|
||||||
[package_managers.homebrew]
|
|
||||||
enable = true
|
|
||||||
packages = [
|
|
||||||
"fd",
|
|
||||||
"fzf",
|
|
||||||
"ripgrep",
|
|
||||||
"neovim",
|
|
||||||
"helix",
|
|
||||||
# "golang",
|
|
||||||
# "rust",
|
|
||||||
"node",
|
|
||||||
"yazi",
|
|
||||||
"zk",
|
|
||||||
"bat",
|
|
||||||
"bottom",
|
|
||||||
"btop",
|
|
||||||
"git-cliff",
|
|
||||||
"glow",
|
|
||||||
"lazygit",
|
|
||||||
"goreleaser",
|
|
||||||
"harlequin",
|
|
||||||
"mage",
|
|
||||||
"posting",
|
|
||||||
"typst",
|
|
||||||
"wails",
|
|
||||||
"zoxide",
|
|
||||||
"lsd",
|
|
||||||
"jq",
|
|
||||||
"yq",
|
|
||||||
"timewarrior",
|
|
||||||
"tmux",
|
|
||||||
# "pipx",
|
|
||||||
"fastfetch",
|
|
||||||
"stow",
|
|
||||||
"distrobox",
|
|
||||||
# "asciidoc",
|
|
||||||
# "asciidoctor",
|
|
||||||
]
|
|
||||||
|
|
||||||
[package_managers.go]
|
|
||||||
enable = true
|
|
||||||
packages = [
|
|
||||||
"github.com/stefanlogue/meteor",
|
|
||||||
"golang.org/x/tools/gopls",
|
|
||||||
"github.com/go-delve/delve/cmd/dlv",
|
|
||||||
"github.com/air-verse/air",
|
|
||||||
# "go.senan.xyz/cliphist",
|
|
||||||
]
|
|
||||||
|
|
||||||
[package_managers.cargo]
|
|
||||||
enable = true
|
|
||||||
packages = ["typst-cli"]
|
|
||||||
|
|
||||||
[package_managers.pipx]
|
|
||||||
enable = true
|
|
||||||
packages = ["euporie"]
|
|
||||||
|
|
||||||
[package_managers.flatpak]
|
|
||||||
enable = true
|
|
||||||
remotes = [
|
|
||||||
{ name = "flathub", url = "https://flathub.org/repo/flathub.flatpakrepo" },
|
|
||||||
]
|
|
||||||
packages = [
|
|
||||||
"com.spotify.Client",
|
|
||||||
"us.zoom.Zoom",
|
|
||||||
"org.zotero.Zotero",
|
|
||||||
"com.google.AndroidStudio",
|
|
||||||
"io.freetubeapp.FreeTube",
|
|
||||||
"com.discordapp.Discord",
|
|
||||||
"com.nextcloud.desktopclient.nextcloud",
|
|
||||||
"com.github.tchx84.Flatseal",
|
|
||||||
"io.github.flattool.Warehouse",
|
|
||||||
"org.onlyoffice.desktopeditors",
|
|
||||||
# "com.google.Chrome",
|
|
||||||
# "org.mozilla.Thunderbird",
|
|
||||||
]
|
|
||||||
|
|
||||||
[dotfiles]
|
[dotfiles]
|
||||||
enable = false
|
enable = false
|
||||||
git_repo = "https://codeberg.org/Pata1704/dotfiles.git"
|
git_repo = 'https://codeberg.org/Pata1704/dotfiles.git'
|
||||||
|
|
||||||
|
[package_managers]
|
||||||
|
[package_managers.cargo]
|
||||||
|
enable = true
|
||||||
|
packages = ['typst-cli']
|
||||||
|
|
||||||
|
[package_managers.flatpak]
|
||||||
|
enable = true
|
||||||
|
packages = ['com.spotify.Client', 'us.zoom.Zoom', 'org.zotero.Zotero', 'com.google.AndroidStudio', 'io.freetubeapp.FreeTube', 'com.discordapp.Discord', 'com.nextcloud.desktopclient.nextcloud', 'com.github.tchx84.Flatseal', 'io.github.flattool.Warehouse', 'org.onlyoffice.desktopeditors']
|
||||||
|
|
||||||
|
[package_managers.go]
|
||||||
|
enable = true
|
||||||
|
packages = ['github.com/stefanlogue/meteor', 'golang.org/x/tools/gopls', 'github.com/go-delve/delve/cmd/dlv', 'github.com/air-verse/air']
|
||||||
|
|
||||||
|
[package_managers.homebrew]
|
||||||
|
enable = true
|
||||||
|
packages = ['fd', 'fzf', 'ripgrep', 'neovim', 'helix', 'node', 'yazi', 'zk', 'bat', 'bottom', 'btop', 'git-cliff', 'glow', 'lazygit', 'goreleaser', 'harlequin', 'mage', 'posting', 'typst', 'wails', 'zoxide', 'lsd', 'jq', 'yq', 'timewarrior', 'tmux', 'fastfetch', 'stow', 'distrobox']
|
||||||
|
|
||||||
|
[package_managers.pipx]
|
||||||
|
enable = true
|
||||||
|
packages = ['euporie']
|
||||||
|
|
||||||
|
[packages]
|
||||||
|
headless = ['git', 'curl', 'wget', 'biber', 'bear', 'docker', 'docker-compose', 'zsh', 'npm', 'task', 'tree-sitter-cli', 'python3-pip', 'latexmk', 'luarocks']
|
||||||
|
non_headless = ['flameshot', 'fuzzel', 'hyprcursor', 'hypridle', 'hyprland', 'hyprland-qtutils', 'hyprlock', 'hyprpaper', 'hyprutils', 'kitty', 'mako', 'SwayNotificationCenter', 'SwayNotificationCenter-zsh-completion', 'mpv', 'pidgin', 'remmina', 'thunderbird-i18n-de', 'virt-manager', 'vlc', 'waybar', 'nwg-panel', 'nwg-look', 'xdg-desktop-portal-hyprland', 'zathura', 'zathura-pdf-mupdf']
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -1,4 +1,4 @@
|
||||||
module system_setup
|
module system_setup_tool
|
||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
|
|
|
||||||
192
package_commands.go
Normal file
192
package_commands.go
Normal file
|
|
@ -0,0 +1,192 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
var packageCmd = &cobra.Command{
|
||||||
|
Use: "package",
|
||||||
|
Short: "Manage packages in the configuration",
|
||||||
|
}
|
||||||
|
|
||||||
|
var addCmd = &cobra.Command{
|
||||||
|
Use: "add",
|
||||||
|
Short: "Adds a package to the config.toml",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
name, _ := cmd.Flags().GetString("name")
|
||||||
|
manager, _ := cmd.Flags().GetString("manager")
|
||||||
|
isSystem, _ := cmd.Flags().GetBool("system")
|
||||||
|
isHeadless, _ := cmd.Flags().GetBool("headless")
|
||||||
|
|
||||||
|
if isSystem {
|
||||||
|
var packages []string
|
||||||
|
if isHeadless {
|
||||||
|
packages = viper.GetStringSlice("packages.headless")
|
||||||
|
} else {
|
||||||
|
packages = viper.GetStringSlice("packages.non_headless")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add package to the appropriate list
|
||||||
|
packages = append(packages, name)
|
||||||
|
|
||||||
|
if isHeadless {
|
||||||
|
viper.Set("packages.headless", packages)
|
||||||
|
} else {
|
||||||
|
viper.Set("packages.non_headless", packages)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
packages := viper.GetStringSlice(fmt.Sprintf("package_managers.%s.packages", manager))
|
||||||
|
|
||||||
|
for _, pkg := range packages {
|
||||||
|
if pkg == name {
|
||||||
|
fmt.Printf("Package %s is already present in the configuration for %s\n", name, manager)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
packages = append(packages, name)
|
||||||
|
viper.Set(fmt.Sprintf("package_managers.%s.packages", manager), packages)
|
||||||
|
|
||||||
|
}
|
||||||
|
if err := viper.WriteConfig(); err != nil {
|
||||||
|
fmt.Printf("Error writing configuration: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Package %s has been added to the configuration for %s\n", name, manager)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var deleteCmd = &cobra.Command{
|
||||||
|
Use: "delete",
|
||||||
|
Short: "Deletes a package from the config.toml",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
name, _ := cmd.Flags().GetString("name")
|
||||||
|
manager, _ := cmd.Flags().GetString("manager")
|
||||||
|
isSystem, _ := cmd.Flags().GetBool("system")
|
||||||
|
isHeadless, _ := cmd.Flags().GetBool("headless")
|
||||||
|
|
||||||
|
if isSystem {
|
||||||
|
var packages []string
|
||||||
|
var packageType string
|
||||||
|
if isHeadless {
|
||||||
|
packages = viper.GetStringSlice("packages.headless")
|
||||||
|
packageType = "headless"
|
||||||
|
} else {
|
||||||
|
packages = viper.GetStringSlice("packages.non_headless")
|
||||||
|
packageType = "non-headless"
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, pkg := range packages {
|
||||||
|
if pkg == name {
|
||||||
|
packages = append(packages[:i], packages[i+1:]...)
|
||||||
|
if isHeadless {
|
||||||
|
viper.Set("packages.headless", packages)
|
||||||
|
} else {
|
||||||
|
viper.Set("packages.non_headless", packages)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := viper.WriteConfig(); err != nil {
|
||||||
|
fmt.Printf("Error writing configuration: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Package %s has been deleted from the %s system packages\n", name, packageType)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Package %s was not found in the %s system packages\n", name, packageType)
|
||||||
|
} else {
|
||||||
|
packages := viper.GetStringSlice(fmt.Sprintf("package_managers.%s.packages", manager))
|
||||||
|
|
||||||
|
for i, pkg := range packages {
|
||||||
|
if pkg == name {
|
||||||
|
packages = append(packages[:i], packages[i+1:]...)
|
||||||
|
viper.Set(fmt.Sprintf("package_managers.%s.packages", manager), packages)
|
||||||
|
|
||||||
|
if err := viper.WriteConfig(); err != nil {
|
||||||
|
fmt.Printf("Error writing configuration: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Package %s has been deleted from the configuration for %s\n", name, manager)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Package %s was not found in the configuration for %s\n", name, manager)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var showCmd = &cobra.Command{
|
||||||
|
Use: "show",
|
||||||
|
Short: "Shows the current package configuration",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
packageManagers := viper.GetStringMap("package_managers")
|
||||||
|
|
||||||
|
if len(packageManagers) == 0 {
|
||||||
|
fmt.Println("No package managers configured.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var managers []string
|
||||||
|
for manager := range packageManagers {
|
||||||
|
managers = append(managers, manager)
|
||||||
|
}
|
||||||
|
sort.Strings(managers)
|
||||||
|
|
||||||
|
for _, manager := range managers {
|
||||||
|
fmt.Printf("Package Manager: %s\n", manager)
|
||||||
|
packages := viper.GetStringSlice(fmt.Sprintf("package_managers.%s.packages", manager))
|
||||||
|
sort.Strings(packages)
|
||||||
|
for _, pkg := range packages {
|
||||||
|
fmt.Printf(" - %s\n", pkg)
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
fmt.Println("System Packages:")
|
||||||
|
headless := viper.GetStringSlice("packages.headless")
|
||||||
|
nonHeadless := viper.GetStringSlice("packages.non_headless")
|
||||||
|
|
||||||
|
fmt.Println(" Headless:")
|
||||||
|
for _, pkg := range headless {
|
||||||
|
fmt.Printf(" - %s\n", pkg)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(" Non-Headless:")
|
||||||
|
for _, pkg := range nonHeadless {
|
||||||
|
fmt.Printf(" - %s\n", pkg)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var enableCmd = &cobra.Command{
|
||||||
|
Use: "enable [package_manager]",
|
||||||
|
Short: "Enable or disable a package manager (Options: homebrew|go|cargo|pipx|flatpak)",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
manager := args[0]
|
||||||
|
enable, _ := cmd.Flags().GetBool("value")
|
||||||
|
|
||||||
|
if !viper.IsSet(fmt.Sprintf("package_managers.%s", manager)) {
|
||||||
|
fmt.Printf("Package manager %s not found in configuration\n", manager)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
viper.Set(fmt.Sprintf("package_managers.%s.enable", manager), enable)
|
||||||
|
|
||||||
|
if err := viper.WriteConfig(); err != nil {
|
||||||
|
fmt.Printf("Error writing configuration: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Package manager %s has been %s\n", manager, map[bool]string{true: "enabled", false: "disabled"}[enable])
|
||||||
|
},
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue