Skip to content

Commit

Permalink
Merge pull request #1 from chad-bekmezian-snap/improve-error-message
Browse files Browse the repository at this point in the history
Improve error message
  • Loading branch information
chad-bekmezian-snap authored Mar 23, 2023
2 parents 09ce188 + bf57f09 commit 38235aa
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 49 deletions.
9 changes: 9 additions & 0 deletions .idea/k8s-compose-forward.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package-windows-amd64:
mkdir composeForward_windows_x86_64
cp -r ./bin/* composeForward_windows_x86_64
env GOARCH=amd64 GOOS=windows go build -o ./composeForward_windows_x86_64/composeForward ./cmd/compose-forward/
env GOARCH=amd64 GOOS=windows go build -trimpath -o ./composeForward_windows_x86_64/composeForward ./cmd/compose-forward/
tar --exclude="*.DS_Store" -czf composeForward_windows_x86_64.tgz composeForward_windows_x86_64/
rm -r composeForward_windows_x86_64
.PHONY: package-windows-amd64

package-windows-arm64:
mkdir composeForward_windows_arm64
cp -r ./bin/* composeForward_windows_arm64
env GOARCH=arm64 GOOS=windows go build -o ./composeForward_windows_arm64/composeForward ./cmd/compose-forward/
env GOARCH=arm64 GOOS=windows go build -trimpath -o ./composeForward_windows_arm64/composeForward ./cmd/compose-forward/
tar --exclude="*.DS_Store" -czf composeForward_windows_arm64.tgz composeForward_windows_arm64/
rm -r composeForward_windows_arm64
.PHONY: package-windows-amd64

package-darwin-amd64:
mkdir composeForward_darwin_amd64
cp -r ./bin/* composeForward_darwin_amd64
env GOARCH=amd64 GOOS=darwin go build -o ./composeForward_darwin_amd64/composeForward ./cmd/compose-forward/
env GOARCH=amd64 GOOS=darwin go build -trimpath -o ./composeForward_darwin_amd64/composeForward ./cmd/compose-forward/
tar --exclude="*.DS_Store" -czf composeForward_darwin_amd64.tgz composeForward_darwin_amd64/
rm -r composeForward_darwin_amd64
.PHONY: package-windows-amd64

package-darwin-arm64:
mkdir composeForward_darwin_arm64
cp -r ./bin/* composeForward_darwin_arm64
env GOARCH=arm64 GOOS=darwin go build -o ./composeForward_darwin_arm64/composeForward ./cmd/compose-forward/
env GOARCH=arm64 GOOS=darwin go build -trimpath -o ./composeForward_darwin_arm64/composeForward ./cmd/compose-forward/
tar --exclude="*.DS_Store" -czf composeForward_darwin_arm64.tgz composeForward_darwin_arm64/
rm -r composeForward_darwin_arm64
.PHONY: package-windows-arm64
Expand Down
12 changes: 9 additions & 3 deletions cmd/compose-forward/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import (

func main() {
if listServices {
printAppsSilently(yamlPathFlag)
if err := printAppsSilently(yamlPathFlag); err != nil {
fmt.Println(color.Ize(color.Red, err))
}
return
}

if completions {
results, _ := compose.GetBashCompletions(yamlPathFlag)
results, err := compose.GetBashCompletions(yamlPathFlag)
if err != nil {
fmt.Println(color.Ize(color.Red, err))
return
}
fmt.Println(strings.Trim(fmt.Sprint(results), "[]"))
return
}
Expand All @@ -29,7 +35,7 @@ func main() {
nameToService, err := compose.Load(yamlPathFlag)
if err != nil {
fmt.Println(color.Ize(color.Red, err))
panic(err)
return
}

var wg sync.WaitGroup
Expand Down
7 changes: 3 additions & 4 deletions cmd/compose-forward/printApps.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package main

import (
"fmt"
"github.com/TwiN/go-color"
"github.com/chad-bekmezian-snap/k8s-compose-forward/service/compose"
"os"
"sort"
"strings"
)

func printAppsSilently(path string) {
func printAppsSilently(path string) error {
_ = os.Setenv("FORWARD_SILENT", "true")
nameToService, err := compose.Load(path)
if err != nil {
fmt.Println(color.Ize(color.Red, err))
panic(err)
return err
}
printValidApplications(nameToService)
return nil
}

func printValidApplications(nameToService map[string]compose.Service) {
Expand Down
7 changes: 7 additions & 0 deletions service/k8s/dependencies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package k8s

import "os/exec"

var (
executeCMD = exec.Command
)
32 changes: 1 addition & 31 deletions service/k8s/listServices.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
)

// ListServices kubectl get service --field-selector metadata.namespace!=default -A -o=json
func ListServices() (ServiceSlice, error) {
cmd := exec.Command("kubectl", "get", "service", "-A", "-o=json", "--field-selector=metadata.namespace!=default")
cmd := executeCMD("kubectl", "get", "service", "-A", "-o=json", "--field-selector=metadata.namespace!=default")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
Expand All @@ -27,31 +25,3 @@ func ListServices() (ServiceSlice, error) {

return result.Services, nil
}

func (s ServiceSlice) FindServiceByClosestMatchingName(v string, namespace ...string) (Service, bool) {
currentMatchIndex := -1

ServiceLoop:
for i, svc := range s {
for _, ns := range namespace {
// Skip this iteration if namespace doesn't match provided
if ns == svc.Detail.Namespace {
break
}
continue ServiceLoop
}

if strings.Contains(svc.Detail.Name, v) {
if currentMatchIndex > -1 && len(svc.Detail.Name) >= len(s[currentMatchIndex].Detail.Name) {
continue
}
currentMatchIndex = i
}
}

if currentMatchIndex == -1 {
return Service{}, false
}

return s[currentMatchIndex], true
}
9 changes: 4 additions & 5 deletions service/k8s/portForward.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/TwiN/go-color"
"os"
"os/exec"
"strings"
"sync"
)
Expand Down Expand Up @@ -33,7 +32,7 @@ func PortForwardToService(s ForwardableService) {
return
}

cmd := exec.Command("kubectl", cmdArgs...)
cmd := executeCMD("kubectl", cmdArgs...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
Expand All @@ -60,9 +59,9 @@ func cmdRunning(name string, args ...string) bool {
}

isDuplicateProcess := func() bool {
psCmd := exec.Command("ps", "aux")
grep := exec.Command("grep", fmt.Sprintf(`%s %s`, strings.TrimSpace(name), strings.Join(args, " ")))
removeGrep := exec.Command("grep", "-v", "grep")
psCmd := executeCMD("ps", "aux")
grep := executeCMD("grep", fmt.Sprintf(`%s %s`, strings.TrimSpace(name), strings.Join(args, " ")))
removeGrep := executeCMD("grep", "-v", "grep")

grepPipe, _ := psCmd.StdoutPipe()
defer grepPipe.Close()
Expand Down
30 changes: 30 additions & 0 deletions service/k8s/service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
package k8s

import "strings"

type ServiceSlice []Service

func (s ServiceSlice) FindServiceByClosestMatchingName(v string, namespace ...string) (Service, bool) {
currentMatchIndex := -1

ServiceLoop:
for i, svc := range s {
for _, ns := range namespace {
// Skip this iteration if namespace doesn't match provided
if ns == svc.Detail.Namespace {
break
}
continue ServiceLoop
}

if strings.Contains(svc.Detail.Name, v) {
if currentMatchIndex > -1 && len(svc.Detail.Name) >= len(s[currentMatchIndex].Detail.Name) {
continue
}
currentMatchIndex = i
}
}

if currentMatchIndex == -1 {
return Service{}, false
}

return s[currentMatchIndex], true
}

type Service struct {
Kind string `json:"kind"`
Detail struct {
Expand Down

0 comments on commit 38235aa

Please sign in to comment.