90bc093eedc1199b2a01d4a666f40d47d62319d2
[ric-plt/xapp-frame.git] / pkg / xapp / Utils.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package xapp
21
22 import (
23         "archive/zip"
24         "io"
25         "io/ioutil"
26         "os"
27         "path/filepath"
28         "strings"
29 )
30
31 type Utils struct {
32         baseDir string
33         status  string
34 }
35
36 func NewUtils() *Utils {
37         b := Config.GetString("controls.symptomdata.baseDir")
38         if b == "" {
39                 b = "/tmp/symptomdata/"
40         }
41
42         return &Utils{
43                 baseDir: b,
44         }
45 }
46
47 func (u *Utils) FileExists(f string) bool {
48         _, err := os.Stat(f)
49         return err == nil || os.IsExist(err)
50 }
51
52 func (u *Utils) CreateDir(path string) error {
53         if u.FileExists(path) {
54                 os.RemoveAll(path)
55         }
56         err := os.MkdirAll(path, os.ModePerm)
57         if err != nil {
58                 return err
59         }
60         os.Chmod(path, os.ModePerm)
61         return nil
62 }
63
64 func (u *Utils) DeleteFile(fileName string) {
65         os.Remove(fileName)
66 }
67
68 func (u *Utils) AddFileToZip(zipWriter *zip.Writer, filePath string, filename string) error {
69         fileToZip, err := os.Open(filename)
70         if err != nil {
71                 return err
72         }
73         defer fileToZip.Close()
74
75         info, err := fileToZip.Stat()
76         if err != nil {
77                 return err
78         }
79
80         header, err := zip.FileInfoHeader(info)
81         if err != nil {
82                 return err
83         }
84
85         if strings.HasPrefix(filename, filePath) {
86                 filename = strings.TrimPrefix(filename, filePath)
87         }
88         header.Name = filename
89         header.Method = zip.Deflate
90
91         writer, err := zipWriter.CreateHeader(header)
92         if err != nil {
93                 return err
94         }
95         if info.Size() > 0 {
96                 _, err = io.Copy(writer, fileToZip)
97         }
98         return err
99 }
100
101 func (u *Utils) ZipFiles(newZipFile *os.File, filePath string, files []string) error {
102         defer newZipFile.Close()
103         zipWriter := zip.NewWriter(newZipFile)
104         defer zipWriter.Close()
105         for _, file := range files {
106                 if err := u.AddFileToZip(zipWriter, filePath, file); err != nil {
107                         Logger.Error("AddFileToZip() failed: %+v", err.Error())
108                         return err
109                 }
110         }
111
112         return nil
113 }
114
115 func (u *Utils) FetchFiles(filePath string, fileList []string) []string {
116         files, err := ioutil.ReadDir(filePath)
117         if err != nil {
118                 Logger.Error("ioutil.ReadDir failed: %+v", err)
119                 return nil
120         }
121         for _, file := range files {
122                 if !file.IsDir() {
123                         fileList = append(fileList, filepath.Join(filePath, file.Name()))
124                 } else {
125                         subPath := filepath.Join(filePath, file.Name())
126                         subFiles, _ := ioutil.ReadDir(subPath)
127                         for _, subFile := range subFiles {
128                                 if !subFile.IsDir() {
129                                         fileList = append(fileList, filepath.Join(subPath, subFile.Name()))
130                                 } else {
131                                         fileList = u.FetchFiles(filepath.Join(subPath, subFile.Name()), fileList)
132                                 }
133                         }
134                 }
135         }
136         return fileList
137 }
138
139 func (u *Utils) WriteToFile(fileName string, data string) error {
140         f, err := os.Create(fileName)
141         defer f.Close()
142
143         if err != nil {
144                 Logger.Error("Unable to create file %s': %+v", fileName, err)
145         } else {
146                 _, err := io.WriteString(f, data)
147                 if err != nil {
148                         Logger.Error("Unable to write to file '%s': %+v", fileName, err)
149                         u.DeleteFile(fileName)
150                 }
151         }
152         return err
153 }