Skip to content

Commit 2ec75bb

Browse files
authored
Fix the error with pull latest hd-home (#23)
1 parent 8ad71c5 commit 2ec75bb

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

cmd/get.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type downloadOption struct {
4646
URL string
4747
Output string
4848
ShowProgress bool
49+
Fetch bool
4950

5051
ContinueAt int64
5152

@@ -236,7 +237,10 @@ func (o *downloadOption) fetchHomeConfig() (err error) {
236237
userHome, _ := homedir.Dir()
237238
configDir := userHome + "/.config/hd-home"
238239
if ok, _ := pathExists(configDir); ok {
239-
err = execCommand("git", "pull", "-C", configDir)
240+
err = execCommandInDir("git", configDir, "reset", "--hard", "origin/master")
241+
if err == nil {
242+
err = execCommandInDir("git", configDir, "pull")
243+
}
240244
} else {
241245
if err = os.MkdirAll(configDir, 0644); err == nil {
242246
err = execCommand("git", "clone", "https://github.com/LinuxSuRen/hd-home", configDir)

cmd/install.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func NewInstallCmd() (cmd *cobra.Command) {
2828
//flags.StringVarP(&opt.Mode, "mode", "m", "package",
2929
// "If you want to install it via platform package manager")
3030
flags.BoolVarP(&opt.ShowProgress, "show-progress", "", true, "If show the progress of download")
31+
flags.BoolVarP(&opt.Fetch, "fetch", "", true,
32+
"If fetch the latest config from https://github.com/LinuxSuRen/hd-home")
3133
flags.IntVarP(&opt.Thread, "thread", "t", 4,
3234
`Download file with multi-threads. It only works when its value is bigger than 1`)
3335
flags.BoolVarP(&opt.KeepPart, "keep-part", "", false,
@@ -44,9 +46,11 @@ type installOption struct {
4446
}
4547

4648
func (o *installOption) preRunE(cmd *cobra.Command, args []string) (err error) {
47-
if err = o.fetchHomeConfig(); err != nil {
48-
// this is not a fatal, don't block the process
49-
cmd.Printf("Failed with fetching home config: %v\n", err)
49+
if o.Fetch {
50+
if err = o.fetchHomeConfig(); err != nil {
51+
err = fmt.Errorf("failed with fetching home config: %v", err)
52+
return
53+
}
5054
}
5155
err = o.downloadOption.preRunE(cmd, args)
5256
return
@@ -153,8 +157,11 @@ func (o *installOption) extractFiles(tarFile, targetName string) (err error) {
153157
return
154158
}
155159

156-
func execCommand(name string, arg ...string) (err error) {
160+
func execCommandInDir(name, dir string, arg ...string) (err error) {
157161
command := exec.Command(name, arg...)
162+
if dir != "" {
163+
command.Dir = dir
164+
}
158165

159166
//var stdout []byte
160167
//var errStdout error
@@ -183,6 +190,10 @@ func execCommand(name string, arg ...string) (err error) {
183190
return
184191
}
185192

193+
func execCommand(name string, arg ...string) (err error) {
194+
return execCommandInDir(name, "", arg...)
195+
}
196+
186197
func copyAndCapture(w io.Writer, r io.Reader) ([]byte, error) {
187198
var out []byte
188199
buf := make([]byte, 1024, 1024)

0 commit comments

Comments
 (0)