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

fix: fix windows paths #141

Merged
merged 4 commits into from
May 5, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.sandbox
include/
.sandbox/
.tool-versions
Expand Down
23 changes: 4 additions & 19 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
concurrency: 16
run:
deadline: 10m
timeout: 10m
concurrency: 16
issues-exit-code: 1
tests: true

output:
format: colored-line-number
formats:
- format: colored-line-number

linters-settings:
godox:
Expand All @@ -17,25 +18,12 @@ issues:

linters:
enable:
- scopelint
- govet
- ineffassign
- varcheck
- unused
- deadcode
- bodyclose
- structcheck
- staticcheck
- godox
- rowserrcheck
- prealloc
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- deadcode
- decorder
- dogsled
- dupword
Expand Down Expand Up @@ -86,11 +74,9 @@ linters:
- protogetter
- reassign
- rowserrcheck
- scopelint
- sloglint
- sqlclosecheck
- staticcheck
- structcheck
- tagalign
- tagliatelle
- tenv
Expand All @@ -103,7 +89,6 @@ linters:
- unparam
- unused
- usestdlibvars
- varcheck
- wastedassign
- whitespace
- zerologlint
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ test:
lint:
GOOS=darwin GOARCH=arm64 gomodrun golangci-lint run ./cmd/... ./src/... --verbose
GOOS=linux GOARCH=amd64 gomodrun golangci-lint run ./cmd/... ./src/... --verbose
GOOS=windows GOARCH=amd64 gomodrun golangci-lint run ./cmd/... ./src/... --verbose
GOOS=windows GOARCH=amd64 gomodrun golangci-lint run ./cmd/... ./src/... --verbose

build-for-windows-amd:
GOOS=windows GOARCH=amd64 go build -o bin/zcli.exe cmd/zcli/main.go

build-for-linux-amd:
GOOS=linux GOARCH=amd64 go build -o bin/zcli cmd/zcli/main.go

build-for-darwin-arm:
GOOS=darwin GOARCH=arm64 go build -o bin/zcli cmd/zcli/main.go
10 changes: 5 additions & 5 deletions src/archiveClient/handler_findGitFiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package archiveClient
import (
"bufio"
"bytes"
"context"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/zeropsio/zcli/src/cmdRunner"
)

func (h *Handler) FindGitFiles(workingDir string) (res []File, _ error) {
func (h *Handler) FindGitFiles(ctx context.Context, workingDir string) (res []File, _ error) {
workingDir, err := filepath.Abs(workingDir)
if err != nil {
return nil, err
Expand All @@ -28,8 +28,8 @@ func (h *Handler) FindGitFiles(workingDir string) (res []File, _ error) {
}
}

createCmd := func(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
createCmd := func(name string, arg ...string) *cmdRunner.ExecCmd {
cmd := cmdRunner.CommandContext(ctx, name, arg...)
cmd.Dir = workingDir
return cmd
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (h *Handler) FindGitFiles(workingDir string) (res []File, _ error) {
return res, nil
}

func (h *Handler) listFiles(cmd *exec.Cmd, fn func(path string) error) error {
func (h *Handler) listFiles(cmd *cmdRunner.ExecCmd, fn func(path string) error) error {
output, err := cmdRunner.Run(cmd)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/serviceDeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
"path"
"path/filepath"
"time"

"github.com/zeropsio/zcli/src/archiveClient"
Expand Down Expand Up @@ -93,7 +93,7 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
size = s.Size()
reader = packageFile
} else {
tempFile := path.Join(os.TempDir(), appVersion.Id.Native())
tempFile := filepath.Join(os.TempDir(), appVersion.Id.Native())
f, err := os.Create(tempFile)
if err != nil {
return err
Expand Down Expand Up @@ -178,9 +178,9 @@ func serviceDeployCmd() *cmdBuilder.Cmd {
cmdData.UxBlocks,
[]uxHelpers.Process{{
F: uxHelpers.CheckZeropsProcess(deployProcess.Id, cmdData.RestApiClient),
RunningMessage: i18n.T(i18n.PushRunning),
ErrorMessageMessage: i18n.T(i18n.PushFailed),
SuccessMessage: i18n.T(i18n.PushFinished),
RunningMessage: i18n.T(i18n.DeployRunning),
ErrorMessageMessage: i18n.T(i18n.DeployFailed),
SuccessMessage: i18n.T(i18n.DeployFinished),
}},
)

Expand Down
6 changes: 3 additions & 3 deletions src/cmd/servicePush.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"io"
"os"
"path"
"path/filepath"
"time"

"github.com/zeropsio/zcli/src/archiveClient"
Expand Down Expand Up @@ -92,13 +92,13 @@ func servicePushCmd() *cmdBuilder.Cmd {
size = s.Size()
reader = packageFile
} else {
tempFile := path.Join(os.TempDir(), appVersion.Id.Native())
tempFile := filepath.Join(os.TempDir(), appVersion.Id.Native())
f, err := os.Create(tempFile)
if err != nil {
return err
}
defer os.Remove(tempFile)
files, err := arch.FindGitFiles(cmdData.Params.GetString("workingDir"))
files, err := arch.FindGitFiles(ctx, cmdData.Params.GetString("workingDir"))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/vpnDown.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func disconnectVpn(ctx context.Context, uxBlocks uxBlock.UxBlocks) error {
}
defer f.Close()

c := wg.DownCmd(ctx, filePath)
c := wg.DownCmd(ctx, filePath, constants.WgInterfaceName)
_, err = cmdRunner.Run(c)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/pkg/errors"
"github.com/zeropsio/zcli/src/uxBlock"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

"github.com/zeropsio/zcli/src/cliStorage"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/zeropsio/zcli/src/file"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/nettools"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/uxHelpers"
"github.com/zeropsio/zcli/src/wg"
Expand Down Expand Up @@ -96,6 +96,7 @@ func vpnUpCmd() *cmdBuilder.Cmd {
if err != nil {
return err
}
defer f.Close()

err = wg.GenerateConfig(f, privateKey, vpnSettings)
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions src/cmdRunner/execCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmdRunner

import (
"context"
"os/exec"
)

type Func func(ctx context.Context) error

func CommandContext(ctx context.Context, cmd string, args ...string) *ExecCmd {
return &ExecCmd{
Cmd: exec.CommandContext(ctx, cmd, args...),
ctx: ctx,
}
}

type ExecCmd struct {
*exec.Cmd
//nolint:containedctx
ctx context.Context
before Func
after Func
}

func (e *ExecCmd) SetBefore(f Func) *ExecCmd {
e.before = f
return e
}

func (e *ExecCmd) execBefore() error {
if e.before == nil {
return nil
}
return e.before(e.ctx)
}

func (e *ExecCmd) SetAfter(f Func) *ExecCmd {
e.after = f
return e
}

func (e *ExecCmd) execAfter() error {
if e.after == nil {
return nil
}
return e.after(e.ctx)
}
17 changes: 10 additions & 7 deletions src/cmdRunner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ var ErrIpAlreadySet = errors.New("RTNETLINK answers: File exists")
var ErrCannotFindDevice = errors.New(`Cannot find device "wg0"`)
var ErrOperationNotPermitted = errors.New(`Operation not permitted`)

type ExecErrInterface interface {
error
ExitCode() int
}

type execError struct {
cmd *exec.Cmd
cmd *ExecCmd
prev error
exitCode int
}
Expand All @@ -39,13 +34,17 @@ func (e execError) Is(target error) bool {
return errors.Is(e.prev, target)
}

func Run(cmd *exec.Cmd) ([]byte, ExecErrInterface) {
func Run(cmd *ExecCmd) ([]byte, error) {
output := &bytes.Buffer{}
errOutput := &bytes.Buffer{}
cmd.Stdout = output
cmd.Stderr = errOutput
cmd.Env = append(os.Environ(), cmd.Env...)

if err := cmd.execBefore(); err != nil {
return nil, err
}

if err := cmd.Run(); err != nil {
exitCode := 0
var exitError *exec.ExitError
Expand Down Expand Up @@ -81,5 +80,9 @@ func Run(cmd *exec.Cmd) ([]byte, ExecErrInterface) {
return nil, execError
}

if err := cmd.execAfter(); err != nil {
return nil, err
}

return output.Bytes(), nil
}
10 changes: 5 additions & 5 deletions src/constants/darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package constants

import (
"os"
"path"
"path/filepath"
)

func getDataFilePathsReceivers() []pathReceiver {
Expand All @@ -21,7 +21,7 @@ func getDataFilePathsReceivers() []pathReceiver {
func getLogFilePathReceivers() []pathReceiver {
return []pathReceiver{
receiverFromEnv(CliLogFilePathEnvVar),
receiverFromPath(path.Join("/usr/local/var/log/", ZeropsLogFile)),
receiverFromPath(filepath.Join("/usr/local/var/log/", ZeropsLogFile)),
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, ZeropsLogFile),
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, ZeropsLogFile),
receiverFromOsFunc(os.UserHomeDir, "zerops."+ZeropsLogFile),
Expand All @@ -32,9 +32,9 @@ func getLogFilePathReceivers() []pathReceiver {
func getWgConfigFilePathReceivers() []pathReceiver {
return []pathReceiver{
receiverFromEnv(CliWgConfigPathEnvVar),
receiverFromPath(path.Join("/etc/wireguard/", WgConfigFile)),
receiverFromPath(path.Join("/usr/local/etc/wireguard/", WgConfigFile)),
receiverFromPath(path.Join("/opt/homebrew/etc/wireguard/", WgConfigFile)),
receiverFromPath(filepath.Join("/etc/wireguard/", WgConfigFile)),
receiverFromPath(filepath.Join("/usr/local/etc/wireguard/", WgConfigFile)),
receiverFromPath(filepath.Join("/opt/homebrew/etc/wireguard/", WgConfigFile)),
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, WgConfigFile),
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, WgConfigFile),
receiverFromOsFunc(os.UserHomeDir, WgConfigFile),
Expand Down
10 changes: 5 additions & 5 deletions src/constants/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package constants

import (
"os"
"path"
"path/filepath"
)

func getDataFilePathsReceivers() []pathReceiver {
Expand All @@ -21,7 +21,7 @@ func getDataFilePathsReceivers() []pathReceiver {
func getLogFilePathReceivers() []pathReceiver {
return []pathReceiver{
receiverFromEnv(CliLogFilePathEnvVar),
receiverFromPath(path.Join("/var/log/", ZeropsLogFile)),
receiverFromPath(filepath.Join("/var/log/", ZeropsLogFile)),
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, ZeropsLogFile),
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, ZeropsLogFile),
receiverFromOsFunc(os.UserHomeDir, "zerops."+ZeropsLogFile),
Expand All @@ -32,9 +32,9 @@ func getLogFilePathReceivers() []pathReceiver {
func getWgConfigFilePathReceivers() []pathReceiver {
return []pathReceiver{
receiverFromEnv(CliWgConfigPathEnvVar),
receiverFromPath(path.Join("/etc/wireguard/", WgConfigFile)),
receiverFromPath(path.Join("/usr/local/etc/wireguard/", WgConfigFile)),
receiverFromPath(path.Join("/opt/homebrew/etc/wireguard/", WgConfigFile)),
receiverFromPath(filepath.Join("/etc/wireguard/", WgConfigFile)),
receiverFromPath(filepath.Join("/usr/local/etc/wireguard/", WgConfigFile)),
receiverFromPath(filepath.Join("/opt/homebrew/etc/wireguard/", WgConfigFile)),
receiverFromOsFunc(os.UserConfigDir, ZeropsDir, WgConfigFile),
receiverFromOsFunc(os.UserHomeDir, ZeropsDir, WgConfigFile),
receiverFromOsFunc(os.UserHomeDir, WgConfigFile),
Expand Down
Loading
Loading