github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/cmd/edgecore/app/options/options.go (about)

     1  /*
     2  Copyright 2019 The KubeEdge Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8     http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package options
    18  
    19  import (
    20  	"fmt"
    21  	"path"
    22  
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  	cliflag "k8s.io/component-base/cli/flag"
    25  
    26  	"github.com/kubeedge/kubeedge/common/constants"
    27  	"github.com/kubeedge/kubeedge/pkg/apis/componentconfig/edgecore/v1alpha1"
    28  	"github.com/kubeedge/kubeedge/pkg/util/validation"
    29  )
    30  
    31  type EdgeCoreOptions struct {
    32  	ConfigFile string
    33  }
    34  
    35  func NewEdgeCoreOptions() *EdgeCoreOptions {
    36  	return &EdgeCoreOptions{
    37  		ConfigFile: path.Join(constants.DefaultConfigDir, "edgecore.yaml"),
    38  	}
    39  }
    40  
    41  func (o *EdgeCoreOptions) Flags() (fss cliflag.NamedFlagSets) {
    42  	fs := fss.FlagSet("global")
    43  	fs.StringVar(&o.ConfigFile, "config", o.ConfigFile, "The path to the configuration file. Flags override values in this file.")
    44  	return
    45  }
    46  
    47  func (o *EdgeCoreOptions) Validate() []error {
    48  	var errs []error
    49  	if !validation.FileIsExist(o.ConfigFile) {
    50  		errs = append(errs, field.Required(field.NewPath("config"),
    51  			fmt.Sprintf("config file %v not exist. For the configuration file format, please refer to --minconfig and --defaultconfig command", o.ConfigFile)))
    52  	}
    53  	return errs
    54  }
    55  
    56  func (o *EdgeCoreOptions) Config() (*v1alpha1.EdgeCoreConfig, error) {
    57  	cfg := v1alpha1.NewDefaultEdgeCoreConfig()
    58  	if err := cfg.Parse(o.ConfigFile); err != nil {
    59  		return nil, err
    60  	}
    61  	return cfg, nil
    62  }