-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make versions section load everything concurrently (#6)
- Loading branch information
1 parent
e9018ed
commit 6e0f4e2
Showing
10 changed files
with
142 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package versions | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
) | ||
|
||
var sections = []section{ | ||
// Languages | ||
Go, | ||
Java, | ||
Node, | ||
Php, | ||
Ruby, | ||
Rust, | ||
// Tools | ||
Docker, | ||
} | ||
|
||
func All() string { | ||
var wg sync.WaitGroup | ||
result := make([]string, len(sections)) | ||
|
||
for i, s := range sections { | ||
wg.Add(1) | ||
|
||
i := i | ||
s := s | ||
go func() { | ||
result[i] = s.version() | ||
wg.Done() | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
return strings.Join(result, " ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
"strings" | ||
) | ||
|
||
var dockerFiles = []string{ | ||
"Dockerfile", | ||
"docker-compose.yml", | ||
"docker-compose.yaml", | ||
} | ||
var Docker = section{ | ||
symbol: "🐳", | ||
color: ansi.Blue, | ||
|
||
func DockerVersion() string { | ||
if _, ok := internal.UpsearchWd(dockerFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"Dockerfile", | ||
"docker-compose.yml", | ||
"docker-compose.yaml", | ||
}, | ||
|
||
if version, ok := internal.Command("docker", "-v"); ok { | ||
command: []string{"docker", "-v"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
Docker version 23.0.1, build a5ee5b1dfc | ||
*/ | ||
version, _, _ = strings.Cut(version[15:], ",") | ||
return ansi.Color(ansi.Blue, " 🐳 v"+version) | ||
} | ||
|
||
return "" | ||
version, _, _ := strings.Cut(output[15:], ",") | ||
return "v" + version | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
"strings" | ||
) | ||
|
||
var goFiles = []string{ | ||
"go.mod", | ||
} | ||
var Go = section{ | ||
symbol: "🐹", | ||
color: ansi.Cyan, | ||
|
||
func GoVersion() string { | ||
if _, ok := internal.UpsearchWd(goFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"go.mod", | ||
}, | ||
|
||
if version, ok := internal.Command("go", "version"); ok { | ||
command: []string{"go", "version"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
go version go1.20.2 linux/amd64 | ||
*/ | ||
version := strings.SplitN(version, " ", 4)[2][2:] | ||
return ansi.Color(ansi.Cyan, " 🐹 v"+version) | ||
} | ||
|
||
return "" | ||
return "v" + strings.SplitN(output, " ", 4)[2][2:] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
"strings" | ||
) | ||
|
||
var javaFiles = []string{ | ||
"pom.xml", | ||
"build.gradle", | ||
"settings.gradle", | ||
"build.xml", | ||
} | ||
var Java = section{ | ||
symbol: "☕", | ||
color: ansi.Cyan, | ||
|
||
func JavaVersion() string { | ||
if _, ok := internal.UpsearchWd(javaFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"pom.xml", | ||
"build.gradle", | ||
"settings.gradle", | ||
"build.xml", | ||
}, | ||
|
||
if version, ok := internal.Command("java", "--version"); ok { | ||
command: []string{"java", "--version"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
openjdk 19.0.2 2023-01-17 | ||
OpenJDK Runtime Environment (build 19.0.2+7) | ||
OpenJDK 64-Bit Server VM (build 19.0.2+7, mixed mode) | ||
*/ | ||
version := strings.SplitN(version, " ", 3)[1] | ||
return ansi.Color(ansi.Cyan, " ☕ v"+version) | ||
} | ||
|
||
return "" | ||
return "v" + strings.SplitN(output, " ", 3)[1] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
) | ||
|
||
var nodeFiles = []string{ | ||
"package.json", | ||
"node_modules", | ||
} | ||
var Node = section{ | ||
symbol: "⬢", | ||
color: ansi.Green, | ||
|
||
func NodeVersion() string { | ||
if _, ok := internal.UpsearchWd(nodeFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"package.json", | ||
"node_modules", | ||
}, | ||
|
||
if version, ok := internal.Command("node", "-v"); ok { | ||
command: []string{"node", "-v"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
v19.1.0 | ||
*/ | ||
return ansi.Color(ansi.Green, " ⬢ "+version) | ||
} | ||
|
||
return "" | ||
return output | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,26 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
"strings" | ||
) | ||
|
||
var phpFiles = []string{ | ||
"composer.json", | ||
} | ||
var Php = section{ | ||
symbol: "🐘", | ||
color: ansi.Magenta, | ||
|
||
func PhpVersion() string { | ||
if _, ok := internal.UpsearchWd(phpFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"composer.json", | ||
}, | ||
|
||
if version, ok := internal.Command("php", "-v"); ok { | ||
command: []string{"php", "-v"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
PHP 8.2.4 (cli) (built: Mar 15 2023 15:27:52) (NTS) | ||
Copyright (c) The PHP Group | ||
Zend Engine v4.2.4, Copyright (c) Zend Technologies | ||
*/ | ||
version := strings.SplitN(version, " ", 3)[1] | ||
return ansi.Color(ansi.Magenta, " 🐘 v"+version) | ||
} | ||
|
||
return "" | ||
return "v" + strings.SplitN(output, " ", 3)[1] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
"strings" | ||
) | ||
|
||
var rubyFiles = []string{ | ||
"Gemfile", | ||
"Rakefile", | ||
} | ||
var Ruby = section{ | ||
symbol: "💎", | ||
color: ansi.Red, | ||
|
||
func RubyVersion() string { | ||
if _, ok := internal.UpsearchWd(rubyFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"Gemfile", | ||
"Rakefile", | ||
}, | ||
|
||
if version, ok := internal.Command("ruby", "-v"); ok { | ||
command: []string{"ruby", "-v"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [x86_64-linux] | ||
*/ | ||
version = strings.SplitN(version, " ", 3)[1] | ||
return ansi.Color(ansi.Red, " 💎 v"+version) | ||
} | ||
|
||
return "" | ||
return "v" + strings.SplitN(output, " ", 3)[1] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
"strings" | ||
) | ||
|
||
var rustFiles = []string{ | ||
"Cargo.toml", | ||
} | ||
var Rust = section{ | ||
symbol: "🦀", | ||
color: ansi.Red, | ||
|
||
func RustVersion() string { | ||
if _, ok := internal.UpsearchWd(rustFiles); !ok { | ||
return "" | ||
} | ||
upsearchFiles: []string{ | ||
"Cargo.toml", | ||
}, | ||
|
||
if version, ok := internal.Command("rustc", "--version"); ok { | ||
command: []string{"rustc", "--version"}, | ||
versionFunc: func(output string) string { | ||
/* | ||
Example: | ||
rustc 1.69.0 (84c898d65 2023-04-16) (Arch Linux rust 1:1.69.0-2) | ||
*/ | ||
version = strings.SplitN(version, " ", 3)[1] | ||
return ansi.Color(ansi.Red, " 🦀 v"+version) | ||
} | ||
|
||
return "" | ||
return "v" + strings.SplitN(output, " ", 3)[1] | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package versions | ||
|
||
import ( | ||
"gokart-prompt/internal" | ||
"gokart-prompt/internal/ansi" | ||
) | ||
|
||
type section struct { | ||
symbol string | ||
color ansi.ColorCode | ||
upsearchFiles []string | ||
command []string | ||
versionFunc func(output string) string | ||
} | ||
|
||
func (s section) version() string { | ||
if _, ok := internal.UpsearchWd(s.upsearchFiles); !ok { | ||
return "" | ||
} | ||
|
||
if output, ok := internal.Command(s.command[0], s.command[1:]...); ok { | ||
return ansi.Color(s.color, " "+s.symbol+" "+s.versionFunc(output)) | ||
} | ||
|
||
return "" | ||
} |