mirror of
https://github.com/mudler/luet.git
synced 2025-09-15 23:00:04 +00:00
Define context for scoped operation across core types
It holds necessary state plus additional information relative to the context which we are being run to (e.g. if we are in a terminal or not). Besides in the future we can use it also as a contextual logger to provide more smart logging capabilities. This also replace the general global configuration instance that previously was share between the core components.
This commit is contained in:
116
pkg/api/core/config/config_protect.go
Normal file
116
pkg/api/core/config/config_protect.go
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright © 2019-2020 Ettore Di Giacinto <mudler@gentoo.org>
|
||||
// Daniele Rondina <geaaru@sabayonlinux.org>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ConfigProtectConfFile struct {
|
||||
Filename string
|
||||
|
||||
Name string `mapstructure:"name" yaml:"name" json:"name"`
|
||||
Directories []string `mapstructure:"dirs" yaml:"dirs" json:"dirs"`
|
||||
}
|
||||
|
||||
func NewConfigProtectConfFile(filename string) *ConfigProtectConfFile {
|
||||
return &ConfigProtectConfFile{
|
||||
Filename: filename,
|
||||
Name: "",
|
||||
Directories: []string{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ConfigProtectConfFile) String() string {
|
||||
return fmt.Sprintf("[%s] filename: %s, dirs: %s", c.Name, c.Filename,
|
||||
c.Directories)
|
||||
}
|
||||
|
||||
type ConfigProtect struct {
|
||||
AnnotationDir string
|
||||
MapProtected map[string]bool
|
||||
}
|
||||
|
||||
func NewConfigProtect(annotationDir string) *ConfigProtect {
|
||||
if len(annotationDir) > 0 && annotationDir[0:1] != "/" {
|
||||
annotationDir = "/" + annotationDir
|
||||
}
|
||||
return &ConfigProtect{
|
||||
AnnotationDir: annotationDir,
|
||||
MapProtected: make(map[string]bool, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ConfigProtect) AddAnnotationDir(d string) {
|
||||
c.AnnotationDir = d
|
||||
}
|
||||
|
||||
func (c *ConfigProtect) GetAnnotationDir() string {
|
||||
return c.AnnotationDir
|
||||
}
|
||||
|
||||
func (c *ConfigProtect) Map(files []string, protected []ConfigProtectConfFile) {
|
||||
|
||||
for _, file := range files {
|
||||
|
||||
if file[0:1] != "/" {
|
||||
file = "/" + file
|
||||
}
|
||||
|
||||
if len(protected) > 0 {
|
||||
for _, conf := range protected {
|
||||
for _, dir := range conf.Directories {
|
||||
// Note file is without / at begin (on unpack)
|
||||
if strings.HasPrefix(file, filepath.Clean(dir)) {
|
||||
// docker archive modifier works with path without / at begin.
|
||||
c.MapProtected[file] = true
|
||||
goto nextFile
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if c.AnnotationDir != "" && strings.HasPrefix(file, filepath.Clean(c.AnnotationDir)) {
|
||||
c.MapProtected[file] = true
|
||||
}
|
||||
nextFile:
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (c *ConfigProtect) Protected(file string) bool {
|
||||
if file[0:1] != "/" {
|
||||
file = "/" + file
|
||||
}
|
||||
_, ans := c.MapProtected[file]
|
||||
return ans
|
||||
}
|
||||
|
||||
func (c *ConfigProtect) GetProtectFiles(withSlash bool) []string {
|
||||
ans := []string{}
|
||||
|
||||
for key, _ := range c.MapProtected {
|
||||
if withSlash {
|
||||
ans = append(ans, key)
|
||||
} else {
|
||||
ans = append(ans, key[1:])
|
||||
}
|
||||
}
|
||||
return ans
|
||||
}
|
Reference in New Issue
Block a user