Skip to content

Commit

Permalink
refactor: Go 1.23.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Dec 31, 2024
1 parent d57a7dd commit c164ec9
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
sudo apt-get install -y pandoc
- name: Install Go
run: |
GO_VERSION=1.22.0
GO_VERSION=${{ vars.GO_VERSION }}
wget https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,15 @@ jobs:
echo "const AppVersion = \"$(echo ${GITHUB_REF#refs/tags/} | cut -c 2-)\"" >> ./build.go &&
echo "const BuildArch = runtime.GOARCH" >> ./build.go &&
echo "const BuildOS = runtime.GOOS" >> ./build.go
echo "const BuildCompiler = \"${{ vars.GO_VERSION }}\"" >> ./build.go
- uses: wangyoucao577/go-release-action@v1
with:
retry: 10
overwrite: true
github_token: ${{ secrets.GH_PAT }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "1.22"
goversion: ${{ vars.GO_VERSION }}
md5sum: false
sha256sum: true
project_path: "./"
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log (go-package-manager)

## 0.26.0

- feat: automatic Go compiler detection in `generate project` command
- refactor: using Go compiler `1.23.4`

## 0.25.5

- feat: `publish` command
Expand Down
1 change: 1 addition & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ import "runtime"
const AppVersion = "0.0.0"
const BuildOS = runtime.GOOS
const BuildArch = runtime.GOARCH
const BuildCompiler = "0.0.0"
11 changes: 9 additions & 2 deletions commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,19 @@ You can use any popular module if needed as well if I does want something else.`
}

if len(modulesToInstall) > 0 {
compilerVersion, err := app.GetCurrentCompilerVersion()

goCompiler := "0.0.0"
if err == nil && compilerVersion != nil {
goCompiler = compilerVersion.String()
}

goModContent := fmt.Sprintf(`module %s
go 1.22.0
go %s
require (
`, projectUrl)
`, projectUrl, goCompiler)

for modUrl := range modulesToInstall {
goModContent = goModContent + fmt.Sprintf(`%v%v latest
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mkloubert/go-package-manager

go 1.22.0
go 1.23.4

require (
github.com/alecthomas/chroma v0.10.0
Expand Down
70 changes: 70 additions & 0 deletions types/app_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import (
"os"
"os/exec"
"path"
"runtime"
"strings"

"github.com/goccy/go-yaml"
"github.com/hashicorp/go-version"
"github.com/joho/godotenv"
"github.com/mkloubert/go-package-manager/utils"

Expand Down Expand Up @@ -484,6 +486,74 @@ func (app *AppContext) GetBinFolderPath() (string, error) {
return "", nil
}

// app.GetCurrentCompilerVersion() - tries to detect the current Go compiler
// version that should be used
func (app *AppContext) GetCurrentCompilerVersion() (*version.Version, error) {
app.Debug("Checking for version in 'go.mod' file ...")

// first try detect in `go.mod` of current directory
goModFile := app.GetFullPathOrDefault("go.mod", "")
if goModFile != "" {
doesGoModExist, err := utils.IsFileExisting(goModFile)
if err == nil && doesGoModExist {
goModContent, err := os.ReadFile(goModFile)
if err == nil {
var versionInGoMod *version.Version = nil

// search for line with `go <version>`
for _, line := range strings.Split(string(goModContent), "\n") {
trimmedLine := strings.TrimSpace(line)
if !strings.HasPrefix(trimmedLine, "go ") {
continue
}

maybeVersion := strings.TrimSpace(trimmedLine[3:])
ver, err := version.NewVersion(maybeVersion)
if err == nil {
versionInGoMod = ver
}

// found => stop here, even if failed
break
}

if versionInGoMod != nil {
// take from go.mod
return versionInGoMod, nil
}
}
}
}

// now try via `go version`
app.Debug("Running 'go version' ...")

p := exec.Command("go", "version")
p.Env = os.Environ()
p.Dir = app.Cwd

output, err := p.Output()
if err == nil {
versionOutput := strings.TrimSpace(string(output))
fields := strings.Fields(versionOutput)
if len(fields) > 2 {
ver, err := version.NewVersion(fields[2][2:])
if err == nil {
return ver, nil // from `go version`
}
}
}

app.Debug("Try get version from 'runtime.Version()' ...")
runtimeVersion := runtime.Version()
ver, err := version.NewVersion(runtimeVersion[2:])
if err == nil {
return ver, nil // from `runtime.Version()`
}

return nil, fmt.Errorf("could not detect Go compiler version")
}

// app.GetCurrentGitBranch() - returns the name of the current branch using git command
func (app *AppContext) GetCurrentGitBranch() (string, error) {
p := exec.Command("git", "symbolic-ref", "--short", "HEAD")
Expand Down

0 comments on commit c164ec9

Please sign in to comment.