diff --git a/cmd/init.go b/cmd/init.go index cb41060..44ea98c 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -13,6 +13,26 @@ func init() { RootCmd.AddCommand(initCommand) } +func backupExistingConfigs(programs []string) { + // takes list of programs and backs up configs for them + configRoot := os.Getenv("HOME") + "/.config/" + for _, program := range(programs) { + // TODO: do something here + print(configRoot + program) + } +} + +func createDotfileStructure(programs []string) { + // takes list of programs and creates dotfiles for them + dotfileRoot := os.Getenv("HOME") + "/.dotfiles/" + for _, program := range(programs) { + fmt.Printf("attempting to create directory %s%s\n", dotfileRoot, program) + if err := os.MkdirAll(dotfileRoot + program, os.ModePerm); err != nil { + log.Fatal(err) + } + } +} + var initCommand = &cobra.Command { Use: "init", Run: func(cmd *cobra.Command, args []string) { @@ -42,7 +62,7 @@ var initCommand = &cobra.Command { for _, acceptedfile := range(acceptedfiles) { if path == rootpath + acceptedfile { - files = append(files, path) + files = append(files, path[len(rootpath):]) } } return nil @@ -54,8 +74,10 @@ var initCommand = &cobra.Command { fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n") for _, file := range(files) { - fmt.Fprintf(cmd.OutOrStdout(), file[len(rootpath):] + "\n" ) + fmt.Fprintf(cmd.OutOrStdout(), file + "\n" ) } + + createDotfileStructure(files) }, } diff --git a/test/copy_test.go b/test/copy_test.go new file mode 100644 index 0000000..e23b0d6 --- /dev/null +++ b/test/copy_test.go @@ -0,0 +1,3 @@ +package test + + diff --git a/tools/copy.go b/tools/copy.go new file mode 100644 index 0000000..69ce011 --- /dev/null +++ b/tools/copy.go @@ -0,0 +1,54 @@ +package tools + +import ( + "fmt" + "io" + "os" + "path/filepath" +) + +func CopyFile(srcFile, destFile string) error{ + // helper function to copy files over + sourceFileStat, err := os.Stat(srcFile) + if err != nil { + return err + } + + if !sourceFileStat.Mode().IsRegular() { + return fmt.Errorf("%s is not a regular file", srcFile) + } + + source, err := os.Open(srcFile) + if err != nil { + return err + } + defer source.Close() + + destination, err := os.Create(destFile) + if err != nil { + return err + } + defer destination.Close() + _, err = io.Copy(destination, source) + + return err +} + +func CopyDir(srcDir, destDir string) error { + entries, err := os.ReadDir(srcDir) + if err != nil { + return err + } + + for _, entry := range(entries) { + sourcePath := filepath.Join(srcDir, entry.Name()) + destPath := filepath.Join(destDir, entry.Name()) + + err := CopyFile(sourcePath, destPath) + if err != nil { + return err + } + } + + return nil +}