From 587a6e6365fb2dad8aa36fb499d634f781d36024 Mon Sep 17 00:00:00 2001 From: alvanrahimli Date: Sun, 22 Aug 2021 20:08:12 +0400 Subject: [PATCH] Wallpaper support implemented --- .github/workflows/publish.yaml | 2 +- commands/add.go | 36 ++++++++++++++++++++++++---- commands/commands.go | 2 +- commands/help.go | 1 + commands/install.go | 27 +++++++++++++++++++++ handlerlist.json | 43 +++++++++++++++++++++------------- models/models.go | 31 +++++++++++++----------- 7 files changed, 106 insertions(+), 36 deletions(-) diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index fe4cea4..9429362 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -14,7 +14,7 @@ jobs: with: go-version: "^1.13.1" - name: Build - run: go build -o dots-cli + run: go build -o dots-cli-linux - name: Create release id: create_release uses: actions/create-release@v1 diff --git a/commands/add.go b/commands/add.go index 9517998..0178b41 100644 --- a/commands/add.go +++ b/commands/add.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "github.com/alvanrahimli/dots-cli/apphandler" + "github.com/alvanrahimli/dots-cli/dlog" "github.com/alvanrahimli/dots-cli/models" "github.com/alvanrahimli/dots-cli/utils" "os" @@ -20,7 +21,7 @@ func (a Add) GetArguments() []string { } func (a Add) CheckRequirements() (bool, string) { - if len(a.Options.Arguments) < 2 { + if len(a.Options.Arguments) < 2 && a.Options.WpPath == "" { return false, fmt.Sprintf("%s is not enough arguments for add command.", a.Options.Arguments) } @@ -43,6 +44,34 @@ func (a Add) ExecuteCommand(opts *models.Opts, config *models.AppConfig) models. os.Exit(1) } + // If user adds wallpaper + if a.Options.WpPath != "" { + wallpapersDir := path.Join(a.Options.OutputDir, "wallpapers") + // Check for wallpapers directory + _, statErr := os.Stat(wallpapersDir) + if statErr != nil { + mkdirErr := os.Mkdir(wallpapersDir, os.ModePerm) + if mkdirErr != nil { + dlog.Err(mkdirErr.Error()) + return models.CommandResult{ + Code: 1, + Message: "Could not create wallpapers folder", + } + } + } + + wpName := path.Join(wallpapersDir, path.Base(a.Options.WpPath)) + copyErr := utils.CopyFile(a.Options.WpPath, wpName) + if copyErr != nil { + dlog.Err(copyErr.Error()) + fmt.Println("Could not copy wallpaper") + } else { + fmt.Println("Wallpaper added") + relativePath := path.Join("wallpapers", path.Base(a.Options.WpPath)) + manifest.Wallpapers = append(manifest.Wallpapers, relativePath) + } + } + // Check if apps exist in package or not possibleAppNames := make([]string, 0) for _, appName := range opts.Arguments[1:] { @@ -56,7 +85,7 @@ func (a Add) ExecuteCommand(opts *models.Opts, config *models.AppConfig) models. } // Exit app if all apps are in package - if len(possibleAppNames) == 0 { + if len(possibleAppNames) == 0 && a.Options.WpPath == "" { fmt.Println("All packages are already in package") os.Exit(1) } @@ -79,7 +108,7 @@ func (a Add) ExecuteCommand(opts *models.Opts, config *models.AppConfig) models. } // If there are new apps - if len(addedApps) > 0 { + if len(addedApps) > 0 || a.Options.WpPath != "" { // Remove old manifest manifestPath := path.Join(opts.OutputDir, "manifest.json") removeErr := os.Remove(manifestPath) @@ -92,7 +121,6 @@ func (a Add) ExecuteCommand(opts *models.Opts, config *models.AppConfig) models. if !manifest.Modified { manifest.Modified = true - // Offer new version manifest.OfferNewVersion() } diff --git a/commands/commands.go b/commands/commands.go index 4af0692..1099bf0 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -54,7 +54,7 @@ func DispatchCommand(config *models.AppConfig, args []string) { // Handle command result := commands[args[0]].ExecuteCommand(&opts, config) if result.Code == 0 { - fmt.Printf("Command executed successfully:\n\t %s\n", result.Message) + fmt.Printf("Command executed successfully.\n\t %s\n", result.Message) os.Exit(0) } else { fmt.Printf("Error occured: %s\n", result.Message) diff --git a/commands/help.go b/commands/help.go index 800a416..9b47af3 100644 --- a/commands/help.go +++ b/commands/help.go @@ -21,6 +21,7 @@ func (h Help) ExecuteCommand(_ *models.Opts, _ *models.AppConfig) models.Command fmt.Println() fmt.Println(" dots-cli init Initializes new package in") fmt.Println(" dots-cli add Adds given apps to package") + fmt.Println(" dots-cli add -w Adds specified wallpaper to package") fmt.Println(" dots-cli remote add Adds new remote to package") fmt.Println(" dots-cli pack Makes package version") fmt.Println(" dots-cli push Pushes package to specified remote") diff --git a/commands/install.go b/commands/install.go index 8e9729a..ca964f4 100644 --- a/commands/install.go +++ b/commands/install.go @@ -41,6 +41,33 @@ func (i Install) ExecuteCommand(opts *models.Opts, config *models.AppConfig) mod } } + // Check for wallpaper directory + wpDir := os.ExpandEnv("$HOME/.local/share/backgrounds") + shouldCopyWallpapers := true + _, statErr := os.Stat(wpDir) + if statErr != nil { + shouldCopyWallpapers = false + mkdirErr := os.MkdirAll(wpDir, os.ModePerm) + if mkdirErr != nil { + dlog.Err(mkdirErr.Error()) + fmt.Printf("Could not create %s\n", wpDir) + fmt.Println("You can use wallpapers from current directory later.") + } else { + shouldCopyWallpapers = true + } + } + + if shouldCopyWallpapers { + for _, wallpaper := range manifest.Wallpapers { + localWpName := path.Join(wpDir, path.Base(wallpaper)) + copyErr := utils.CopyFile(wallpaper, localWpName) + if copyErr != nil { + dlog.Err(copyErr.Error()) + fmt.Printf("Could not copy %s to %s\n", wallpaper, localWpName) + } + } + } + // TODO: Handle mismatching versions // BACKUP for _, app := range manifest.Apps { diff --git a/handlerlist.json b/handlerlist.json index 0886606..cf1c17e 100644 --- a/handlerlist.json +++ b/handlerlist.json @@ -1,4 +1,11 @@ { + "vim": { + "Version": "", + "ConfigRoot": "", + "Dotfiles": [ + "" + ] + }, "i3": { "Version": "1.0.0", "ConfigRoot": "$HOME/.config/i3", @@ -6,29 +13,33 @@ "$HOME/.config/i3/config" ] }, - "niceapp": { - "Version": "54.2", - "ConfigRoot": "$HOME/.config/niceapp", + "emacs": { + "Version": "", + "ConfigRoot": "", + "Dotfiles": [ + "" + ] + }, + "xmonad": { + "Version": "0.15", + "ConfigRoot": "$HOME/.xmonad", "Dotfiles": [ - "$HOME/.config/niceapp/config.conf", - "$HOME/.config/niceapp/settings.json" + "$HOME/.xmonad/xmonad.hs" ] }, - "test": { - "Version": "0.1.5-build:2", - "ConfigRoot": "$HOME/test", + "zsh": { + "Version": "5.8", + "ConfigRoot": "$HOME", "Dotfiles": [ - "$HOME/test/salam.txt", - "$HOME/test/necesen.txt", - "$HOME/test/hmm/yaxsi.json" + "$HOME/.zshrc" ] }, - "i4": { - "Version": "1.0.4", - "ConfigRoot": "$HOME/.config/i4", + "nvim": { + "Version": "v0.5.0", + "ConfigRoot": "$HOME/.config/nvim", "Dotfiles": [ - "$HOME/.config/i4/config.conf", - "$HOME/.config/i4/includes/salam.txt" + "$HOME/.config/nvim/init.vim", + "$HOME/.config/nvim/init.lua" ] } } \ No newline at end of file diff --git a/models/models.go b/models/models.go index 8600bc7..eddfddb 100644 --- a/models/models.go +++ b/models/models.go @@ -20,30 +20,33 @@ type Opts struct { AuthorName string `long:"author-name" description:"Author name"` AuthorEmail string `long:"author-email" description:"Author email"` Installed bool `long:"installed" description:"Should app print only installed apps"` + WpPath string `short:"w" long:"wallpaper" description:"Adds wallpaper file"` //AppName string `short:"a" long:"app" description:"App to add to package"` Arguments []string } func NewManifest() Manifest { return Manifest{ - Id: "", - Name: "", - Author: Author{}, - Modified: true, - Versions: make([]Version, 0), - Apps: make([]App, 0), - Remotes: make([]RemoteAddr, 0), + Id: "", + Name: "", + Author: Author{}, + Modified: true, + Versions: make([]Version, 0), + Apps: make([]App, 0), + Remotes: make([]RemoteAddr, 0), + Wallpapers: make([]string, 0), } } type Manifest struct { - Id string - Name string - Author Author - Modified bool - Versions []Version - Apps []App - Remotes []RemoteAddr + Id string + Name string + Author Author + Modified bool + Versions []Version + Apps []App + Remotes []RemoteAddr + Wallpapers []string } type Version struct {