Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: escape support #3

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import (
"context"
"os"

"github.com/rs/zerolog"

logger2 "github.com/ft-t/browser-switcher/pkg/logger"
)
Expand All @@ -10,6 +13,8 @@ func main() {
logger := logger2.GetLogger()
ctx := logger.WithContext(context.Background())

zerolog.Ctx(ctx).Debug().Msgf("starting proxy with arguments: %v", os.Args)

if err := run(ctx); err != nil {
logger.Panic().Err(err).Msg("failed to run")
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/proxy/win.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/rs/zerolog"
"golang.org/x/sys/windows/registry"

"github.com/ft-t/browser-switcher/pkg/escaper"
)

func findAppRegistrationPath(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -39,11 +41,18 @@ func run(ctx context.Context) error {
return err
}

var escapedArgs []string
for _, arg := range os.Args[1:] {
escapedArgs = append(escapedArgs, escaper.Escape(arg))
}

args := slices.Concat([]string{
"/C",
"start",
appPath,
}, os.Args[1:])
}, escapedArgs)

zerolog.Ctx(ctx).Debug().Msgf("running command: cmd.exe %v", escapedArgs)

cmd := exec.Command(
"cmd.exe",
Expand Down
6 changes: 5 additions & 1 deletion cmd/switcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/natefinch/lumberjack.v2"

config2 "github.com/ft-t/browser-switcher/pkg/config"
"github.com/ft-t/browser-switcher/pkg/escaper"
"github.com/ft-t/browser-switcher/pkg/launcher"
"github.com/ft-t/browser-switcher/pkg/selector"
"github.com/ft-t/browser-switcher/pkg/ui"
Expand Down Expand Up @@ -37,8 +38,11 @@ func main() {
}

targetURL := os.Args[1]
targetURL = escaper.Unescape(targetURL)

ctx = lg.With().Str("targetURL", targetURL).Logger().WithContext(ctx)
ctx = lg.With().Str("rawTargetURL", os.Args[1]).
Str("targetURL", targetURL).
Logger().WithContext(ctx)

browserConfig, err := config2.ReadConfig(ctx)

Expand Down
20 changes: 20 additions & 0 deletions pkg/escaper/escape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package escaper

import (
"fmt"
"strings"
)

var charsToEscape = []string{
"&",
}

func Escape(
input string,
) string {
for _, char := range charsToEscape {
input = strings.ReplaceAll(input, char, fmt.Sprintf(`"%s"`, char))
}

return input
}
16 changes: 16 additions & 0 deletions pkg/escaper/unescape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package escaper

import (
"fmt"
"strings"
)

func Unescape(
input string,
) string {
for _, char := range charsToEscape {
input = strings.ReplaceAll(input, fmt.Sprintf(`"%s"`, char), char)
}

return input
}
Loading