Files
colima/cmd/template.go
Abiola IbrahimandGitHub c42a673589 multiple fixes pre 0.4.0 (#251)
* misc: general refactor

* docker: reduce deamon file error to warning

* core: breaking change! change folder structure

* dns: more fixes

* net: make vmnet optional

* net: limit vmnet to macOS

* net: fix flag check for non-macOS

* ci: fix go version

* ci: fix golang ci

* ci: fix lint error

* ci: increase delay for kubernetes integration test

* k8s: fix flannel route when slirp is enabled

* k3s: enable traefik

* ci: fix lint error

* k3s: fix intermitent kubeconfig issue

* misc: support config folder structure of v <0.4.0

* misc: fix regression, improve log messages

* net: replace launchagent with daemon library

* ci: fix lint error

* ci: change macos version for runner

* ci: attempt macos 12 github runner

* ci: revert macos version for github runner

* net: fix vmnet directory bug

* docs: update readme

* cli: add docker socket path to status

* net: improve vmnet process management

* chore: move embedded file

* checkpoint: config file support

* chore: use global cache directory

* core: add iso with 9p mount support

* core: add support for 9p mounts

* conf: update default config

* core: add config file support

* k3s: configurable version

* k3s: fix bug due to configuration version

* k3s: make ingress (traefik) configurable

* net: ignore network check on non-macOS at shutdown

* chore: code refactor

* ci: fix failed build

* chore: fix false negative error message

* ci: add more tests

* chore: improve error output for commands

* k3s: fix regression due to file transfer method

* cli: do not truncate outputs

* cli: improve list output

* core: add support for docker config, mount type

* cli: add --edit flag to start

* chore: use temp file for startup edit

* cli: new template command for default settings

* net: delay during vmnet process termination

* ci: fix lint error

* net: refactor vmnet daemon, remove symlink hack

* net: improve vmnet daemon shutdown

* net: add gvproxy as additional network option

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* net: fix gvproxy for multiple instances

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* net: fix regression due to gvproxy

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* net: disable network address by default

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: fix lint error

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* cli: rename network-gateway to network-driver

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: prompt before restarting on config edit

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* k8s: fix broken kubeconfig after network address toggle

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* core: update iso image

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* ci: add cross-architecture to integration test

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* docs: update readme

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: ignore already running instances for `start` command

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: handle code and siblings when specified in `colima start --edit --editor`

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* docs: update README.md

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: handle input validation for mount type

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* net: make gvproxy the default driver

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* docs: update readme

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: fix minor typo in template

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
2022-05-03 18:10:16 +01:00

95 lines
2.5 KiB
Go

package cmd
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/abiosoft/colima/cmd/root"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/config/configmanager"
"github.com/abiosoft/colima/embedded"
"github.com/spf13/cobra"
)
// templateCmd represents the template command
var templateCmd = &cobra.Command{
Use: "template",
Aliases: []string{"tmpl", "tpl", "t"},
Short: "edit the template for default configurations",
Long: `Edit the template for default configurations of new instances.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if templateCmdArgs.Print {
fmt.Println(templateFile())
return nil
}
// there are unwarranted []byte to string overheads.
// not a big deal in this case
abort, err := embedded.ReadString("defaults/abort.yaml")
if err != nil {
return fmt.Errorf("error reading embedded file: %w", err)
}
info, err := embedded.ReadString("defaults/template.yaml")
if err != nil {
return fmt.Errorf("error reading embedded file: %w", err)
}
template, err := templateFileOrDefault()
if err != nil {
return fmt.Errorf("error reading template file: %w", err)
}
tmpFile, err := waitForUserEdit(templateCmdArgs.Editor, []byte(abort+"\n"+info+"\n"+template))
if err != nil {
return fmt.Errorf("error editing template file: %w", err)
}
if tmpFile == "" {
return fmt.Errorf("empty file, template edit aborted")
}
defer func() {
_ = os.Remove(tmpFile)
}()
// load and resave template to ensure the format is correct
cf, err := configmanager.LoadFrom(tmpFile)
if err != nil {
return fmt.Errorf("error in template: %w", err)
}
if err := configmanager.SaveToFile(cf, templateFile()); err != nil {
return fmt.Errorf("error saving template: %w", err)
}
log.Println("configurations template saved")
return nil
},
}
func templateFile() string { return filepath.Join(config.TemplatesDir(), "default.yaml") }
func templateFileOrDefault() (string, error) {
tFile := templateFile()
if _, err := os.Stat(tFile); err == nil {
b, err := os.ReadFile(tFile)
if err == nil {
return string(b), nil
}
}
return embedded.ReadString("defaults/colima.yaml")
}
var templateCmdArgs struct {
Editor string
Print bool
}
func init() {
root.Cmd().AddCommand(templateCmd)
templateCmd.Flags().StringVar(&templateCmdArgs.Editor, "editor", "", `editor to use for edit e.g. vim, nano, code (default "$EDITOR" env var)`)
templateCmd.Flags().BoolVar(&templateCmdArgs.Print, "print", false, `print out the configuration file path, without editing`)
}