From 894aec3f5cb85ffe8eda6b38b15afb3d4ea7a475 Mon Sep 17 00:00:00 2001 From: Benedikt Iltisberger Date: Mon, 16 Oct 2023 23:27:31 +0200 Subject: [PATCH] fix: multiple fixes to improve windows support. --- README.md | 7 +++++++ cmd/upgrade.go | 9 ++++++++- go.sum | 2 -- kubernetes/addResources.go | 17 ++++++++++++++++- structs/command.go | 17 +++++++++++++++-- utils/utils.go | 38 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 82 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 173e273..43048ed 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,13 @@ go install github.com/swaggo/swag/cmd/swag@latest swag init --parseDependency --parseInternal ``` +## FAQ +How to set a default editor for Windows (for example Visual Studio Code): +``` +Open powershell as admin and run: +[System.Environment]::SetEnvironmentVariable("EDITOR", "code -w", [System.EnvironmentVariableTarget]::Machine) +``` + ## Contribution Punq is in its nascent stages, brimming with potential, and we're excited to extend an invitation for you to be part of this journey. Your insights, expertise, and contributions can significantly shape its evolution, enhancing this tool for many users and diverse needs. Here's how you can get involved: diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 220204e..1a9b454 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -20,7 +20,14 @@ var updateOperatorImageCmd = &cobra.Command{ Short: "Upgrade the container image of the punq operator.", Long: `Upgrade the container image of the punq operator within your currently selected kubernetes context.`, Run: func(cmd *cobra.Command, args []string) { - vers, err := utils.CurrentReleaseVersion() + var vers string + var err error + if version.Branch == "main" { + vers, err = utils.CurrentReleaseVersion() + } else { + vers, err = utils.CurrentPreReleaseVersion() + } + if err != nil { utils.PrintError(err.Error()) return diff --git a/go.sum b/go.sum index 1e34334..406d279 100644 --- a/go.sum +++ b/go.sum @@ -134,8 +134,6 @@ github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzk github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY= github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo= github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag= -github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw= -github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs= github.com/jedib0t/go-pretty/v6 v6.4.8 h1:HiNzyMSEpsBaduKhmK+CwcpulEeBrTmxutz4oX/oWkg= github.com/jedib0t/go-pretty/v6 v6.4.8/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= diff --git a/kubernetes/addResources.go b/kubernetes/addResources.go index caa4682..f0d59fd 100644 --- a/kubernetes/addResources.go +++ b/kubernetes/addResources.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "os/exec" + "runtime" "github.com/mogenius/punq/dtos" "github.com/mogenius/punq/version" @@ -122,13 +123,27 @@ func addTraefikMiddleware(provider *KubeProvider, ingressHostname string) { fmt.Printf("Creating TRAEFIK middleware (%s) ...\n", ingressHostname) mwYaml := utils.InitPunqIngressTraefikMiddlewareYaml() - cmd := exec.Command("bash", "-c", fmt.Sprintf("echo \"%s\" | kubectl %s apply -f -", mwYaml, ContextFlag(nil))) + if err := os.WriteFile("traefik-middleware.yaml", []byte(mwYaml), 0600); err != nil { + utils.FatalError(err.Error()) + } + + var cmd *exec.Cmd + if runtime.GOOS == "windows" { + cmd = exec.Command("cmd", "/C", fmt.Sprintf("kubectl %s apply -f traefik-middleware.yaml", ContextFlag(nil))) + } else { + cmd = exec.Command("bash", "-c", fmt.Sprintf("kubectl %s apply -f traefik-middleware.yaml", ContextFlag(nil))) + } output, err := cmd.CombinedOutput() if err != nil { utils.FatalError(fmt.Sprintf("failed to execute command (%s): %s\n%s", cmd.String(), err.Error(), string(output))) } + + if err := os.Remove("traefik-middleware.yaml"); err != nil { + utils.FatalError(err.Error()) + } + fmt.Printf("Created TRAEFIK middleware (%s). ✅\n", ingressHostname) } diff --git a/structs/command.go b/structs/command.go index 824c139..dddb85a 100644 --- a/structs/command.go +++ b/structs/command.go @@ -2,6 +2,7 @@ package structs import ( "os/exec" + "runtime" "time" "github.com/mogenius/punq/utils" @@ -27,7 +28,12 @@ type Command struct { } func ExecuteBashCommandSilent(title string, shellCmd string) { - _, err := exec.Command("bash", "-c", shellCmd).Output() + var err error + if runtime.GOOS == "windows" { + _, err = exec.Command("cmd", "/C", shellCmd).Output() + } else { + _, err = exec.Command("bash", "-c", shellCmd).Output() + } if exitErr, ok := err.(*exec.ExitError); ok { exitCode := exitErr.ExitCode() errorMsg := string(exitErr.Stderr) @@ -43,7 +49,14 @@ func ExecuteBashCommandSilent(title string, shellCmd string) { } func ExecuteBashCommandWithResponse(title string, shellCmd string) string { - returnStr, err := exec.Command("bash", "-c", shellCmd).Output() + var err error + var returnStr []byte + if runtime.GOOS == "windows" { + returnStr, err = exec.Command("cmd", "/C", shellCmd).Output() + } else { + returnStr, err = exec.Command("bash", "-c", shellCmd).Output() + } + if exitErr, ok := err.(*exec.ExitError); ok { exitCode := exitErr.ExitCode() errorMsg := string(exitErr.Stderr) diff --git a/utils/utils.go b/utils/utils.go index fd75fad..9b15ad3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -37,8 +37,9 @@ type ResponseError struct { } type Release struct { - TagName string `json:"tag_name"` - Published string `json:"published_at"` + TagName string `json:"tag_name"` + Published string `json:"published_at"` + Prerelease bool `json:"prerelease"` } func IsProduction() bool { @@ -113,6 +114,39 @@ func CurrentReleaseVersion() (string, error) { return release.TagName, nil } +func CurrentPreReleaseVersion() (string, error) { + resp, err := http.Get("https://api.github.com/repos/mogenius/punq/releases") + if err != nil { + return "", err + } + defer resp.Body.Close() + + // Check the status code, handle it accordingly + if resp.StatusCode != 200 { + return "", fmt.Errorf("failed to fetch with status code: %d", resp.StatusCode) + } + + // Read and parse the JSON response + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var releases []Release + if err := json.Unmarshal(body, &releases); err != nil { + return "", err + } + + // Find the latest pre-release + for _, release := range releases { + if release.Prerelease { + return release.TagName, nil // Return the latest pre-release + } + } + + return "", nil +} + func CreateError(err error) ResponseError { return ResponseError{ Error: err.Error(),