1
0
Fork 0
out-of-tree/config/out-of-tree.go

109 lines
2.0 KiB
Go
Raw Permalink Normal View History

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
2024-02-20 13:25:31 +00:00
"code.dumpstack.io/tools/out-of-tree/artifact"
"code.dumpstack.io/tools/out-of-tree/config/dotfiles"
"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"
)
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 {
2024-02-20 13:25:31 +00:00
Timeout artifact.Duration
2019-08-29 21:12:24 +00:00
}
Docker struct {
2024-02-20 13:25:31 +00:00
Timeout artifact.Duration
2019-08-29 21:12:24 +00:00
Registry string
// Commands that will be executed before
// the base layer of Dockerfile
2024-02-20 13:25:31 +00:00
Commands []distro.Command
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 != "" {
2024-02-20 13:25:31 +00:00
dotfiles.Directory = c.Directory
} else {
2024-02-20 13:25:31 +00:00
c.Directory = dotfiles.Dir("")
2019-08-29 21:12:24 +00:00
}
if c.Kernels == "" {
2024-02-20 13:25:31 +00:00
c.Kernels = dotfiles.File("kernels.toml")
2019-08-29 21:12:24 +00:00
}
if c.UserKernels == "" {
2024-02-20 13:25:31 +00:00
c.UserKernels = dotfiles.File("kernels.user.toml")
2019-08-29 21:12:24 +00:00
}
if c.Database == "" {
2024-02-20 13:25:31 +00:00
c.Database = dotfiles.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 = 8 * time.Minute
2019-08-29 21:12:24 +00:00
}
return
}