2019-08-29 21:12:24 +00:00
|
|
|
// Copyright 2019 Mikhail Klementev. All rights reserved.
|
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-01-30 20:20:51 +00:00
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"time"
|
2019-08-29 21:12:24 +00:00
|
|
|
|
2023-05-18 16:07:24 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro"
|
|
|
|
|
2023-01-30 20:20:51 +00:00
|
|
|
"github.com/alecthomas/kong"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-08-29 21:12:24 +00:00
|
|
|
"github.com/naoina/toml"
|
|
|
|
)
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
type DockerCommand struct {
|
2023-05-18 16:07:24 +00:00
|
|
|
Distro distro.Distro
|
|
|
|
Command string
|
2019-08-30 00:05:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 21:12:24 +00:00
|
|
|
type OutOfTree struct {
|
2023-05-13 15:45:21 +00:00
|
|
|
// Directory for all files if not explicitly specified
|
|
|
|
Directory string
|
|
|
|
|
2019-08-29 21:12:24 +00:00
|
|
|
Kernels string
|
|
|
|
UserKernels string
|
|
|
|
|
|
|
|
Database string
|
|
|
|
|
|
|
|
Qemu struct {
|
2023-01-30 20:20:51 +00:00
|
|
|
Timeout Duration
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Docker struct {
|
2023-01-30 20:20:51 +00:00
|
|
|
Timeout Duration
|
2019-08-29 21:12:24 +00:00
|
|
|
Registry string
|
2019-08-30 00:05:50 +00:00
|
|
|
|
|
|
|
// Commands that will be executed before
|
|
|
|
// the base layer of Dockerfile
|
|
|
|
Commands []DockerCommand
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 20:20:51 +00:00
|
|
|
func (c *OutOfTree) Decode(ctx *kong.DecodeContext) (err error) {
|
|
|
|
if ctx.Value.Set {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := homedir.Expand(ctx.Scan.Pop().String())
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultValue, err := homedir.Expand(ctx.Value.Default)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(s)
|
|
|
|
if s != defaultValue && errors.Is(err, os.ErrNotExist) {
|
|
|
|
return errors.New("'" + s + "' does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
*c, err = ReadOutOfTreeConf(s)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-29 21:12:24 +00:00
|
|
|
func ReadOutOfTreeConf(path string) (c OutOfTree, err error) {
|
|
|
|
buf, err := readFileAll(path)
|
|
|
|
if err == nil {
|
|
|
|
err = toml.Unmarshal(buf, &c)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It's ok if there's no configuration
|
|
|
|
// then we'll just set default values
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2023-05-13 15:45:21 +00:00
|
|
|
if c.Directory != "" {
|
|
|
|
Directory = c.Directory
|
2023-05-13 15:56:54 +00:00
|
|
|
} else {
|
|
|
|
c.Directory = Dir("")
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Kernels == "" {
|
2023-05-13 15:45:21 +00:00
|
|
|
c.Kernels = File("kernels.toml")
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.UserKernels == "" {
|
2023-05-13 15:55:38 +00:00
|
|
|
c.UserKernels = File("kernels.user.toml")
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Database == "" {
|
2023-05-13 15:55:38 +00:00
|
|
|
c.Database = File("db.sqlite")
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 20:20:51 +00:00
|
|
|
if c.Qemu.Timeout.Duration == 0 {
|
|
|
|
c.Qemu.Timeout.Duration = time.Minute
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 20:20:51 +00:00
|
|
|
if c.Docker.Timeout.Duration == 0 {
|
|
|
|
c.Docker.Timeout.Duration = time.Minute
|
2019-08-29 21:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|