Skip to content

Commit ac2199a

Browse files
authored
build: add a makefile (#2)
Also, add a version command.
1 parent 3ff313e commit ac2199a

File tree

10 files changed

+129
-24
lines changed

10 files changed

+129
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

Makefile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
SHELL = bash
2+
# Current git branch
3+
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
4+
# Current git commit hash
5+
GIT_COMMIT_HASH := $(shell git show --no-patch --no-notes --pretty='%h' HEAD)
6+
VERSION := $(BRANCH).$(GIT_COMMIT_HASH)
7+
ifeq ($(shell git status --porcelain),)
8+
DIRTY :=
9+
else
10+
DIRTY := "dev"
11+
endif
12+
LDFLAGS=--ldflags "-s -X github.com/SwissDataScienceCenter/renku-dev-utils/pkg/version.Version=$(VERSION) -X github.com/SwissDataScienceCenter/renku-dev-utils/pkg/version.VersionSuffix=$(DIRTY)"
13+
14+
.PHONY: all
15+
all: help
16+
17+
.PHONY: vars
18+
vars: ## Show the Makefile vars
19+
@echo SHELL="'$(SHELL)'"
20+
@echo BRANCH="'$(BRANCH)'"
21+
@echo GIT_COMMIT_HASH="'$(GIT_COMMIT_HASH)'"
22+
@echo VERSION="'$(VERSION)'"
23+
@echo DIRTY="'$(DIRTY)'"
24+
25+
.PHONY: rdu
26+
rdu: build/renku-dev-utils ## Build and install renku-dev-utils
27+
mkdir -p `go env GOPATH`/bin/
28+
cp -av build/renku-dev-utils`go env GOEXE` `go env GOPATH`/bin/rdu`go env GOEXE`.new
29+
mv -v `go env GOPATH`/bin/rdu`go env GOEXE`.new `go env GOPATH`/bin/rdu`go env GOEXE`
30+
31+
.PHONY: build/renku-dev-utils
32+
build/renku-dev-utils:
33+
go build -v -o build/ $(LDFLAGS)
34+
35+
# From the operator sdk Makefile
36+
# The help target prints out all targets with their descriptions organized
37+
# beneath their categories. The categories are represented by '##@' and the
38+
# target descriptions by '##'. The awk command is responsible for reading the
39+
# entire set of makefiles included in this invocation, looking for lines of the
40+
# file as xyz: ## something, and then pretty-format the target and help. Then,
41+
# if there's a line with ##@ something, that gets pretty-printed as a category.
42+
# More info on the usage of ANSI control characters for terminal formatting:
43+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
44+
# More info on the awk command:
45+
# http://linuxcommand.org/lc3_adv_awk.php
46+
.PHONY: help
47+
help: ## Display this help.
48+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-25s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
49+
50+
##@ Development
51+
52+
.PHONY: format
53+
format: ## Format source files
54+
gofmt -l -w .

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
# Renku Dev Utils
22

33
Miscellanous utilities for the @SwissDataScienceCenter/renku team.
4+
5+
## Installation
6+
7+
Installing `renku-dev-utils` requires go version `>= v1.24.1` and `make`.
8+
9+
```bash
10+
make rdu
11+
```
12+
13+
14+
## Usage
15+
16+
```bash
17+
rdu
18+
```

build/.gitkeep

Whitespace-only changes.

cmd/renku-dev-utils/main.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

pkg/cmd/copykeycloakadminpassword.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"os"
67

78
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/k8s"
89
"github.com/spf13/cobra"
@@ -20,36 +21,38 @@ var copyKeycloakAdminPasswordCmd = &cobra.Command{
2021
Use: "copy-keycloak-admin-password",
2122
Aliases: []string{"ckap"},
2223
Short: "Copy the Keycloak admin password to the clipboard",
23-
RunE: runCopyKeycloakAdminPassword,
24+
Run: runCopyKeycloakAdminPassword,
2425
}
2526

26-
func runCopyKeycloakAdminPassword(cmd *cobra.Command, args []string) error {
27+
func runCopyKeycloakAdminPassword(cmd *cobra.Command, args []string) {
2728
ctx := context.Background()
2829

2930
clients, err := k8s.GetClientset()
3031
if err != nil {
31-
return err
32+
fmt.Println(err)
33+
os.Exit(1)
3234
}
3335

3436
secret, err := clients.CoreV1().Secrets(namespace).Get(ctx, secretName, v1.GetOptions{})
3537
if err != nil {
36-
return err
38+
fmt.Println(err)
39+
os.Exit(1)
3740
}
3841

3942
secretValue, found := secret.Data[secretKey]
4043
if !found {
41-
return fmt.Errorf("The secret did not contain '%s'", secretKey)
44+
fmt.Printf("The secret did not contain '%s'\n", secretKey)
45+
os.Exit(1)
4246
}
4347

4448
if err := clipboard.Init(); err != nil {
45-
return err
49+
fmt.Println(err)
50+
os.Exit(1)
4651
}
4752

4853
clipboard.Write(clipboard.FmtText, secretValue)
4954
fmt.Printf("Copied Keycloak admin password into the clipboard")
5055
fmt.Println()
51-
52-
return nil
5356
}
5457

5558
func init() {

pkg/cmd/root.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"os"
5+
46
"github.com/spf13/cobra"
57
)
68

@@ -20,4 +22,12 @@ func runRoot(cmd *cobra.Command, args []string) error {
2022

2123
func init() {
2224
rootCmd.AddCommand(copyKeycloakAdminPasswordCmd)
25+
rootCmd.AddCommand(versionCmd)
26+
}
27+
28+
func Main() {
29+
if err := Execute(); err != nil {
30+
os.Exit(2)
31+
}
32+
os.Exit(0)
2333
}

pkg/cmd/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/version"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var versionCmd = &cobra.Command{
11+
Use: "version",
12+
Short: "Print the version of renku-dev-utils",
13+
Run: runVersion,
14+
}
15+
16+
func runVersion(cmd *cobra.Command, args []string) {
17+
fmt.Printf("renku-dev-utils %s\n", version.Version)
18+
}

pkg/version/version.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package version
2+
3+
// Version of renku-dev-utils
4+
var Version string = "DEV"
5+
6+
// Version suffix of renku-dev-utils
7+
var VersionSuffix string = ""
8+
9+
func init() {
10+
if VersionSuffix != "" {
11+
Version = Version + "-" + VersionSuffix
12+
}
13+
}

rdu.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/cmd"
4+
5+
func main() {
6+
cmd.Main()
7+
}

0 commit comments

Comments
 (0)