Skip to content

Commit

Permalink
Wallpaper support implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
alvanrahimli committed Aug 22, 2021
1 parent ff01af4 commit 587a6e6
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}

Expand All @@ -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:] {
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions commands/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (h Help) ExecuteCommand(_ *models.Opts, _ *models.AppConfig) models.Command
fmt.Println()
fmt.Println(" dots-cli init <pack_name> Initializes new package in")
fmt.Println(" dots-cli add <app1> <app2> Adds given apps to package")
fmt.Println(" dots-cli add -w <image_path> Adds specified wallpaper to package")
fmt.Println(" dots-cli remote add <remote_url> Adds new remote to package")
fmt.Println(" dots-cli pack Makes package version")
fmt.Println(" dots-cli push <remote_name> Pushes package to specified remote")
Expand Down
27 changes: 27 additions & 0 deletions commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
43 changes: 27 additions & 16 deletions handlerlist.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
{
"vim": {
"Version": "",
"ConfigRoot": "",
"Dotfiles": [
""
]
},
"i3": {
"Version": "1.0.0",
"ConfigRoot": "$HOME/.config/i3",
"Dotfiles": [
"$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"
]
}
}
31 changes: 17 additions & 14 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 587a6e6

Please sign in to comment.