From 4504d1b3910dcc22aa7d691814c499abedb02ecd Mon Sep 17 00:00:00 2001 From: "jan.hajek@zerops.io" Date: Wed, 3 Apr 2024 19:50:07 +0200 Subject: [PATCH] fix: (windows) paths --- Makefile | 11 ++++++++++- src/cmd/serviceDeploy.go | 10 +++++----- src/cmd/servicePush.go | 4 ++-- src/cmd/vpnDown.go | 2 +- src/cmd/vpnUp.go | 20 +++++++++++++------- src/constants/darwin.go | 10 +++++----- src/constants/linux.go | 10 +++++----- src/constants/zerops.go | 6 +++--- src/i18n/en.go | 12 ++++++++---- src/i18n/i18n.go | 12 ++++++++---- src/storage/exists.go | 16 ---------------- src/storage/handler.go | 30 ++++++++++-------------------- src/wg/darwin.go | 2 +- src/wg/linux.go | 2 +- src/wg/windows.go | 4 ++-- 15 files changed, 74 insertions(+), 77 deletions(-) delete mode 100644 src/storage/exists.go diff --git a/Makefile b/Makefile index 9c52e7e5..f15fda0c 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + 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 diff --git a/src/cmd/serviceDeploy.go b/src/cmd/serviceDeploy.go index 6c4a8240..92651df4 100644 --- a/src/cmd/serviceDeploy.go +++ b/src/cmd/serviceDeploy.go @@ -4,7 +4,7 @@ import ( "context" "io" "os" - "path" + "path/filepath" "time" "github.com/zeropsio/zcli/src/archiveClient" @@ -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 @@ -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), }}, ) diff --git a/src/cmd/servicePush.go b/src/cmd/servicePush.go index 804fef35..43548928 100644 --- a/src/cmd/servicePush.go +++ b/src/cmd/servicePush.go @@ -4,7 +4,7 @@ import ( "context" "io" "os" - "path" + "path/filepath" "time" "github.com/zeropsio/zcli/src/archiveClient" @@ -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 diff --git a/src/cmd/vpnDown.go b/src/cmd/vpnDown.go index 6541892f..744cdacb 100644 --- a/src/cmd/vpnDown.go +++ b/src/cmd/vpnDown.go @@ -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 diff --git a/src/cmd/vpnUp.go b/src/cmd/vpnUp.go index 04aa61aa..31d710b6 100644 --- a/src/cmd/vpnUp.go +++ b/src/cmd/vpnUp.go @@ -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" @@ -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" @@ -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 } diff --git a/src/constants/darwin.go b/src/constants/darwin.go index 6e043cb9..0dde89dc 100644 --- a/src/constants/darwin.go +++ b/src/constants/darwin.go @@ -5,7 +5,7 @@ package constants import ( "os" - "path" + "path/filepath" ) func getDataFilePathsReceivers() []pathReceiver { @@ -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), @@ -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), diff --git a/src/constants/linux.go b/src/constants/linux.go index 26fc412f..dc448eb2 100644 --- a/src/constants/linux.go +++ b/src/constants/linux.go @@ -5,7 +5,7 @@ package constants import ( "os" - "path" + "path/filepath" ) func getDataFilePathsReceivers() []pathReceiver { @@ -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), @@ -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), diff --git a/src/constants/zerops.go b/src/constants/zerops.go index 44cc94dd..ea706412 100644 --- a/src/constants/zerops.go +++ b/src/constants/zerops.go @@ -2,7 +2,6 @@ package constants import ( "os" - "path" "path/filepath" "strings" @@ -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" @@ -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) { @@ -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 diff --git a/src/i18n/en.go b/src/i18n/en.go index f8791231..ba447a15 100644 --- a/src/i18n/en.go +++ b/src/i18n/en.go @@ -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.", @@ -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", diff --git a/src/i18n/i18n.go b/src/i18n/i18n.go index f982f2ac..d36b404a 100644 --- a/src/i18n/i18n.go +++ b/src/i18n/i18n.go @@ -111,6 +111,9 @@ const ( CmdHelpServiceDeploy = "CmdHelpServiceDeploy" CmdDescDeploy = "CmdDescDeploy" CmdDescDeployLong = "CmdDescDeployLong" + DeployRunning = "DeployRunning" + DeployFailed = "DeployFailed" + DeployFinished = "DeployFinished" // push CmdHelpPush = "CmdHelpPush" @@ -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" diff --git a/src/storage/exists.go b/src/storage/exists.go deleted file mode 100644 index ca85b772..00000000 --- a/src/storage/exists.go +++ /dev/null @@ -1,16 +0,0 @@ -package storage - -import ( - "os" -) - -func FileExists(path string) (bool, error) { - f, err := os.Stat(path) - if err == nil && !f.IsDir() { - return true, nil - } - if os.IsNotExist(err) { - return false, nil - } - return false, err -} diff --git a/src/storage/handler.go b/src/storage/handler.go index 2208a1d3..2110ceac 100644 --- a/src/storage/handler.go +++ b/src/storage/handler.go @@ -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 } @@ -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() @@ -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 } diff --git a/src/wg/darwin.go b/src/wg/darwin.go index 03ceb58f..68c72573 100644 --- a/src/wg/darwin.go +++ b/src/wg/darwin.go @@ -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) } diff --git a/src/wg/linux.go b/src/wg/linux.go index b7629a95..ffe9d157 100644 --- a/src/wg/linux.go +++ b/src/wg/linux.go @@ -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) } diff --git a/src/wg/windows.go b/src/wg/windows.go index 4c7a3516..efc6b72c 100644 --- a/src/wg/windows.go +++ b/src/wg/windows.go @@ -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 = `