github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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.Conf) 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 ExportExperimentalFlags(tr config.Conf) error {
    52  	dir := dirs.FeaturesDir
    53  	if err := os.MkdirAll(dir, 0755); err != nil {
    54  		return err
    55  	}
    56  	features := features.KnownFeatures()
    57  	content := make(map[string]*osutil.FileState, len(features))
    58  	for _, feature := range features {
    59  		if !feature.IsExported() {
    60  			continue
    61  		}
    62  		isEnabled, err := config.GetFeatureFlag(tr, feature)
    63  		if err != nil {
    64  			return err
    65  		}
    66  		if isEnabled {
    67  			content[feature.String()] = &osutil.FileState{Mode: 0644}
    68  		}
    69  	}
    70  	_, _, err := osutil.EnsureDirState(dir, "*", content)
    71  	return err
    72  }