Skip to content

Commit

Permalink
fix: multiple fixes to improve windows support.
Browse files Browse the repository at this point in the history
  • Loading branch information
beneiltis committed Oct 16, 2023
1 parent f2defbf commit 894aec3
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
17 changes: 16 additions & 1 deletion kubernetes/addResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/exec"
"runtime"

"github.com/mogenius/punq/dtos"
"github.com/mogenius/punq/version"
Expand Down Expand Up @@ -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)
}

Expand Down
17 changes: 15 additions & 2 deletions structs/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package structs

import (
"os/exec"
"runtime"
"time"

"github.com/mogenius/punq/utils"
Expand All @@ -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)
Expand All @@ -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)
Expand Down
38 changes: 36 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 894aec3

Please sign in to comment.