github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/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 earlyExperimentalSettingsFilter(values, early map[string]interface{}) {
    40  	for key, v := range values {
    41  		if strings.HasPrefix(key, "experimental.") && supportedConfigurations["core."+key] {
    42  			early[key] = v
    43  		}
    44  	}
    45  }
    46  
    47  func validateExperimentalSettings(tr config.ConfGetter) error {
    48  	for k := range supportedConfigurations {
    49  		if !strings.HasPrefix(k, "core.experimental.") {
    50  			continue
    51  		}
    52  		if err := validateBoolFlag(tr, strings.TrimPrefix(k, "core.")); err != nil {
    53  			return err
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  func doExportExperimentalFlags(tr config.ConfGetter, opts *fsOnlyContext) error {
    60  	var dir string
    61  	if opts != nil {
    62  		dir = dirs.FeaturesDirUnder(opts.RootDir)
    63  	} else {
    64  		dir = dirs.FeaturesDir
    65  	}
    66  	if err := os.MkdirAll(dir, 0755); err != nil {
    67  		return err
    68  	}
    69  	feat := features.KnownFeatures()
    70  	content := make(map[string]osutil.FileState, len(feat))
    71  	for _, feature := range feat {
    72  		if !feature.IsExported() {
    73  			continue
    74  		}
    75  		isEnabled, err := features.Flag(tr, feature)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		if isEnabled {
    80  			content[feature.String()] = &osutil.MemoryFileState{Mode: 0644}
    81  		}
    82  	}
    83  	_, _, err := osutil.EnsureDirState(dir, "*", content)
    84  	return err
    85  }
    86  
    87  func ExportExperimentalFlags(tr config.ConfGetter) error {
    88  	return doExportExperimentalFlags(tr, nil)
    89  }