This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (82 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"strings"
"github.com/tsivinsky/walle/internal/pkg/config"
"github.com/tsivinsky/walle/internal/pkg/wallpaper"
)
// flags
var (
image string
setImageImmediately bool
restoringWallpaper bool
)
func main() {
flag.StringVar(&image, "i", "", "walle -i ./path/to/image.png")
flag.BoolVar(&restoringWallpaper, "restore", false, "walle --restore")
flag.BoolVar(&setImageImmediately, "s", false, "walle -i ./path/to/image.png -s")
flag.Parse()
err := config.CreateConfigPathIfNotExist()
if err != nil {
log.Fatal(err)
}
if image == "" && !restoringWallpaper {
flag.Usage()
os.Exit(1)
}
conf, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}
if restoringWallpaper {
imagePath := conf.ImagePath
if imagePath == "" {
fmt.Println("First, you need to call `walle -i ./path/to/image.png` to set some wallpaper")
os.Exit(1)
}
err = wallpaper.SetImage(imagePath)
if err != nil {
log.Fatal(err)
}
} else if image != "" {
if strings.HasPrefix(image, "http") {
imageData, ext, err := wallpaper.GetImageFromHTTPUri(image)
if err != nil {
log.Fatal(err)
}
filePath, err := wallpaper.SaveHTTPWallpaper(imageData, ext)
if err != nil {
log.Fatal(err)
}
conf.ImagePath = filePath
err = conf.Save()
if err != nil {
log.Fatal(err)
}
} else {
if !strings.HasPrefix(image, "/") {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
image = path.Join(cwd, image)
}
if setImageImmediately {
err = wallpaper.SetImage(image)
if err != nil {
log.Fatal(err)
}
} else {
conf.ImagePath = image
err = conf.Save()
if err != nil {
log.Fatal(err)
}
}
}
}
}