github.com/erroneousboat/dot@v0.0.0-20170402193747-d2fd504d98ec/helpers.go (about) 1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "os" 8 "os/exec" 9 "strings" 10 11 "github.com/fatih/color" 12 homedir "github.com/mitchellh/go-homedir" 13 ) 14 15 // PrintHeader will print out a colourful header given a string 16 func PrintHeader(text string) { 17 color := color.New(color.FgYellow, color.Bold) 18 color.Printf("==> %s\n", text) 19 } 20 21 // PrintBody will print out a colourful body given a string 22 func PrintBody(text string) { 23 color := color.New(color.FgCyan) 24 color.Printf("... %s\n", text) 25 } 26 27 // PrintBodyBold will print out a colourful body give a string 28 func PrintBodyBold(text string) { 29 color := color.New(color.FgCyan, color.Bold) 30 color.Printf("... %s\n", text) 31 } 32 33 // PrintBodyError will print out a colourful error given a string 34 func PrintBodyError(text string) { 35 color := color.New(color.FgRed) 36 color.Printf("... ERROR: %s\n", text) 37 } 38 39 // HomeDir return the home directory of the logged in user. 40 func HomeDir() string { 41 dir, err := homedir.Dir() 42 if err != nil { 43 log.Fatal(err) 44 } 45 46 return dir 47 } 48 49 // GetRelativePathFromCwd will remove the home folder from the current working 50 // directory: 51 // `/home/erroneousboat/dotfiles/` will become `dotfiles/` 52 func GetRelativePathFromCwd() (string, error) { 53 // get current working directory, this returns absolute path 54 currentWorkingDir, err := os.Getwd() 55 if err != nil { 56 return "", err 57 } 58 59 // remove HomeDir() from the currentWorkingDir to get relative path 60 relPath := strings.Split(currentWorkingDir, HomeDir()) 61 62 if len(relPath) != 2 { 63 err := errors.New("not able to uncover relative path") 64 return "", err 65 } else { 66 return relPath[1], nil 67 } 68 } 69 70 // GetRelativePath will remove the home folder from the argument `fullPath`: 71 // `/home/erroneousboat/.config/nvim` will become `.config/nvim` 72 func GetRelativePath(fullPath string) (string, error) { 73 relPath := strings.Split(fullPath, HomeDir()) 74 75 if len(relPath) != 2 { 76 err := errors.New("not able to uncover relative path") 77 return "", err 78 } 79 80 return relPath[1], nil 81 } 82 83 // GitCommitPush will execute the command git commit -a -m [message] and 84 // git push origin. This function will be called when a user specifies it 85 // wants to commit the changes made to its repository in the form of the 86 // `-p` flag used in combination with the `dot add` and `dot rm` commands. 87 func GitCommitPush(name, action string) { 88 89 // load config 90 c, err := NewConfig(PathDotConfig) 91 if err != nil { 92 message := fmt.Sprintf("not able to load config file. Make sure the " + 93 ".dotconfig file is present and points to the correct location") 94 PrintBodyError(message) 95 return 96 } 97 98 // change current working directory to DotPath 99 os.Chdir(c.DotPath) 100 101 PrintHeader("Committing changes to repository ...") 102 103 // setup git command 104 cmd := "git" 105 106 // execute git add 107 addArgs := []string{"add", "-A"} 108 cmdGitAddOutput, err := exec.Command(cmd, addArgs...).Output() 109 if err != nil { 110 PrintBodyError("something went wrong with adding the changes " + 111 "to the repository, see the output below:") 112 log.Println(cmdGitAddOutput) 113 log.Fatalln(err) 114 } 115 116 // setup commit arguments 117 commitArgs := []string{"commit", "-a", "-m"} 118 119 // set commit message 120 var commitMessage string 121 if action == "add" { 122 commitMessage = fmt.Sprintf("%s: added %s for tracking", name, name) 123 } else if action == "rm" { 124 commitMessage = fmt.Sprintf("%s: removed %s from tracking", name, name) 125 } 126 127 // add commitMessage to the commitArgs 128 commitArgs = append(commitArgs, commitMessage) 129 130 message := fmt.Sprintf("Committing changes for: %s", name) 131 PrintBody(message) 132 133 // execute git commit 134 cmdGitCommitOutput, err := exec.Command(cmd, commitArgs...).Output() 135 if err != nil { 136 PrintBodyError("something went wrong with commiting the changes " + 137 "to the repository, see the output below:") 138 log.Println(cmdGitCommitOutput) 139 log.Fatalln(err) 140 } 141 142 // execute git push 143 PrintBody("Pushing changes to repository") 144 pushArgs := []string{"push", "origin"} 145 cmdGitPushOutput, err := exec.Command(cmd, pushArgs...).Output() 146 if err != nil { 147 PrintBodyError("something went wrong with pushing the changes " + 148 "to the repository, see the output below:") 149 log.Println(cmdGitPushOutput) 150 log.Fatalln(err) 151 } 152 153 return 154 }