-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_add.go
95 lines (78 loc) · 2.74 KB
/
cmd_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
package main
import (
"context"
"flag"
"log/slog"
"os"
"path/filepath"
"strings"
"github.com/google/subcommands"
)
type addCmd struct {
// localName is the name of the local file.
localName string
}
func (a *addCmd) Name() string {
return "add"
}
func (a *addCmd) Synopsis() string {
return "Start tracking a new dotfile"
}
func (a *addCmd) Usage() string {
return `add:
Start tracking a new dotfile.
`
}
func (a *addCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&a.localName, "local", "", "The name of the local file within your home directory.")
}
func (a *addCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
// Get the dotfiles in the local directory that have the `dot_` prefix.
// We are assuming that the user is running this command from the root of their repository.
// Is the current directory a git repository?
// If not, return an error.
if !isGitRepo() {
slog.Error("Current directory is not a git repository")
return subcommands.ExitFailure
}
if a.localName == "" {
slog.Error("The local flag is required")
return subcommands.ExitFailure
}
// See if the file is already being tracked.
// If it is, return an error.
if stat, err := os.Stat(strings.Replace(a.localName, ".", "dot_", 1)); !os.IsNotExist(err) {
slog.Error("The file is already being tracked", slog.String(loggingKeyFile, a.localName))
return subcommands.ExitFailure
} else if err != nil {
slog.Error("Error checking if the file is already being tracked", slog.String(loggingKeyError, err.Error()))
return subcommands.ExitFailure
} else if stat.Name() != "" {
slog.Error("The file is already being tracked", slog.String(loggingKeyFile, a.localName))
return subcommands.ExitFailure
}
// Find the users home directory path.
homeDir, err := os.UserHomeDir()
if err != nil {
slog.Error("Error getting user home directory", slog.String(loggingKeyError, err.Error()))
return subcommands.ExitFailure
}
// Get the absolute path of the file in the home directory.
homeDotPath := filepath.Join(homeDir, a.localName)
// Does the file exist?
// If not, return an error.
if _, err := os.Stat(homeDotPath); os.IsNotExist(err) {
slog.Error("The file does not exist", slog.String(loggingKeyFile, homeDotPath))
return subcommands.ExitFailure
} else if err != nil {
slog.Error("Error checking if the file exists", slog.String(loggingKeyError, err.Error()))
return subcommands.ExitFailure
}
// Add the file to the repository.
if err := addFile(homeDotPath); err != nil {
slog.Error("Error adding file to the repository", slog.String(loggingKeyError, err.Error()))
return subcommands.ExitFailure
}
slog.Info("File added to the repository", slog.String(loggingKeyFile, homeDotPath))
return subcommands.ExitSuccess
}