-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.go
65 lines (51 loc) · 1.37 KB
/
common.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
package common
import (
"fmt"
"log"
"os"
"os/user"
"path"
"github.com/1995parham/gosimac/core"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
const (
// FlagPath flag indicates where to store the wallpapers.
FlagPath = "path"
// FlagCount flag indicates number of fetching images from source.
FlagCount = "number"
// DefaultCount is a default number of fetching images from sources.
DefaultCount = 10
// DirectoryPermission used for creating GoSiMac Directory.
DirectoryPermission = 0755
)
// DefaultPath is a default path for storing the wallpapers.
func DefaultPath() string {
usr, err := user.Current()
if err != nil {
log.Fatalf("user.Current: %v", err)
}
p := path.Join(usr.HomeDir, "Pictures", "GoSiMac")
if _, err := os.Stat(p); err != nil {
if err := os.Mkdir(p, DirectoryPermission); err != nil {
log.Fatalf("os.Mkdir: %v", err)
}
}
return p
}
// Run runs given source on given path and waits for its results.
// this used by source specific sub commands.
func Run(s core.Source, cmd *cobra.Command) error {
p, err := cmd.Flags().GetString(FlagPath)
if err != nil {
return fmt.Errorf("flag path parse failed: %w", err)
}
pterm.Description.Printf("Source: %+v\n", s)
pterm.Description.Printf("Path: %s\n", p)
a := core.NewApp(p, s)
if err := a.Run(); err != nil {
return fmt.Errorf("enigne failed: %w", err)
}
a.Wait()
return nil
}