add copy functionality

pull/3/head
Marcus Kok 2 years ago
parent e2b88f11d9
commit 77ef043a73

@ -13,6 +13,26 @@ func init() {
RootCmd.AddCommand(initCommand) 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 { var initCommand = &cobra.Command {
Use: "init", Use: "init",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -42,7 +62,7 @@ var initCommand = &cobra.Command {
for _, acceptedfile := range(acceptedfiles) { for _, acceptedfile := range(acceptedfiles) {
if path == rootpath + acceptedfile { if path == rootpath + acceptedfile {
files = append(files, path) files = append(files, path[len(rootpath):])
} }
} }
return nil return nil
@ -54,8 +74,10 @@ var initCommand = &cobra.Command {
fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n") fmt.Fprintf(cmd.OutOrStdout(), "binaries installed: \n =======================\n")
for _, file := range(files) { for _, file := range(files) {
fmt.Fprintf(cmd.OutOrStdout(), file[len(rootpath):] + "\n" ) fmt.Fprintf(cmd.OutOrStdout(), file + "\n" )
} }
createDotfileStructure(files)
}, },
} }

@ -0,0 +1,3 @@
package test

@ -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
}
Loading…
Cancel
Save