Merge pull request #227 from yanxuean/tomldefault

Add a default config file
This commit is contained in:
Lantao Liu 2017-09-17 22:38:40 -07:00 committed by GitHub
commit a8d4940285
2 changed files with 24 additions and 4 deletions

View File

@ -42,6 +42,11 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if o.PrintDefaultConfig {
o.PrintDefaultTomlConfig()
os.Exit(0)
}
if !o.EnableSelinux { if !o.EnableSelinux {
selinux.SetDisabled() selinux.SetDisabled()
} }

View File

@ -18,6 +18,7 @@ package options
import ( import (
"flag" "flag"
"fmt"
"os" "os"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -75,6 +76,8 @@ type CRIContainerdOptions struct {
ConfigFilePath string ConfigFilePath string
// PrintVersion indicates to print version information of cri-containerd. // PrintVersion indicates to print version information of cri-containerd.
PrintVersion bool PrintVersion bool
// PrintDefaultConfig indicates to print default toml config of cri-containerd.
PrintDefaultConfig bool
} }
// NewCRIContainerdOptions returns a reference to CRIContainerdOptions // NewCRIContainerdOptions returns a reference to CRIContainerdOptions
@ -110,6 +113,8 @@ func (c *CRIContainerdOptions) AddFlags(fs *pflag.FlagSet) {
false, "Enable selinux support.") false, "Enable selinux support.")
fs.StringVar(&c.SandboxImage, "sandbox-image", fs.StringVar(&c.SandboxImage, "sandbox-image",
"gcr.io/google_containers/pause:3.0", "The image used by sandbox container.") "gcr.io/google_containers/pause:3.0", "The image used by sandbox container.")
fs.BoolVar(&c.PrintDefaultConfig, "default-config",
false, "Print default toml config of cri-containerd and quit.")
} }
// InitFlags must be called after adding all cli options flags are defined and // InitFlags must be called after adding all cli options flags are defined and
@ -121,14 +126,12 @@ func (c *CRIContainerdOptions) InitFlags(fs *pflag.FlagSet) error {
fs.AddGoFlagSet(flag.CommandLine) fs.AddGoFlagSet(flag.CommandLine)
commandline := os.Args[1:] commandline := os.Args[1:]
err := fs.Parse(commandline) if err := fs.Parse(commandline); err != nil {
if err != nil {
return err return err
} }
// Load default config file if none provided // Load default config file if none provided
_, err = toml.DecodeFile(c.ConfigFilePath, &c.Config) if _, err := toml.DecodeFile(c.ConfigFilePath, &c.Config); err != nil {
if err != nil {
// the absence of default config file is normal case. // the absence of default config file is normal case.
if !fs.Changed(configFilePathArgName) && os.IsNotExist(err) { if !fs.Changed(configFilePathArgName) && os.IsNotExist(err) {
return nil return nil
@ -146,3 +149,15 @@ func (c *CRIContainerdOptions) InitFlags(fs *pflag.FlagSet) error {
// So I trigger twice parses, one for default value, one for commandline value. // So I trigger twice parses, one for default value, one for commandline value.
return fs.Parse(commandline) return fs.Parse(commandline)
} }
// PrintDefaultTomlConfig print default toml config of cri-containerd.
func (c *CRIContainerdOptions) PrintDefaultTomlConfig() {
fs := pflag.NewFlagSet("default-config", pflag.ExitOnError)
c.AddFlags(fs)
if err := toml.NewEncoder(os.Stdout).Encode(c.Config); err != nil {
fmt.Println(err)
return
}
}