github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/configstate/configcore/experimental.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package configcore 21 22 import ( 23 "os" 24 "strings" 25 26 "github.com/snapcore/snapd/dirs" 27 "github.com/snapcore/snapd/features" 28 "github.com/snapcore/snapd/osutil" 29 "github.com/snapcore/snapd/overlord/configstate/config" 30 ) 31 32 func init() { 33 for _, feature := range features.KnownFeatures() { 34 snapName, confName := feature.ConfigOption() 35 supportedConfigurations[snapName+"."+confName] = true 36 } 37 } 38 39 func validateExperimentalSettings(tr config.ConfGetter) error { 40 for k := range supportedConfigurations { 41 if !strings.HasPrefix(k, "core.experimental.") { 42 continue 43 } 44 if err := validateBoolFlag(tr, strings.TrimPrefix(k, "core.")); err != nil { 45 return err 46 } 47 } 48 return nil 49 } 50 51 func doExportExperimentalFlags(tr config.ConfGetter, opts *fsOnlyContext) error { 52 var dir string 53 if opts != nil { 54 dir = dirs.FeaturesDirUnder(opts.RootDir) 55 } else { 56 dir = dirs.FeaturesDir 57 } 58 if err := os.MkdirAll(dir, 0755); err != nil { 59 return err 60 } 61 feat := features.KnownFeatures() 62 content := make(map[string]osutil.FileState, len(feat)) 63 for _, feature := range feat { 64 if !feature.IsExported() { 65 continue 66 } 67 isEnabled, err := features.Flag(tr, feature) 68 if err != nil { 69 return err 70 } 71 if isEnabled { 72 content[feature.String()] = &osutil.MemoryFileState{Mode: 0644} 73 } 74 } 75 _, _, err := osutil.EnsureDirState(dir, "*", content) 76 return err 77 } 78 79 func ExportExperimentalFlags(tr config.ConfGetter) error { 80 return doExportExperimentalFlags(tr, nil) 81 }