Skip to content

Commit

Permalink
allow go install knative.dev/func@{version} (#2220)
Browse files Browse the repository at this point in the history
  • Loading branch information
dprotaso authored Mar 13, 2024
1 parent 9bfefd5 commit f01c709
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 70 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ VTAG := $(shell [ -z $(VTAG) ] && echo $(ETAG) || echo $(VTAG))
VERS ?= $(shell git describe --tags --match 'v*')
KVER ?= $(shell git describe --tags --match 'knative-*')

LDFLAGS := -X main.date=$(DATE) -X main.vers=$(VERS) -X main.kver=$(KVER) -X main.hash=$(HASH)
LDFLAGS := -X knative.dev/func/pkg/app.vers=$(VERS) -X knative.dev/func/pkg/app.kver=$(KVER) -X knative.dev/func/pkg/app.hash=$(HASH)
ifneq ($(FUNC_REPO_REF),)
LDFLAGS += -X knative.dev/func/pkg/pipelines/tekton.FuncRepoRef=$(FUNC_REPO_REF)
endif
Expand Down Expand Up @@ -202,7 +202,7 @@ test-integration: ## Run integration tests using an available cluster.
.PHONY: func-instrumented

func-instrumented: ## Func binary that is instrumented for e2e tests
env CGO_ENABLED=1 go build -ldflags "$(LDFLAGS)" -cover -o func ./cmd/func
env CGO_ENABLED=1 go build -ldflags "$(LDFLAGS)" -cover -o func ./cmd/$(BIN)

test-e2e: func-instrumented ## Run end-to-end tests using an available cluster.
./test/e2e_extended_tests.sh
Expand Down
70 changes: 2 additions & 68 deletions cmd/func/main.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"

"github.com/AlecAivazis/survey/v2/terminal"
"knative.dev/func/cmd"
"knative.dev/func/pkg/docker"
)

// Statically-populated build metadata set by `make build`.
var vers, kver, hash string
import "knative.dev/func/pkg/app"

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
<-sigs // second sigint/sigterm is treated as sigkill
os.Exit(137)
}()

cfg := cmd.RootCommandConfig{
Name: "func",
Version: cmd.Version{
Vers: vers,
Kver: kver,
Hash: hash,
}}

if err := cmd.NewRootCmd(cfg).ExecuteContext(ctx); err != nil {
if !errors.Is(err, terminal.InterruptErr) {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
if ctx.Err() != nil || errors.Is(err, terminal.InterruptErr) {
os.Exit(130)
}

if errors.Is(err, docker.ErrNoDocker) {
if !dockerOrPodmanInstalled() {
fmt.Fprintln(os.Stderr, `Docker/Podman not installed.
Please consider installing one of these:
https://podman-desktop.io/
https://www.docker.com/products/docker-desktop/`)
} else {
fmt.Fprintln(os.Stderr, `Possible causes:
The docker/podman daemon is not running.
The DOCKER_HOST environment variable is not set.`)
}
}

os.Exit(1)
}
}

func dockerOrPodmanInstalled() bool {
_, err := exec.LookPath("podman")
if err == nil {
return true
}
_, err = exec.LookPath("docker")
return err == nil
app.Main()
}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "knative.dev/func/pkg/app"

func main() {
app.Main()
}
72 changes: 72 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"

"github.com/AlecAivazis/survey/v2/terminal"
"knative.dev/func/cmd"
"knative.dev/func/pkg/docker"
)

var vers, kver, hash string

func Main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
cancel()
<-sigs // second sigint/sigterm is treated as sigkill
os.Exit(137)
}()

cfg := cmd.RootCommandConfig{
Name: "func",
Version: cmd.Version{
Vers: vers,
Kver: kver,
Hash: hash,
}}

if err := cmd.NewRootCmd(cfg).ExecuteContext(ctx); err != nil {
if !errors.Is(err, terminal.InterruptErr) {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
}
if ctx.Err() != nil || errors.Is(err, terminal.InterruptErr) {
os.Exit(130)
}

if errors.Is(err, docker.ErrNoDocker) {
if !dockerOrPodmanInstalled() {
fmt.Fprintln(os.Stderr, `Docker/Podman not installed.
Please consider installing one of these:
https://podman-desktop.io/
https://www.docker.com/products/docker-desktop/`)
} else {
fmt.Fprintln(os.Stderr, `Possible causes:
The docker/podman daemon is not running.
The DOCKER_HOST environment variable is not set.`)
}
}

os.Exit(1)
}
}

func dockerOrPodmanInstalled() bool {
_, err := exec.LookPath("podman")
if err == nil {
return true
}
_, err = exec.LookPath("docker")
return err == nil
}

0 comments on commit f01c709

Please sign in to comment.