Skip to content

Commit

Permalink
fix: (windows) paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-hajek authored and l-hellmann committed May 5, 2024
1 parent 36f5f97 commit 4504d1b
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 77 deletions.
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/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
4 changes: 2 additions & 2 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,7 +92,7 @@ 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
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
20 changes: 13 additions & 7 deletions 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 @@ -92,13 +92,19 @@ func vpnUpCmd() *cmdBuilder.Cmd {
return err
}

f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
if err != nil {
return err
}
if err := func() error {
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fileMode)
if err != nil {
return err
}
defer f.Close()

err = wg.GenerateConfig(f, privateKey, vpnSettings)
if err != nil {
err = wg.GenerateConfig(f, privateKey, vpnSettings)
if err != nil {
return err
}
return nil
}(); err != nil {
return err
}

Expand Down
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
6 changes: 3 additions & 3 deletions src/constants/zerops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package constants

import (
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -17,6 +16,7 @@ const (
ZeropsDir = "zerops"
ZeropsLogFile = "zerops.log"
WgConfigFile = "zerops.conf"
WgInterfaceName = "zerops"
CliDataFileName = "cli.data"
CliDataFilePathEnvVar = "ZEROPS_CLI_DATA_FILE_PATH"
CliLogFilePathEnvVar = "ZEROPS_CLI_LOG_FILE_PATH"
Expand All @@ -35,7 +35,7 @@ func LogFilePath() (string, os.FileMode, error) {
}

func WgConfigFilePath() (string, os.FileMode, error) {
return checkReceivers(getWgConfigFilePathReceivers(), 0600, i18n.UnableToWriteLogFile)
return checkReceivers(getWgConfigFilePathReceivers(), 0600, i18n.UnableToWriteWgConfigFile)
}

func checkReceivers(pathReceivers []pathReceiver, fileMode os.FileMode, errorText string) (string, os.FileMode, error) {
Expand Down Expand Up @@ -96,7 +96,7 @@ func findFirstWritablePath(paths []pathReceiver, fileMode os.FileMode) string {
}

func checkPath(filePath string, fileMode os.FileMode) (string, error) {
dir := path.Dir(filePath)
dir := filepath.Dir(filePath)

if err := os.MkdirAll(dir, 0755); err != nil {
return "", err
Expand Down
12 changes: 8 additions & 4 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ var en = map[string]string{
"directory. The working directory is by default the current directory and can be changed\n" +
"using the --workingDir flag. zCLI deploys selected directories and/or files to Zerops. \n\n" +
"To build your application in Zerops, use the zcli push command instead.",
DeployRunning: "Deploy is running",
DeployFailed: "Deploy failed",
DeployFinished: "Deploy finished",

// push
CmdHelpPush: "the service push command.",
Expand Down Expand Up @@ -242,10 +245,11 @@ var en = map[string]string{
CliLogFilePathEnvVar: "Path to a log file.",
CliDataFilePathEnvVar: "Path to data file.",

UnknownTerminalMode: "Unknown terminal mode: %s. Falling back to auto-discovery. Possible values: auto, enabled, disabled.",
UnableToDecodeJsonFile: "Unable to decode json file: %s",
UnableToWriteCliData: "Unable to write zcli data, paths tested: %s",
UnableToWriteLogFile: "Unable to write zcli debug log file, paths tested: %s",
UnknownTerminalMode: "Unknown terminal mode: %s. Falling back to auto-discovery. Possible values: auto, enabled, disabled.",
UnableToDecodeJsonFile: "Unable to decode json file: %s",
UnableToWriteCliData: "Unable to write zcli data, paths tested: %s",
UnableToWriteLogFile: "Unable to write zcli debug log file, paths tested: %s",
UnableToWriteWgConfigFile: "Unable to write zcli wireguard config file, paths tested: %s",

// args
ArgsOnlyOneOptionalAllowed: "optional arg %s can be only the last one",
Expand Down
12 changes: 8 additions & 4 deletions src/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ const (
CmdHelpServiceDeploy = "CmdHelpServiceDeploy"
CmdDescDeploy = "CmdDescDeploy"
CmdDescDeployLong = "CmdDescDeployLong"
DeployRunning = "DeployRunning"
DeployFailed = "DeployFailed"
DeployFinished = "DeployFinished"

// push
CmdHelpPush = "CmdHelpPush"
Expand Down Expand Up @@ -236,10 +239,11 @@ const (
CliLogFilePathEnvVar = "CliLogFilePathEnvVar"
CliDataFilePathEnvVar = "CliDataFilePathEnvVar"

UnknownTerminalMode = "UnknownTerminalMode"
UnableToDecodeJsonFile = "UnableToDecodeJsonFile"
UnableToWriteCliData = "UnableToWriteCliData"
UnableToWriteLogFile = "UnableToWriteLogFile"
UnknownTerminalMode = "UnknownTerminalMode"
UnableToDecodeJsonFile = "UnableToDecodeJsonFile"
UnableToWriteCliData = "UnableToWriteCliData"
UnableToWriteLogFile = "UnableToWriteLogFile"
UnableToWriteWgConfigFile = "UnableToWriteWgConfigFile"

// args
ArgsOnlyOneOptionalAllowed = "ArgsOnlyOneOptionalAllowed"
Expand Down
16 changes: 0 additions & 16 deletions src/storage/exists.go

This file was deleted.

30 changes: 10 additions & 20 deletions src/storage/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ func New[T any](config Config) (*Handler[T], error) {
}

func (h *Handler[T]) load() error {
storageFileExists, err := FileExists(h.config.FilePath)
fileInfo, err := os.Stat(h.config.FilePath)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return errors.WithStack(err)
}
if !storageFileExists {
if fileInfo.Size() == 0 {
if err := os.Remove(h.config.FilePath); err != nil {
return errors.WithStack(err)
}
return nil
}

Expand All @@ -48,29 +54,12 @@ func (h *Handler[T]) load() error {
}
defer f.Close()

// If the file is empty, set the default value and save it.
fi, err := f.Stat()
if err != nil {
return errors.WithStack(err)
}
if fi.Size() == 0 {
return h.Clear()
}

if err := json.NewDecoder(f).Decode(&h.data); err != nil {
return errors.WithMessagef(err, i18n.T(i18n.UnableToDecodeJsonFile, h.config.FilePath))
}

return nil
}

func (h *Handler[T]) Clear() error {
h.lock.Lock()
defer h.lock.Unlock()
var data T
return h.save(data)
}

func (h *Handler[T]) Update(callback func(T) T) (T, error) {
h.lock.Lock()
defer h.lock.Unlock()
Expand All @@ -95,10 +84,11 @@ func (h *Handler[T]) save(data T) error {
}(); err != nil {
return err
}
os.Remove(h.config.FilePath)
defer os.Remove(h.config.FilePath + ".new")
if err := os.Rename(h.config.FilePath+".new", h.config.FilePath); err != nil {
return errors.WithStack(err)
}
os.Remove(h.config.FilePath + ".new")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion src/wg/darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func UpCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wg-quick", "up", filePath)
}

func DownCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
func DownCmd(ctx context.Context, filePath, _ string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wg-quick", "down", filePath)
}

Expand Down
2 changes: 1 addition & 1 deletion src/wg/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func UpCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wg-quick", "up", filePath)
}

func DownCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
func DownCmd(ctx context.Context, filePath, _ string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wg-quick", "down", filePath)
}

Expand Down
4 changes: 2 additions & 2 deletions src/wg/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func UpCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wireguard", "/installtunnelservice", filePath)
}

func DownCmd(ctx context.Context, filePath string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wireguard", "/uninstalltunnelservice", filePath)
func DownCmd(ctx context.Context, _, interfaceName string) (err *exec.Cmd) {
return exec.CommandContext(ctx, "wireguard", "/uninstalltunnelservice", interfaceName)
}

var vpnTmpl = `
Expand Down

0 comments on commit 4504d1b

Please sign in to comment.