Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish the k0s_sort binary #9

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
name: Go

on: [push, pull_request]
on: [pull_request]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
if: github.ref != 'refs/heads/main'
uses: actions/setup-go@v5
with:
go-version: 1.17

- name: Go modules cache
uses: actions/cache@v2
if: github.ref != 'refs/heads/main'
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
go-version-file: go.mod

- name: Run golangci-lint
if: github.ref != 'refs/heads/main'
uses: golangci/[email protected]
with:
version: v1.44.0
skip-go-installation: true
uses: golangci/golangci-lint-action@v3

- name: Build
if: github.ref != 'refs/heads/main'
run: go build -v ./...
run: make k0s_sort

- name: Test
if: github.ref != 'refs/heads/main'
run: go test -v ./...
run: make test

42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Release

on:
push:
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
release:
name: release
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Tag name
id: tag-name
run: echo "tag=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT"

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Build binaries
id: build_bins
env:
TAG_NAME: ${{ steps.tag-name.outputs.tag }}
run: make build-all

- name: Create release and upload binaries
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
bin/k0s_sort-*
bin/checksums.txt
tag_name: ${{ steps.tag-name.outputs.tag }}
name: ${{ steps.tag-name.outputs.tag }}
generate_release_notes: true
prerelease: ${{ contains(steps.tag-name.outputs.tag, '-') }} # v0.1.2-beta1, 1.2.3-rc1

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
k0s_sort
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
GO_SRCS := $(shell find . -type f -name '*.go' -a ! \( -name 'zz_generated*' -o -name '*_test.go' \))
TAG_NAME = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null || echo "dev")
PREFIX = /usr/local
LD_FLAGS = -s -w -X github.com/k0sproject/version/internal/version.Version=$(TAG_NAME)
BUILD_FLAGS = -trimpath -a -tags "netgo,osusergo,static_build" -installsuffix netgo -ldflags "$(LD_FLAGS) -extldflags '-static'"

k0s_sort: $(GO_SRCS)
CGO_ENABLED=0 go build $(BUILD_FLAGS) -o $@ ./cmd/k0s_sort

PLATFORMS := linux-amd64 linux-arm64 linux-arm darwin-amd64 darwin-arm64 windows-amd64
bins := $(foreach platform, $(PLATFORMS), bin/$(BIN_PREFIX)$(platform))
$(bins):
$(eval temp := $(subst -, ,$(subst $(BIN_PREFIX),,$(notdir $@))))
$(eval OS := $(word 1, $(subst -, ,$(temp))))
$(eval ARCH := $(word 2, $(subst -, ,$(temp))))
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(BUILD_FLAGS) -o $@ ./cmd/k0s_sort

bin/sha256sums.txt: $(bins)
sha256sum -b $(bins) | sed 's|bin/||' > $@

build-all: $(bins) bin/sha256sums.md
kke marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: install
install: k0s_sort
install -d $(DESTDIR)$(PREFIX)/bin/
install -m 755 k0s_sort $(DESTDIR)$(PREFIX)/bin/

.PHONY: test
test:
go clean -testcache && go test -count=1 -v ./...

.PHONY: clean
clean:
rm -rf k0s_sort bin

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ func main() {
fmt.Printf("Latest k0s version is: %s\n", latest)
}
```

### `k0s_sort` executable

A command-line interface to the package. Can be used to sort lists of versions or to obtain the latest version number.

```console
Usage: k0s_sort [options] [filename ...]
-l only print the latest version
-o print the latest version from online
-s omit prerelease versions
-v print k0s_sort version
```
86 changes: 79 additions & 7 deletions cmd/k0s_sort/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,106 @@ package main

import (
"bufio"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"

"github.com/k0sproject/version"
toolversion "github.com/k0sproject/version/internal/version"
)

func main() {
stat, err := os.Stdin.Stat()
var (
versionFlag bool
latestFlag bool
onlineFlag bool
stableOnlyFlag bool
)

func online() {
v, err := version.LatestByPrerelease(!stableOnlyFlag)
if err != nil {
panic(fmt.Errorf("can't stat stdin: %w", err))
println("failed to get latest version:", err.Error())
os.Exit(1)
}
fmt.Println(v.String())
}

func main() {
flag.Usage = func() {
exe, _ := os.Executable()
fmt.Fprintf(os.Stderr, "Usage: %s [options] [filename ...]\n", filepath.Base(exe))
flag.PrintDefaults()
}
flag.BoolVar(&versionFlag, "v", false, "print k0s_sort version")
flag.BoolVar(&latestFlag, "l", false, "only print the latest version from input")
flag.BoolVar(&onlineFlag, "o", false, "print the latest version from online")
flag.BoolVar(&stableOnlyFlag, "s", false, "omit prerelease versions")
flag.Parse()

if versionFlag {
fmt.Println(toolversion.Version)
return
}

if onlineFlag {
online()
return
}
if (stat.Mode() & os.ModeCharDevice) != 0 {
panic(fmt.Errorf("can't read stdin"))

var input io.Reader
if flag.NArg() > 0 && flag.Arg(0) != "-" {
var files []io.Reader
for _, fn := range flag.Args() {
file, err := os.Open(fn)
if err != nil {
println("can't open file:", err.Error())
os.Exit(1)
}
defer file.Close()
files = append(files, file)
}
input = io.MultiReader(files...)
} else {
stat, err := os.Stdin.Stat()
if err != nil {
println("can't stat stdin:", err.Error())
os.Exit(1)
}
if (stat.Mode() & os.ModeCharDevice) != 0 {
println("can't read stdin")
os.Exit(1)
}
input = os.Stdin
}
versions := version.Collection{}
scanner := bufio.NewScanner(os.Stdin)
scanner := bufio.NewScanner(input)
for scanner.Scan() {
v, err := version.NewVersion(scanner.Text())
if err != nil {
panic(fmt.Errorf("failed to parse version: %w", err))
println("failed to parse version:", err.Error())
os.Exit(1)
}
if v.Prerelease() != "" && stableOnlyFlag {
continue
}
versions = append(versions, v)
}

sort.Sort(versions)

if len(versions) == 0 {
println("no versions found")
os.Exit(1)
}

if latestFlag {
fmt.Printf("v%s\n", strings.TrimPrefix(versions[len(versions)-1].String(), "v"))
return
}
for _, v := range versions {
fmt.Printf("v%s\n", strings.TrimPrefix(v.String(), "v"))
}
Expand Down
3 changes: 3 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package version

var Version = "dev"
Loading