77 "strings"
88
99 "github.com/pkg/errors"
10+
11+ "github.com/zeropsio/zcli/src/file"
1012 "github.com/zeropsio/zcli/src/i18n"
1113)
1214
@@ -22,64 +24,69 @@ const (
2224 CliTerminalMode = "ZEROPS_CLI_TERMINAL_MODE"
2325)
2426
25- type pathReceiver func () (path string , err error )
27+ type pathReceiver func (fileMode os. FileMode ) (path string , err error )
2628
27- func CliDataFilePath () (string , error ) {
28- return checkReceivers (getDataFilePathsReceivers (), i18n .UnableToWriteCliData )
29+ func CliDataFilePath () (string , os. FileMode , error ) {
30+ return checkReceivers (getDataFilePathsReceivers (), 0600 , i18n .UnableToWriteCliData )
2931}
3032
31- func LogFilePath () (string , error ) {
32- return checkReceivers (getLogFilePathReceivers (), i18n .UnableToWriteLogFile )
33+ func LogFilePath () (string , os. FileMode , error ) {
34+ return checkReceivers (getLogFilePathReceivers (), 0666 , i18n .UnableToWriteLogFile )
3335}
3436
35- func WgConfigFilePath () (string , error ) {
36- return checkReceivers (getWgConfigFilePathReceivers (), i18n .UnableToWriteLogFile )
37+ func WgConfigFilePath () (string , os. FileMode , error ) {
38+ return checkReceivers (getWgConfigFilePathReceivers (), 0600 , i18n .UnableToWriteLogFile )
3739}
3840
39- func checkReceivers (pathReceivers []pathReceiver , errorText string ) (string , error ) {
40- path := findFirstWritablePath (pathReceivers )
41+ func checkReceivers (pathReceivers []pathReceiver , fileMode os. FileMode , errorText string ) (string , os. FileMode , error ) {
42+ path := findFirstWritablePath (pathReceivers , fileMode )
4143 if path == "" {
4244 paths := make ([]string , 0 , len (pathReceivers ))
4345 for _ , p := range pathReceivers {
44- _ , err := p ()
46+ _ , err := p (fileMode )
4547 paths = append (paths , err .Error ())
4648 }
47- return "" , errors .New (i18n .T (errorText , "\n " + strings .Join (paths , "\n " )+ "\n " ))
49+ return "" , 0 , errors .New (i18n .T (errorText , "\n " + strings .Join (paths , "\n " )+ "\n " ))
4850 }
49- return path , nil
51+ return path , fileMode , nil
5052}
5153
5254func receiverFromPath (path string ) pathReceiver {
53- return func () (string , error ) {
54- return checkPath (path )
55+ return func (fileMode os. FileMode ) (string , error ) {
56+ return checkPath (path , fileMode )
5557 }
5658}
5759
5860func receiverFromEnv (envName string ) pathReceiver {
59- return func () (string , error ) {
61+ return func (fileMode os. FileMode ) (string , error ) {
6062 env := os .Getenv (envName )
6163 if env == "" {
6264 return "" , errors .Errorf ("env %s is empty" , envName )
6365 }
64- return checkPath (env )
66+ return checkPath (env , fileMode )
6567 }
6668}
6769
6870func receiverFromOsFunc (osFunc func () (string , error ), elem ... string ) pathReceiver {
69- return func () (string , error ) {
71+ return func (fileMode os. FileMode ) (string , error ) {
7072 dir , err := osFunc ()
7173 if err != nil {
7274 return "" , err
7375 }
74- elem = append ([]string {dir }, elem ... )
7576
76- return filepath .Join (elem ... ), nil
77+ return checkPath (filepath .Join (append ([]string {dir }, elem ... )... ), fileMode )
78+ }
79+ }
80+
81+ func receiverFromOsTemp (elem ... string ) pathReceiver {
82+ return func (fileMode os.FileMode ) (string , error ) {
83+ return checkPath (filepath .Join (append ([]string {os .TempDir ()}, elem ... )... ), fileMode )
7784 }
7885}
7986
80- func findFirstWritablePath (paths []pathReceiver ) string {
87+ func findFirstWritablePath (paths []pathReceiver , fileMode os. FileMode ) string {
8188 for _ , p := range paths {
82- path , err := p ()
89+ path , err := p (fileMode )
8390 if err == nil {
8491 return path
8592 }
@@ -88,14 +95,14 @@ func findFirstWritablePath(paths []pathReceiver) string {
8895 return ""
8996}
9097
91- func checkPath (filePath string ) (string , error ) {
98+ func checkPath (filePath string , fileMode os. FileMode ) (string , error ) {
9299 dir := path .Dir (filePath )
93100
94- if err := os .MkdirAll (dir , 0775 ); err != nil {
101+ if err := os .MkdirAll (dir , 0755 ); err != nil {
95102 return "" , err
96103 }
97104
98- f , err := os . OpenFile (filePath , os .O_APPEND | os .O_CREATE | os . O_RDWR , 0666 )
105+ f , err := file . Open (filePath , os .O_RDWR | os .O_CREATE , fileMode )
99106 if err != nil {
100107 return "" , err
101108 }
0 commit comments