From 6e0f4e20adfb6b2c6a33a6d0bc7e8d576b1dcf0e Mon Sep 17 00:00:00 2001 From: Luca Scalzotto Date: Sat, 20 May 2023 15:58:47 +0200 Subject: [PATCH] Make versions section load everything concurrently (#6) --- cmd/gokart.go | 13 +------------ internal/versions/all.go | 37 ++++++++++++++++++++++++++++++++++++ internal/versions/docker.go | 29 +++++++++++++--------------- internal/versions/golang.go | 24 ++++++++++------------- internal/versions/java.go | 30 +++++++++++++---------------- internal/versions/node.go | 25 +++++++++++------------- internal/versions/php.go | 24 ++++++++++------------- internal/versions/ruby.go | 26 +++++++++++-------------- internal/versions/rust.go | 24 ++++++++++------------- internal/versions/section.go | 26 +++++++++++++++++++++++++ 10 files changed, 142 insertions(+), 116 deletions(-) create mode 100644 internal/versions/all.go create mode 100644 internal/versions/section.go diff --git a/cmd/gokart.go b/cmd/gokart.go index 185f64f..ff50ee5 100644 --- a/cmd/gokart.go +++ b/cmd/gokart.go @@ -30,20 +30,9 @@ func ps1() { fmt.Print("\n") fmt.Print(ansi.Bold()) - // Path and project info fmt.Print(internal.Path()) fmt.Print(git.Git()) - - // Languages - fmt.Print(versions.GoVersion()) - fmt.Print(versions.JavaVersion()) - fmt.Print(versions.NodeVersion()) - fmt.Print(versions.PhpVersion()) - fmt.Print(versions.RubyVersion()) - fmt.Print(versions.RustVersion()) - - // Tools - fmt.Print(versions.DockerVersion()) + fmt.Print(versions.All()) fmt.Print(ansi.Reset()) } diff --git a/internal/versions/all.go b/internal/versions/all.go new file mode 100644 index 0000000..47737b2 --- /dev/null +++ b/internal/versions/all.go @@ -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, " ") +} diff --git a/internal/versions/docker.go b/internal/versions/docker.go index c9af53b..b4d9731 100644 --- a/internal/versions/docker.go +++ b/internal/versions/docker.go @@ -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 + }, } diff --git a/internal/versions/golang.go b/internal/versions/golang.go index c4f7253..9703f29 100644 --- a/internal/versions/golang.go +++ b/internal/versions/golang.go @@ -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:] + }, } diff --git a/internal/versions/java.go b/internal/versions/java.go index 7f1200c..45e10fa 100644 --- a/internal/versions/java.go +++ b/internal/versions/java.go @@ -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] + }, } diff --git a/internal/versions/node.go b/internal/versions/node.go index fb5d09d..8b9a30a 100644 --- a/internal/versions/node.go +++ b/internal/versions/node.go @@ -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 + }, } diff --git a/internal/versions/php.go b/internal/versions/php.go index 722aa8b..b20aa1e 100644 --- a/internal/versions/php.go +++ b/internal/versions/php.go @@ -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] + }, } diff --git a/internal/versions/ruby.go b/internal/versions/ruby.go index 9d945d3..8d5dfc2 100644 --- a/internal/versions/ruby.go +++ b/internal/versions/ruby.go @@ -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] + }, } diff --git a/internal/versions/rust.go b/internal/versions/rust.go index 63fd177..de93ae5 100644 --- a/internal/versions/rust.go +++ b/internal/versions/rust.go @@ -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] + }, } diff --git a/internal/versions/section.go b/internal/versions/section.go new file mode 100644 index 0000000..792ec83 --- /dev/null +++ b/internal/versions/section.go @@ -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 "" +}