Skip to content

Commit 0efc3ff

Browse files
author
permito
committed
Implemented self-update
1 parent f2eb38c commit 0efc3ff

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

hp/cmd/update.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cmd
2+
3+
import (
4+
"HeaderPuller/hp"
5+
"errors"
6+
"github.com/urfave/cli/v2"
7+
"os"
8+
"os/exec"
9+
"strconv"
10+
"time"
11+
)
12+
13+
var UpdateCmd = &cli.Command{
14+
Name: "update",
15+
Aliases: []string{"u"},
16+
Usage: "Update to the latest git commit",
17+
Action: func(cCtx *cli.Context) error {
18+
executablePath, err := exec.LookPath("hp")
19+
if err != nil {
20+
return errors.New("hp executable not found, update unable to finish")
21+
}
22+
23+
tmp := strconv.Itoa(time.Now().Nanosecond())
24+
if err := exec.Command("git", "clone", "https://github.com/Depermitto/HeaderPuller", tmp).Run(); err != nil {
25+
return errors.New("cannot clone repo, update unable to finish")
26+
}
27+
defer os.RemoveAll(tmp)
28+
29+
if err := exec.Command("go", "build", "-C", tmp, "-o", executablePath).Run(); err != nil {
30+
return errors.New("errors found in building the executable, update unable to finish")
31+
}
32+
return hp.NoErrUpdated
33+
},
34+
}

hp/hp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
IncludeDir = "include"
1010
PathSep = string(os.PathSeparator)
1111
Perm = 0755
12+
RepoLink = "https://github.com/Depermitto/HeaderPuller"
1213
)
1314

1415
var (
@@ -21,4 +22,5 @@ var (
2122
NoErrWiped = errors.New("wipe successful")
2223
NoErrRemoved = errors.New("removal successful")
2324
NoErrSynced = errors.New("sync successful")
25+
NoErrUpdated = errors.New("update successful")
2426
)

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func main() {
2020
cmd.SyncCmd,
2121
cmd.RemoveCmd,
2222
cmd.WipeCmd,
23+
cmd.UpdateCmd,
2324
cmd.VersionCmd,
2425
cmd.UninstallCmd,
2526
}}

0 commit comments

Comments
 (0)