Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-hajek committed Mar 19, 2024
1 parent 64f0e9a commit 93caa65
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ func rootCmd() *cmdBuilder.Cmd {
}

func guestInfoPart(tableBody *uxBlock.TableBody) {
cliDataFilePath, err := constants.CliDataFilePath()
cliDataFilePath, _, err := constants.CliDataFilePath()
if err != nil {
cliDataFilePath = err.Error()
}
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoCliDataFilePath), cliDataFilePath)

logFilePath, err := constants.LogFilePath()
logFilePath, _, err := constants.LogFilePath()
if err != nil {
logFilePath = err.Error()
}
tableBody.AddStringsRow(i18n.T(i18n.StatusInfoLogFilePath), logFilePath)

wgConfigFilePath, err := constants.WgConfigFilePath()
wgConfigFilePath, _, err := constants.WgConfigFilePath()
if err != nil {
wgConfigFilePath = err.Error()
}
Expand Down
12 changes: 8 additions & 4 deletions src/cmd/statusShowDebugLogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"io"
"os"

"github.com/pkg/errors"

"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/constants"
"github.com/zeropsio/zcli/src/file"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock/styles"
)
Expand All @@ -18,15 +21,16 @@ func statusShowDebugLogsCmd() *cmdBuilder.Cmd {
Short(i18n.T(i18n.CmdStatusShowDebugLogs)).
HelpFlag(i18n.T(i18n.StatusShowDebugLogsHelp)).
GuestRunFunc(func(ctx context.Context, cmdData *cmdBuilder.GuestCmdData) error {
logFilePath, err := constants.LogFilePath()
logFilePath, fileMode, err := constants.LogFilePath()
if err != nil {
return err
}

f, err := os.OpenFile(logFilePath, os.O_RDONLY, 0777)
f, err := file.Open(logFilePath, os.O_RDONLY, fileMode)
if err != nil {
return err
}
defer f.Close()

line := ""
var cursor int64 = 0
Expand All @@ -43,13 +47,13 @@ func statusShowDebugLogsCmd() *cmdBuilder.Cmd {
cursor -= 1
_, err = f.Seek(cursor, io.SeekEnd)
if err != nil {
return err
return errors.WithStack(err)
}

char := make([]byte, 1)
_, err = f.Read(char)
if err != nil {
return err
return errors.WithStack(err)
}

if cursor != -1 && (char[0] == 10 || char[0] == 13) { // stop if we find a line
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/vpnDown.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/zeropsio/zcli/src/cmdBuilder"
"github.com/zeropsio/zcli/src/cmdRunner"
"github.com/zeropsio/zcli/src/constants"
"github.com/zeropsio/zcli/src/file"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
Expand All @@ -31,13 +32,13 @@ func disconnectVpn(ctx context.Context, uxBlocks uxBlock.UxBlocks) error {
return errors.New(i18n.T(i18n.VpnWgQuickIsNotInstalled))
}

filePath, err := constants.WgConfigFilePath()
filePath, fileMode, err := constants.WgConfigFilePath()
if err != nil {
return err
}

// create empty file if not exists, only thing wg-quick needs is a proper file name
f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0666)
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE, fileMode)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/zeropsio/zcli/src/cmdRunner"
"github.com/zeropsio/zcli/src/constants"
"github.com/zeropsio/zcli/src/entity"
"github.com/zeropsio/zcli/src/file"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/nettools"
"github.com/zeropsio/zcli/src/uxBlock"
Expand Down Expand Up @@ -92,12 +93,12 @@ func vpnUpCmd() *cmdBuilder.Cmd {
return err
}

filePath, err := constants.WgConfigFilePath()
filePath, fileMode, err := constants.WgConfigFilePath()
if err != nil {
return err
}

f, err := os.Create(filePath)
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE, fileMode)
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions src/cmdBuilder/executeRootCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"golang.org/x/term"
"gopkg.in/yaml.v3"

"github.com/zeropsio/zcli/src/cliStorage"
"github.com/zeropsio/zcli/src/constants"
"github.com/zeropsio/zcli/src/errorsx"
Expand All @@ -20,8 +23,6 @@ import (
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zerops-go/apiError"
"golang.org/x/term"
"gopkg.in/yaml.v3"
)

func ExecuteRootCmd(rootCmd *Cmd) (err error) {
Expand Down Expand Up @@ -121,13 +122,14 @@ func createLoggers(isTerminal bool) (*logger.Handler, *logger.Handler) {
IsTerminal: isTerminal,
})

loggerFilePath, err := constants.LogFilePath()
loggerFilePath, fileMode, err := constants.LogFilePath()
if err != nil {
outputLogger.Warning(styles.WarningLine(err.Error()))
}

debugFileLogger := logger.NewDebugFileLogger(logger.DebugFileConfig{
FilePath: loggerFilePath,
FileMode: fileMode,
})

return outputLogger, debugFileLogger
Expand All @@ -145,13 +147,14 @@ func regSignals(contextCancel func()) {
}

func createCliStorage() (*cliStorage.Handler, error) {
filePath, err := constants.CliDataFilePath()
filePath, fileMode, err := constants.CliDataFilePath()
if err != nil {
return nil, err
}
s, err := storage.New[cliStorage.Data](
storage.Config{
FilePath: filePath,
FileMode: fileMode,
},
)
return &cliStorage.Handler{Handler: s}, err
Expand Down
52 changes: 27 additions & 25 deletions src/constants/zerops.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/zeropsio/zcli/src/file"
"github.com/zeropsio/zcli/src/i18n"
)

Expand All @@ -22,69 +24,69 @@ const (
CliTerminalMode = "ZEROPS_CLI_TERMINAL_MODE"
)

type pathReceiver func() (path string, err error)
type pathReceiver func(fileMode os.FileMode) (path string, err error)

func CliDataFilePath() (string, error) {
return checkReceivers(getDataFilePathsReceivers(), i18n.UnableToWriteCliData)
func CliDataFilePath() (string, os.FileMode, error) {
return checkReceivers(getDataFilePathsReceivers(), 0600, i18n.UnableToWriteCliData)
}

func LogFilePath() (string, error) {
return checkReceivers(getLogFilePathReceivers(), i18n.UnableToWriteLogFile)
func LogFilePath() (string, os.FileMode, error) {
return checkReceivers(getLogFilePathReceivers(), 0666, i18n.UnableToWriteLogFile)
}

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

func checkReceivers(pathReceivers []pathReceiver, errorText string) (string, error) {
path := findFirstWritablePath(pathReceivers)
func checkReceivers(pathReceivers []pathReceiver, fileMode os.FileMode, errorText string) (string, os.FileMode, error) {
path := findFirstWritablePath(pathReceivers, fileMode)
if path == "" {
paths := make([]string, 0, len(pathReceivers))
for _, p := range pathReceivers {
_, err := p()
_, err := p(fileMode)
paths = append(paths, err.Error())
}
return "", errors.New(i18n.T(errorText, "\n"+strings.Join(paths, "\n")+"\n"))
return "", 0, errors.New(i18n.T(errorText, "\n"+strings.Join(paths, "\n")+"\n"))
}
return path, nil
return path, fileMode, nil
}

func receiverFromPath(path string) pathReceiver {
return func() (string, error) {
return checkPath(path)
return func(fileMode os.FileMode) (string, error) {
return checkPath(path, fileMode)
}
}

func receiverFromEnv(envName string) pathReceiver {
return func() (string, error) {
return func(fileMode os.FileMode) (string, error) {
env := os.Getenv(envName)
if env == "" {
return "", errors.Errorf("env %s is empty", envName)
}
return checkPath(env)
return checkPath(env, fileMode)
}
}

func receiverFromOsFunc(osFunc func() (string, error), elem ...string) pathReceiver {
return func() (string, error) {
return func(fileMode os.FileMode) (string, error) {
dir, err := osFunc()
if err != nil {
return "", err
}

return checkPath(filepath.Join(append([]string{dir}, elem...)...))
return checkPath(filepath.Join(append([]string{dir}, elem...)...), fileMode)
}
}

func receiverFromOsTemp(elem ...string) pathReceiver {
return func() (string, error) {
return checkPath(filepath.Join(append([]string{os.TempDir()}, elem...)...))
return func(fileMode os.FileMode) (string, error) {
return checkPath(filepath.Join(append([]string{os.TempDir()}, elem...)...), fileMode)
}
}

func findFirstWritablePath(paths []pathReceiver) string {
func findFirstWritablePath(paths []pathReceiver, fileMode os.FileMode) string {
for _, p := range paths {
path, err := p()
path, err := p(fileMode)
if err == nil {
return path
}
Expand All @@ -93,14 +95,14 @@ func findFirstWritablePath(paths []pathReceiver) string {
return ""
}

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

if err := os.MkdirAll(dir, 0666); err != nil {
if err := os.MkdirAll(dir, 0755); err != nil {
return "", err
}

f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
f, err := file.Open(filePath, os.O_RDWR|os.O_CREATE, fileMode)
if err != nil {
return "", err
}
Expand Down
20 changes: 20 additions & 0 deletions src/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package file

import (
"os"

"github.com/pkg/errors"
)

func Open(filePath string, flag int, fileMode os.FileMode) (*os.File, error) {
f, err := os.OpenFile(filePath, flag, fileMode)
if err != nil {
return nil, errors.WithStack(err)
}
err = os.Chmod(filePath, fileMode)
if err != nil {
return nil, errors.WithStack(err)
}

return f, nil
}
3 changes: 2 additions & 1 deletion src/flagParams/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/zeropsio/zcli/src/constants"
)

Expand Down Expand Up @@ -111,7 +112,7 @@ func (h *Handler) InitViper() {
if err == nil {
h.viper.AddConfigPath(path)
}
cliDataPath, err := constants.CliDataFilePath()
cliDataPath, _, err := constants.CliDataFilePath()
if err == nil {
h.viper.AddConfigPath(cliDataPath)
}
Expand Down
7 changes: 5 additions & 2 deletions src/logger/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"io"
"os"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -36,6 +37,7 @@ func NewOutputLogger(config OutputConfig) *Handler {

type DebugFileConfig struct {
FilePath string
FileMode os.FileMode
}

func NewDebugFileLogger(config DebugFileConfig) *Handler {
Expand All @@ -45,8 +47,9 @@ func NewDebugFileLogger(config DebugFileConfig) *Handler {

if config.FilePath != "" {
l.AddHook(&VarLogHook{
path: config.FilePath,
levels: []logrus.Level{logrus.DebugLevel, logrus.InfoLevel, logrus.WarnLevel, logrus.ErrorLevel},
path: config.FilePath,
fileMode: config.FileMode,
levels: []logrus.Level{logrus.DebugLevel, logrus.InfoLevel, logrus.WarnLevel, logrus.ErrorLevel},
formatter: &logrus.TextFormatter{
DisableColors: true,
},
Expand Down
5 changes: 4 additions & 1 deletion src/logger/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"sync"

"github.com/sirupsen/logrus"

"github.com/zeropsio/zcli/src/file"
)

type VarLogHook struct {
path string
fileMode os.FileMode
levels []logrus.Level
formatter logrus.Formatter

Expand All @@ -31,7 +34,7 @@ func (hook *VarLogHook) Fire(entry *logrus.Entry) error {
return err
}

f, err := os.OpenFile(hook.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
f, err := file.Open(hook.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, hook.fileMode)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open file, %v", err)
return err
Expand Down
Loading

0 comments on commit 93caa65

Please sign in to comment.