diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7bb1f4f..3a1f559 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -23,13 +23,10 @@ jobs: with: go-version: '1.25' check-latest: true - - - name: Install libraries for ubuntu - run: | - sudo apt install libx11-dev - name: Print environment run: | + ldconfig -p | grep X11 which go go env make vars @@ -40,9 +37,25 @@ jobs: - name: Check vet run: make check-vet + - name: Run tests + run: make test + - name: Build renku-dev-utils run: make rdu + - name: Install libraries for ubuntu + run: | + sudo apt install libx11-dev + + - name: Print environment with x11 + run: | + which go + go env + make vars + + - name: Run tests with x11 + run: make test + lint: runs-on: ubuntu-24.04 steps: diff --git a/Makefile b/Makefile index d2fa16e..0a39e87 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,18 @@ else VERSION := $(BRANCH).$(GIT_COMMIT_HASH) DIRTY := "dev" endif +# X11 support +X11 := 0 +ifeq ($(shell go env GOOS),linux) +ifneq ($(shell ldconfig -p | grep X11 || echo ""),) +X11 := 1 +endif +endif 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)" +TAGS= +ifeq ($(X11),1) +TAGS=-tags=x11 +endif .PHONY: all all: help @@ -29,6 +40,7 @@ vars: ## Show the Makefile vars @echo GIT_TAG="'$(GIT_TAG)'" @echo VERSION="'$(VERSION)'" @echo DIRTY="'$(DIRTY)'" + @echo X11="'$(X11)'" .PHONY: rdu rdu: build/renku-dev-utils ## Build and install renku-dev-utils @@ -38,7 +50,7 @@ rdu: build/renku-dev-utils ## Build and install renku-dev-utils .PHONY: build/renku-dev-utils build/renku-dev-utils: - go build -v -o build/ $(LDFLAGS) + go build -v -o build/ $(TAGS) $(LDFLAGS) # From the operator sdk Makefile # The help target prints out all targets with their descriptions organized @@ -73,6 +85,10 @@ check-vet: ## Check source files with `go vet` lint: ## Lint source files with `golangci-lint run` golangci-lint run +.PHONY: test +test: ## Run go tests + go test -v $(TAGS) ./... + ##@ Code generation .PHONY: renku-users-apispec diff --git a/pkg/clipboard/clipboard.go b/pkg/clipboard/clipboard.go new file mode 100644 index 0000000..c47f9e8 --- /dev/null +++ b/pkg/clipboard/clipboard.go @@ -0,0 +1,13 @@ +//go:build !linux || x11 + +package clipboard + +import "golang.design/x/clipboard" + +func Init() (supported bool, err error) { + return true, clipboard.Init() +} + +func WriteStr(value string) { + clipboard.Write(clipboard.FmtText, []byte(value)) +} diff --git a/pkg/clipboard/clipboard_test.go b/pkg/clipboard/clipboard_test.go new file mode 100644 index 0000000..1ecbe35 --- /dev/null +++ b/pkg/clipboard/clipboard_test.go @@ -0,0 +1,15 @@ +package clipboard + +import ( + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestClipboardInit(t *testing.T) { + supported, err := Init() + assert.NoError(t, err) + fmt.Fprintf(os.Stderr, "NOTE: clipboard supported: %t\n", supported) +} diff --git a/pkg/clipboard/nox11.go b/pkg/clipboard/nox11.go new file mode 100644 index 0000000..61a79d8 --- /dev/null +++ b/pkg/clipboard/nox11.go @@ -0,0 +1,13 @@ +//go:build linux && !x11 + +package clipboard + +import "fmt" + +func Init() (supported bool, err error) { + return false, nil +} + +func WriteStr(value string) { + fmt.Println(value) +} diff --git a/pkg/cmd/copykeycloakadminpassword.go b/pkg/cmd/copykeycloakadminpassword.go index 0297886..cedb868 100644 --- a/pkg/cmd/copykeycloakadminpassword.go +++ b/pkg/cmd/copykeycloakadminpassword.go @@ -5,12 +5,12 @@ import ( "fmt" "os" + "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/clipboard" "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/github" "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/k8s" ns "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/namespace" "github.com/spf13/cobra" "github.com/spf13/viper" - "golang.design/x/clipboard" ) var copyKeycloakAdminPasswordCmd = &cobra.Command{ @@ -61,14 +61,20 @@ func runCopyKeycloakAdminPassword(cmd *cobra.Command, args []string) { os.Exit(1) } - if err := clipboard.Init(); err != nil { + supported, err := clipboard.Init() + if err != nil { fmt.Println(err) os.Exit(1) } + if !supported { + fmt.Fprintf(os.Stderr, "Warning: clipboard not supported!") + } - clipboard.Write(clipboard.FmtText, secretValue) - fmt.Printf("Copied Keycloak admin password into the clipboard") - fmt.Println() + clipboard.WriteStr(string(secretValue)) + if supported { + fmt.Printf("Copied Keycloak admin password into the clipboard") + fmt.Println() + } } func init() {