feat(GitignoreCmd): moves gitignore gen into its own command to gen separately, or --rewrite
This commit is contained in:
parent
e6d39e84d8
commit
66102cf090
1 changed files with 47 additions and 18 deletions
65
cmd/vcs.go
65
cmd/vcs.go
|
|
@ -31,6 +31,26 @@ var vcsInitCmd = &cobra.Command{
|
||||||
RunE: vcsInit,
|
RunE: vcsInit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var vcsGitignoreCmd = &cobra.Command{
|
||||||
|
Use: "gitignore",
|
||||||
|
Short: "generates a suitable .gitignore file",
|
||||||
|
SilenceUsage: true,
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
repoDir, err := ensureVCSInitialized()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rewrite, err := cmd.Flags().GetBool("rewrite")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return writeGitignore(repoDir, rewrite)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var vcsSnapshotCmd = &cobra.Command{
|
var vcsSnapshotCmd = &cobra.Command{
|
||||||
Use: "snapshot",
|
Use: "snapshot",
|
||||||
Short: "commit a snapshot into the vcs",
|
Short: "commit a snapshot into the vcs",
|
||||||
|
|
@ -165,6 +185,8 @@ var vcsPushCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
vcsInitCmd.Flags().Bool("clean", false, "Remove existing VCS directory before initialising")
|
vcsInitCmd.Flags().Bool("clean", false, "Remove existing VCS directory before initialising")
|
||||||
vcsCmd.AddCommand(vcsInitCmd)
|
vcsCmd.AddCommand(vcsInitCmd)
|
||||||
|
vcsGitignoreCmd.Flags().BoolP("rewrite", "r", false, "Rewrite existing .gitignore, if present")
|
||||||
|
vcsCmd.AddCommand(vcsGitignoreCmd)
|
||||||
vcsCmd.AddCommand(vcsSnapshotCmd)
|
vcsCmd.AddCommand(vcsSnapshotCmd)
|
||||||
vcsCmd.AddCommand(vcsLogCmd)
|
vcsCmd.AddCommand(vcsLogCmd)
|
||||||
vcsPullCmd.Flags().Bool("clean", false, "Remove all existing stores before restoring snapshots")
|
vcsPullCmd.Flags().Bool("clean", false, "Remove all existing stores before restoring snapshots")
|
||||||
|
|
@ -178,6 +200,7 @@ func vcsInit(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
clean, err := cmd.Flags().GetBool("clean")
|
clean, err := cmd.Flags().GetBool("clean")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -221,11 +244,7 @@ func vcsInit(cmd *cobra.Command, args []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := writeGitignore(repoDir); err != nil {
|
return vcsGitignoreCmd.RunE(cmd, args)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func vcsRepoRoot() (string, error) {
|
func vcsRepoRoot() (string, error) {
|
||||||
|
|
@ -254,24 +273,34 @@ func ensureVCSInitialized() (string, error) {
|
||||||
return repoDir, nil
|
return repoDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeGitignore(repoDir string) error {
|
func writeGitignore(repoDir string, rewrite bool) error {
|
||||||
path := filepath.Join(repoDir, ".gitignore")
|
path := filepath.Join(repoDir, ".gitignore")
|
||||||
content := strings.Join([]string{
|
if _, err := os.Stat(path); os.IsNotExist(err) || rewrite {
|
||||||
"# generated by pda",
|
content := strings.Join([]string{
|
||||||
"*",
|
"# generated by pda",
|
||||||
"!snapshots/",
|
"*",
|
||||||
"!snapshots/*.ndjson",
|
"!/",
|
||||||
"",
|
"!.gitignore",
|
||||||
}, "\n")
|
"",
|
||||||
return os.WriteFile(path, []byte(content), 0o640)
|
}, "\n")
|
||||||
|
if err := os.WriteFile(path, []byte(content), 0o640); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := runGit(repoDir, "add", ".gitignore"); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return runGit(repoDir, "commit", "--allow-empty", "-m", "generated gitignore")
|
||||||
|
}
|
||||||
|
fmt.Println("Existing .gitignore found.")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func snapshotDB(store *Store, repoDir, db string) error {
|
func snapshotDB(store *Store, repoDir, db string) error {
|
||||||
snapDir := filepath.Join(repoDir, "snapshots")
|
if err := os.MkdirAll(repoDir, 0o750); err != nil {
|
||||||
if err := os.MkdirAll(snapDir, 0o750); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
target := filepath.Join(snapDir, fmt.Sprintf("%s.ndjson", db))
|
target := filepath.Join(repoDir, fmt.Sprintf("%s.ndjson", db))
|
||||||
f, err := os.Create(target)
|
f, err := os.Create(target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -504,7 +533,7 @@ func autoCommit(store *Store, dbs []string, message string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := runGit(repoDir, "add", "snapshots"); err != nil {
|
if err := runGit(repoDir, "add", strings.Join(dbs, " ")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue