-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from shalb/shell-unit
refactoring
- Loading branch information
Showing
64 changed files
with
2,716 additions
and
2,195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/apex/log" | ||
) | ||
|
||
// CreateFileRepresentation describes the unit's file that will be saved in the unit's working directory when building. | ||
type CreateFileRepresentation struct { | ||
FileName string `yaml:"file"` | ||
FileMode fs.FileMode `yaml:"file_mode,omitempty"` | ||
Content string `yaml:"content"` | ||
} | ||
|
||
// FilesListT describes all unit's files will be write to the unit's working directory when building. | ||
type FilesListT []*CreateFileRepresentation | ||
|
||
// Find searchs file and returns a pointer to it or nil if not found. | ||
func (l *FilesListT) Find(fileName string) int { | ||
for i, f := range *l { | ||
if f.FileName == fileName { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func remove(slice []int, s int) []int { | ||
return append(slice[:s], slice[s+1:]...) | ||
} | ||
|
||
func (l *FilesListT) SPrint() string { | ||
var res string | ||
for _, f := range *l { | ||
res += fmt.Sprintf("%v: %v\n", f.FileName, f.FileMode) | ||
} | ||
return res | ||
} | ||
|
||
// Add insert the new file with name fileName, returns error if file with this name already exists. | ||
func (l *FilesListT) Add(fileName string, content string, mode fs.FileMode) error { | ||
if l.Find(fileName) >= 0 { | ||
return fmt.Errorf("add file: file '%v' already exists", fileName) | ||
} | ||
*l = append(*l, | ||
&CreateFileRepresentation{ | ||
FileName: fileName, | ||
Content: content, | ||
FileMode: mode, | ||
}) | ||
return nil | ||
} | ||
|
||
// ReadDir recursively reads files in path, saving relative pathes from baseDir. | ||
func (l *FilesListT) ReadDir(path, baseDir string) (err error) { | ||
_, err = filepath.Rel(baseDir, path) | ||
if err != nil { | ||
return | ||
} | ||
err = filepath.Walk(path, | ||
func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() { | ||
relPath, err := filepath.Rel(baseDir, path) | ||
if err != nil { | ||
return err | ||
} | ||
content, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
err = l.Add(relPath, string(content), info.Mode()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
return | ||
} | ||
|
||
// WriteFiles write all files to path. | ||
func (l *FilesListT) WriteFiles(path string) (err error) { | ||
for _, file := range *l { | ||
var fileName, fileDir, fileFullName string | ||
splittedPath := strings.Split(file.FileName, "/") | ||
if len(splittedPath) < 2 { | ||
fileDir = path | ||
fileName = file.FileName | ||
} else { | ||
fileDir = filepath.Join(path, filepath.Join(splittedPath[0:len(splittedPath)-1]...)) | ||
fileName = splittedPath[len(splittedPath)-1] | ||
err = os.MkdirAll(fileDir, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
fileFullName = filepath.Join(fileDir, fileName) | ||
log.Debugf("Writing file: %v", fileFullName) | ||
err = ioutil.WriteFile(fileFullName, []byte(file.Content), file.FileMode) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return | ||
} | ||
|
||
// Delete delete file with name fileName, do nothing if not found. | ||
func (l *FilesListT) Delete(fileName string) { | ||
if i := l.Find(fileName); i >= 0 { | ||
*l = append((*l)[:i], (*l)[i+1:]...) | ||
} | ||
return | ||
} |
Oops, something went wrong.