1
0
mirror of https://github.com/lxc/crio-lxc.git synced 2026-02-05 09:45:04 +01:00

cli: Load config file and add config subcommand.

Signed-off-by: Ruben Jenster <r.jenster@drachenfels.de>
This commit is contained in:
Ruben Jenster
2021-05-06 09:18:22 +02:00
parent efc2c25736
commit ba6923eb1e
3 changed files with 80 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/lxc/lxcri/pkg/specki"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli/v2"
"sigs.k8s.io/yaml"
)
var (
@@ -118,6 +119,24 @@ func (app *app) releaseLog() error {
return nil
}
func loadConfig() error {
clxc.configFile = defaultConfigFile
if val, ok := os.LookupEnv("LXCRI_CONFIG"); ok {
clxc.configFile = val
}
data, err := os.ReadFile(clxc.configFile)
// Don't fail if the default config file does not exist.
if os.IsNotExist(err) && clxc.configFile == defaultConfigFile {
return nil
}
if err != nil {
return err
}
return yaml.Unmarshal(data, &clxc)
}
func main() {
app := cli.NewApp()
app.Name = "lxcri"
@@ -137,7 +156,12 @@ func main() {
&execCmd,
&inspectCmd,
&listCmd,
// TODO extend urfave/cli to render a default environment file.
&configCmd,
}
err := loadConfig()
if err != nil {
panic(fmt.Errorf("failed to read config file: %w", err))
}
app.Flags = []cli.Flag{
@@ -815,3 +839,53 @@ func inspectContainer(id string, t *template.Template) error {
_, err = fmt.Fprint(os.Stdout, string(j))
return err
}
var configCmd = cli.Command{
Name: "config",
Usage: "Output a config file for lxcri. Global options modify the output.",
Action: doConfig,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "out",
Usage: "write config to file",
},
&cli.BoolFlag{
Name: "default",
Usage: "use the builtin default configuration",
},
&cli.BoolFlag{
Name: "update-current",
Usage: "write to the current config file (--out is ignored)",
},
&cli.BoolFlag{
Name: "quiet",
Usage: "do not print config to stdout",
},
},
}
// NOTE lxcri config > /etc/lxcri/lxcri.yaml does not work
func doConfig(ctxcli *cli.Context) (err error) {
// generate yaml
c := clxc
if ctxcli.Bool("default") {
c = defaultApp
}
data, err := yaml.Marshal(c)
if err != nil {
return err
}
if !ctxcli.Bool("quiet") {
fmt.Printf("---\n%s---\n", string(data))
}
out := ctxcli.String("out")
if ctxcli.Bool("update-current") {
out = clxc.configFile
}
if out != "" {
fmt.Printf("Writing to file %s\n", out)
return os.WriteFile(out, data, 0640)
}
return nil
}

2
go.mod
View File

@@ -3,7 +3,6 @@ module github.com/lxc/lxcri
require (
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/creack/pty v1.1.11
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/drachenfels-de/gocapability v0.0.0-20210413092208-755d79b01352
github.com/kr/pretty v0.2.1 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d
@@ -13,6 +12,7 @@ require (
golang.org/x/sys v0.0.0-20210228012217-479acdf4ea46
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/lxc/go-lxc.v2 v2.0.0-20210205143421-c4b883be4881
sigs.k8s.io/yaml v1.2.0
)
replace golang.org/x/crypto => golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad

4
go.sum
View File

@@ -51,5 +51,9 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/lxc/go-lxc.v2 v2.0.0-20210205143421-c4b883be4881 h1:YcCjv1g/OoEJ93hK3p+5MhPyuIMD9FwOYF5f4D7rNKk=
gopkg.in/lxc/go-lxc.v2 v2.0.0-20210205143421-c4b883be4881/go.mod h1:4K0lbUXeslpmjwJZyW1lI6s5j97mrsj4+kpYwwvuLXo=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=