This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
/
user_add.go
117 lines (97 loc) · 3.05 KB
/
user_add.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cmd
import (
"os"
"path/filepath"
"strings"
"github.com/eankeen/dotty/internal/util"
"github.com/eankeen/go-logger"
"github.com/spf13/cobra"
)
// assume if type is 'file' or 'folder'
func addFile(filePath string, fileType string) {
if fileType == "file" {
stat, err := os.Stat(filePath)
// we get error if the file doesn't exist
if err != nil {
err := os.MkdirAll(filepath.Dir(filePath), 0755)
util.HandleFsError(err)
newFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0644)
util.HandleError(err)
defer func() {
err := newFile.Close()
util.HandleFsError(err)
}()
logger.Informational("Creating file at '%s'\n", filePath)
return
}
if stat.IsDir() {
// TODO: automatically replace folder with file if it is empty
logger.Error("'%s' exists and is a folder. Please remove folder before continuing\n", filePath)
return
}
logger.Notice("'%s' already exists\n", filePath)
} else if fileType == "folder" {
stat, err := os.Stat(filePath)
// we get error if the file doesn't exist
if err != nil {
logger.Informational("Creating folder at '%s'\n", filePath)
err := os.MkdirAll(filePath, 0755)
util.HandleFsError(err)
return
}
if !stat.IsDir() {
// TODO: automatically replace file with folder if it is empty
logger.Error("'%s' exists and is a file. Please remove the file\n", filePath)
return
}
logger.Notice("Folder '%s' already exists. Not doing anything\n", filePath)
} else {
logger.Error("fileType not either 'file' or 'folder'\n")
}
}
var userAddCmd = &cobra.Command{
Use: "add",
Short: "Add a dotfile",
Long: "Adds a single dotfile",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
os.Stderr.WriteString("Error: Must add a path to add\n")
os.Exit(1)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
userDir := cmd.Flag("user-dir").Value.String()
dotDir := cmd.Flag("dot-dir").Value.String()
fileType := cmd.Flag("type").Value.String()
if !(fileType == "folder" || fileType == "file") {
logger.Error("Type must either be 'file' or 'folder'")
}
newPath := args[0]
// we place this before userDir because dotDir may include
// the contents of userDir, plus an extra path
if strings.HasPrefix(newPath, dotDir) {
addFile(newPath, fileType)
return
}
// test if has userDir prefix, replace it with dotDir
if strings.HasPrefix(newPath, userDir) {
str := filepath.Join(dotDir, "user", newPath[len(userDir):])
addFile(str, fileType)
return
}
if filepath.IsAbs(newPath) {
logger.Error("Your path was not recognized. It must be either a relative path (relative to the dotDir) or an absolute path that has a prefix of your dotDir or userDir")
return
}
// if path not absolute, assume it is relative to dotDir
newPath = filepath.Join(dotDir, "user", newPath)
addFile(newPath, fileType)
},
}
func init() {
userCmd.AddCommand(userAddCmd)
pf := userAddCmd.PersistentFlags()
pf.String("type", "", "Type of the newly added path")
cobra.MarkFlagRequired(pf, "type")
}