-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdates_check.go
133 lines (115 loc) · 3.41 KB
/
updates_check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"time"
"github.com/go-git/go-git/v5"
)
const LATESTCHECK = ".latestcheck"
func checkUpdated(base string, timeInterval time.Duration) {
trace("checkUpdated", base)
olaris_base := joinpath(base, getNuvBranch())
latest_check_path := joinpath(olaris_base, LATESTCHECK)
olaris_path := joinpath(olaris_base, "olaris")
// if no olaris dir, no update check
if !isDir(olaris_path) {
return
}
// get info on latest_check file
file, ok := checkLatestFile(latest_check_path)
if !ok {
return
}
mtime := file.ModTime()
now := time.Now().Local()
diff := now.Sub(mtime)
if diff >= timeInterval {
fmt.Println("Checking for updates...")
// touch latest_check file ONLY if enough time has passed
touchLatestCheckFile(latest_check_path)
// check if remote olaris is newer
if checkRemoteOlarisNewer(olaris_path) {
fmt.Print("New tasks available! Use 'nuv -update' to update.\n\n")
} else {
fmt.Print("Tasks up to date!\n\n")
}
}
}
func checkLatestFile(path string) (fs.FileInfo, bool) {
file, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
debug("latestcheck file not found, skipping update check")
return nil, false
} else if err != nil {
debug("failed to access .latestcheck file info", err)
return nil, false
}
return file, true
}
func createLatestCheckFile(base string) {
// create latest_check file
_, err := os.Create(joinpath(base, LATESTCHECK))
if err != nil {
warn("failed to set latest_check file", err)
}
}
func touchLatestCheckFile(latest_check_path string) {
trace("touch latest_check file", latest_check_path)
currentTime := time.Now().Local()
err := os.Chtimes(latest_check_path, currentTime, currentTime)
if err != nil {
warn("failed to set latest update check", err)
}
}
func checkRemoteOlarisNewer(olaris_path string) bool {
trace("checkRemoteOlarisNewer", olaris_path)
repo, err := git.PlainOpen(olaris_path)
if err != nil {
warn("failed to check olaris folder", err)
return false
}
localRef, err := repo.Head()
if err != nil {
warn("failed to check olaris folder", err)
return false
}
remote, err := repo.Remote("origin")
if err != nil {
warn("failed to check remote olaris", err)
return false
}
_ = remote.Fetch(&git.FetchOptions{})
remoteRefs, err := remote.List(&git.ListOptions{})
if err != nil {
warn("failed to check remote olaris", err)
return false
}
// check ref is in refs
for _, remoteRef := range remoteRefs {
if localRef.Name().String() == remoteRef.Name().String() {
// is hash different?
if localRef.Hash().String() != remoteRef.Hash().String() {
return true
}
}
}
return false
}