detached 7899ebd

Remove digest compare for version check, too painful

commit
7899ebdf848e50a017b03902226ac512a4b7ba88
author
bnema · · 2023-11-03 16:39 UTC
parent
789e34d
signature
unsigned
4 files · +16 -94
M cmd/cli/main.go +1 -8
@@ -9,7 +9,6 @@ import (
99 "github.com/bnema/gordon/internal/cli/cmd"
1010 "github.com/bnema/gordon/internal/common"
1111 "github.com/bnema/gordon/internal/server"
12- "github.com/bnema/gordon/pkg/docker"
1312 "github.com/spf13/cobra"
1413 )
1514
@@ -52,13 +51,7 @@ func main() {
5251 ProxyURL: "https://gordon-proxy.bnema.dev",
5352 }
5453
55- digest, err := docker.WhoAmI()
56- if err != nil {
57- log.Println(err)
58- }
59-
60- fmt.Printf("Gordon version %s (%s) built on %s\n", s.Config.Build.BuildVersion, digest, s.Config.Build.BuildDate)
61-
54+ fmt.Printf("Gordon version %\n", s.Config.Build.BuildVersion)
6255 go func() {
6356 msg, err := common.CheckVersionPeriodically(&s.Config)
6457 if err != nil {
@@ -9,7 +9,6 @@ import (
9 "github.com/bnema/gordon/internal/cli/cmd"9 "github.com/bnema/gordon/internal/cli/cmd"
10 "github.com/bnema/gordon/internal/common"10 "github.com/bnema/gordon/internal/common"
11 "github.com/bnema/gordon/internal/server"11 "github.com/bnema/gordon/internal/server"
12- "github.com/bnema/gordon/pkg/docker"
13 "github.com/spf13/cobra"12 "github.com/spf13/cobra"
14 )13 )
15 14
@@ -52,13 +51,7 @@ func main() {
52 ProxyURL: "https://gordon-proxy.bnema.dev",51 ProxyURL: "https://gordon-proxy.bnema.dev",
53 }52 }
54 53
55- digest, err := docker.WhoAmI()54+ fmt.Printf("Gordon version %\n", s.Config.Build.BuildVersion)
56- if err != nil {
57- log.Println(err)
58- }
59-
60- fmt.Printf("Gordon version %s (%s) built on %s\n", s.Config.Build.BuildVersion, digest, s.Config.Build.BuildDate)
61-
62 go func() {55 go func() {
63 msg, err := common.CheckVersionPeriodically(&s.Config)56 msg, err := common.CheckVersionPeriodically(&s.Config)
64 if err != nil {57 if err != nil {
M internal/common/initconfig.go +5 -8
@@ -58,24 +58,21 @@ var (
5858 )
5959
6060 func getConfigDir() (string, error) {
61- if inContainer, err := docker.IsRunningInContainer(); err != nil {
62- return "", fmt.Errorf("error checking if running in a container: %w", err)
63- } else if inContainer {
61+
62+ if docker.IsRunningInContainer() {
6463 return "/.", nil
6564 }
6665
67- // For non-container environments, determine the user's home directory
66+ // Get the user's home directory for non-container environments
6867 homeDir, err := os.UserHomeDir()
6968 if err != nil {
7069 return "", fmt.Errorf("error getting user's home directory: %w", err)
7170 }
7271
73- // Determine the configuration directory based on the operating system
74- var configDir string
72+ // Select the configuration directory based on the OS
73+ configDir := filepath.Join(homeDir, ".config", "Gordon")
7574 if runtime.GOOS == "windows" {
7675 configDir = filepath.Join(homeDir, "AppData", "Roaming", "Gordon")
77- } else {
78- configDir = filepath.Join(homeDir, ".config", "Gordon")
7976 }
8077
8178 return configDir, nil
@@ -58,24 +58,21 @@ var (
58 )58 )
59 59
60 func getConfigDir() (string, error) {60 func getConfigDir() (string, error) {
61- if inContainer, err := docker.IsRunningInContainer(); err != nil {61+
62- return "", fmt.Errorf("error checking if running in a container: %w", err)62+ if docker.IsRunningInContainer() {
63- } else if inContainer {
64 return "/.", nil63 return "/.", nil
65 }64 }
66 65
67- // For non-container environments, determine the user's home directory66+ // Get the user's home directory for non-container environments
68 homeDir, err := os.UserHomeDir()67 homeDir, err := os.UserHomeDir()
69 if err != nil {68 if err != nil {
70 return "", fmt.Errorf("error getting user's home directory: %w", err)69 return "", fmt.Errorf("error getting user's home directory: %w", err)
71 }70 }
72 71
73- // Determine the configuration directory based on the operating system72+ // Select the configuration directory based on the OS
74- var configDir string73+ configDir := filepath.Join(homeDir, ".config", "Gordon")
75 if runtime.GOOS == "windows" {74 if runtime.GOOS == "windows" {
76 configDir = filepath.Join(homeDir, "AppData", "Roaming", "Gordon")75 configDir = filepath.Join(homeDir, "AppData", "Roaming", "Gordon")
77- } else {
78- configDir = filepath.Join(homeDir, ".config", "Gordon")
79 }76 }
80 77
81 return configDir, nil78 return configDir, nil
M internal/common/version.go +8 -41
@@ -14,34 +14,16 @@ import (
1414 )
1515
1616 type CurrentVersion struct {
17- isDocker bool
18- Version string `json:"version"`
19- Commit string `json:"commit"`
20- Date string `json:"date"`
21- Digest string `json:"digest"`
17+ isContainer bool
18+ Version string `json:"version"`
2219 }
2320
24-// {
25-// "amd64": {
26-// "name": "0.0.951-amd64",
27-// "digest": "sha256:8486058cea46c5a18bc73d19446d6590b8b49cfadd7128ea443abbd69c11ea83"
28-// },
29-// "arm64": {
30-// "name": "0.0.951-arm64",
31-// "digest": "sha256:d9c2749117711e0bccd9cf3a3ac36ef7c4aa5487046e584640f2467add57a829"
32-// }
33-// }
34-// VersionInfo represents the JSON structure of the version endpoint response.
35-
36-// ArchVersion represents the architecture-specific version details.
3721 type ArchVersion struct {
38- Name string `json:"name"`
39- Digest string `json:"digest"`
22+ Name string `json:"name"`
4023 }
4124
4225 type VersionInfo struct {
43- Name string `json:"name"`
44- Digest string `json:"digest"`
26+ Name string `json:"name"`
4527 }
4628
4729 type VersionsResponse struct {
@@ -50,23 +32,10 @@ type VersionsResponse struct {
5032 }
5133
5234 func GetCurrentBuildVersionInfo(c *Config) CurrentVersion {
53- // Check if the Docker client has been initialized .iscontainer should be at root level
54- isContainer, err := docker.IsRunningInContainer()
55- if err != nil {
56- log.Println(err)
57- }
58-
59- digest, err := docker.WhoAmI()
60- if err != nil {
61- log.Println(err)
62- }
6335
6436 return CurrentVersion{
65- isDocker: isContainer,
66- Version: c.Build.BuildVersion,
67- Commit: c.Build.BuildCommit,
68- Date: c.Build.BuildDate,
69- Digest: digest,
37+ isContainer: docker.IsRunningInContainer(),
38+ Version: c.Build.BuildVersion,
7039 }
7140 }
7241
@@ -91,7 +60,7 @@ func CheckForNewVersion(local CurrentVersion, remote VersionsResponse) string {
9160
9261 // Compare the normalized local version with the remote version
9362 if localVersion != remoteVersion {
94- return "a new version is available"
63+ return "A new version is available"
9564 }
9665 return ""
9766 }
@@ -123,7 +92,7 @@ func CheckVersionPeriodically(c *Config) (string, error) {
12392
12493 message := CheckForNewVersion(localVersion, remoteVersion)
12594 if message != "" {
126- log.Printf("New version available: %s", message)
95+ log.Print(message)
12796 // TODO: Add a notification
12897 }
12998 }
@@ -134,8 +103,6 @@ func CheckVersionPeriodically(c *Config) (string, error) {
134103 func getLocalVersion(c *Config) CurrentVersion {
135104 return CurrentVersion{
136105 Version: c.Build.BuildVersion,
137- Commit: c.Build.BuildCommit,
138- Date: c.Build.BuildDate,
139106 }
140107 }
141108
@@ -14,34 +14,16 @@ import (
14 )14 )
15 15
16 type CurrentVersion struct {16 type CurrentVersion struct {
17- isDocker bool17+ isContainer bool
18- Version string `json:"version"`18+ Version string `json:"version"`
19- Commit string `json:"commit"`
20- Date string `json:"date"`
21- Digest string `json:"digest"`
22 }19 }
23 20
24-// {
25-// "amd64": {
26-// "name": "0.0.951-amd64",
27-// "digest": "sha256:8486058cea46c5a18bc73d19446d6590b8b49cfadd7128ea443abbd69c11ea83"
28-// },
29-// "arm64": {
30-// "name": "0.0.951-arm64",
31-// "digest": "sha256:d9c2749117711e0bccd9cf3a3ac36ef7c4aa5487046e584640f2467add57a829"
32-// }
33-// }
34-// VersionInfo represents the JSON structure of the version endpoint response.
35-
36-// ArchVersion represents the architecture-specific version details.
37 type ArchVersion struct {21 type ArchVersion struct {
38- Name string `json:"name"`22+ Name string `json:"name"`
39- Digest string `json:"digest"`
40 }23 }
41 24
42 type VersionInfo struct {25 type VersionInfo struct {
43- Name string `json:"name"`26+ Name string `json:"name"`
44- Digest string `json:"digest"`
45 }27 }
46 28
47 type VersionsResponse struct {29 type VersionsResponse struct {
@@ -50,23 +32,10 @@ type VersionsResponse struct {
50 }32 }
51 33
52 func GetCurrentBuildVersionInfo(c *Config) CurrentVersion {34 func GetCurrentBuildVersionInfo(c *Config) CurrentVersion {
53- // Check if the Docker client has been initialized .iscontainer should be at root level
54- isContainer, err := docker.IsRunningInContainer()
55- if err != nil {
56- log.Println(err)
57- }
58-
59- digest, err := docker.WhoAmI()
60- if err != nil {
61- log.Println(err)
62- }
63 35
64 return CurrentVersion{36 return CurrentVersion{
65- isDocker: isContainer,37+ isContainer: docker.IsRunningInContainer(),
66- Version: c.Build.BuildVersion,38+ Version: c.Build.BuildVersion,
67- Commit: c.Build.BuildCommit,
68- Date: c.Build.BuildDate,
69- Digest: digest,
70 }39 }
71 }40 }
72 41
@@ -91,7 +60,7 @@ func CheckForNewVersion(local CurrentVersion, remote VersionsResponse) string {
91 60
92 // Compare the normalized local version with the remote version61 // Compare the normalized local version with the remote version
93 if localVersion != remoteVersion {62 if localVersion != remoteVersion {
94- return "a new version is available"63+ return "A new version is available"
95 }64 }
96 return ""65 return ""
97 }66 }
@@ -123,7 +92,7 @@ func CheckVersionPeriodically(c *Config) (string, error) {
123 92
124 message := CheckForNewVersion(localVersion, remoteVersion)93 message := CheckForNewVersion(localVersion, remoteVersion)
125 if message != "" {94 if message != "" {
126- log.Printf("New version available: %s", message)95+ log.Print(message)
127 // TODO: Add a notification96 // TODO: Add a notification
128 }97 }
129 }98 }
@@ -134,8 +103,6 @@ func CheckVersionPeriodically(c *Config) (string, error) {
134 func getLocalVersion(c *Config) CurrentVersion {103 func getLocalVersion(c *Config) CurrentVersion {
135 return CurrentVersion{104 return CurrentVersion{
136 Version: c.Build.BuildVersion,105 Version: c.Build.BuildVersion,
137- Commit: c.Build.BuildCommit,
138- Date: c.Build.BuildDate,
139 }106 }
140 }107 }
141 108
M pkg/docker/detect.go +2 -37
@@ -1,9 +1,7 @@
11 package docker
22
33 import (
4- "bufio"
54 "os"
6- "strings"
75 )
86
97 func fileExists(filepath string) bool {
@@ -11,40 +9,7 @@ func fileExists(filepath string) bool {
119 return !os.IsNotExist(err)
1210 }
1311
14-func checkCgroup() (bool, error) {
15- // Open the cgroup file for the init process
16- file, err := os.Open("/proc/1/cgroup")
17- if err != nil {
18- return false, err
19- }
20- defer file.Close()
21-
22- // Scan through the cgroup file looking for docker or kube pod paths
23- scanner := bufio.NewScanner(file)
24- for scanner.Scan() {
25- line := scanner.Text()
26- if strings.Contains(line, "docker") || strings.Contains(line, "kubepods") {
27- return true, nil
28- }
29- }
30- if err := scanner.Err(); err != nil {
31- return false, err
32- }
33-
34- return false, nil
35-}
36-
37-func IsRunningInContainer() (bool, error) {
12+func IsRunningInContainer() bool {
3813 // Check for .dockerenv file
39- if fileExists("/.iscontainer") {
40- return true, nil
41- }
42-
43- // Check cgroup for docker or kube pod paths
44- inContainer, err := checkCgroup()
45- if err != nil {
46- return false, err
47- }
48-
49- return inContainer, nil
14+ return fileExists("/.iscontainer") || fileExists("/.dockerenv")
5015 }
@@ -1,9 +1,7 @@
1 package docker1 package docker
2 2
3 import (3 import (
4- "bufio"
5 "os"4 "os"
6- "strings"
7 )5 )
8 6
9 func fileExists(filepath string) bool {7 func fileExists(filepath string) bool {
@@ -11,40 +9,7 @@ func fileExists(filepath string) bool {
11 return !os.IsNotExist(err)9 return !os.IsNotExist(err)
12 }10 }
13 11
14-func checkCgroup() (bool, error) {12+func IsRunningInContainer() bool {
15- // Open the cgroup file for the init process
16- file, err := os.Open("/proc/1/cgroup")
17- if err != nil {
18- return false, err
19- }
20- defer file.Close()
21-
22- // Scan through the cgroup file looking for docker or kube pod paths
23- scanner := bufio.NewScanner(file)
24- for scanner.Scan() {
25- line := scanner.Text()
26- if strings.Contains(line, "docker") || strings.Contains(line, "kubepods") {
27- return true, nil
28- }
29- }
30- if err := scanner.Err(); err != nil {
31- return false, err
32- }
33-
34- return false, nil
35-}
36-
37-func IsRunningInContainer() (bool, error) {
38 // Check for .dockerenv file13 // Check for .dockerenv file
39- if fileExists("/.iscontainer") {14+ return fileExists("/.iscontainer") || fileExists("/.dockerenv")
40- return true, nil
41- }
42-
43- // Check cgroup for docker or kube pod paths
44- inContainer, err := checkCgroup()
45- if err != nil {
46- return false, err
47- }
48-
49- return inContainer, nil
50 }15 }

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close