Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config to support additional directories #128

Merged
merged 12 commits into from
Dec 26, 2022
Merged
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ Finch has a simple and extensible configuration. A configuration file at `${HOME

Currently, the options are:

* CPUs [int]: the amount of vCPU to dedicate to the virtual machine
* Memory [string]: the amount of memory to dedicate to the virtual machine
* CPUs [int] (required): the amount of vCPU to dedicate to the virtual machine
* Memory [string] (required): the amount of memory to dedicate to the virtual machine
* AdditionalDirectories [AdditionalDirectory{path [string]}] (optional): the work directories that are not supported by default. In macOS, only home directory is supported by default. For example, if you want to mount a directory into a container, and that directory is not under your home directory, then you'll need to specify this field to add that directory or any ascendant of it as a work directory.

For a full list of configuration options, check [the struct here](pkg/config/config.go#L25).

Expand All @@ -106,6 +107,8 @@ An example `finch.yaml` looks like this:
```yaml
cpus: 4
memory: 4GiB
additional_directories:
- path: /Volumes
```

## What's next?
Expand Down
26 changes: 26 additions & 0 deletions e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
"os/exec"
"path/filepath"

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/option"
"github.com/xorcare/pointer"
"gopkg.in/yaml.v3"
)

var finchConfigFilePath = os.Getenv("HOME") + "/.finch/finch.yaml"
Expand Down Expand Up @@ -124,5 +127,28 @@ var testConfig = func(o *option.Option, installed bool) {
startCmdSession := updateAndApplyConfig(o, []byte("memory: 0GiB"))
gomega.Expect(startCmdSession).Should(gexec.Exit(1))
})

ginkgo.It("updates config values when a config file is present with additional directories", func() {
mharwani marked this conversation as resolved.
Show resolved Hide resolved
startCmdSession := updateAndApplyConfig(o, []byte(`memory: 4GiB
cpus: 6
additional_directories:
- path: /Volumes
- path: /tmp/workspace`))
gomega.Expect(startCmdSession).Should(gexec.Exit(0))

gomega.Expect(limaConfigFilePath).Should(gomega.BeARegularFile())
cfgBuf, err := os.ReadFile(filepath.Clean(limaConfigFilePath))
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
var limaCfg limayaml.LimaYAML
davidhsingyuchen marked this conversation as resolved.
Show resolved Hide resolved
err = yaml.Unmarshal(cfgBuf, &limaCfg)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(limaCfg.CPUs).Should(gomega.Equal(pointer.Int(6)))
gomega.Expect(limaCfg.Memory).Should(gomega.Equal(pointer.String("4GiB")))
gomega.Expect(len(limaCfg.Mounts)).Should(gomega.Equal(2))
gomega.Expect(limaCfg.Mounts[0].Location).Should(gomega.Equal("/Volumes"))
gomega.Expect(limaCfg.Mounts[0].Writable).Should(gomega.Equal(pointer.Bool(true)))
gomega.Expect(limaCfg.Mounts[1].Location).Should(gomega.Equal("/tmp/workspace"))
gomega.Expect(limaCfg.Mounts[1].Writable).Should(gomega.Equal(pointer.Bool(true)))
})
})
}
10 changes: 8 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ import (
"github.com/runfinch/finch/pkg/system"
)

// AdditionalDirectory represents the additional directory used in Finch config.
davidhsingyuchen marked this conversation as resolved.
Show resolved Hide resolved
type AdditionalDirectory struct {
Path *string `yaml:"path"`
}

// Finch represents the configuration file for Finch CLI.
type Finch struct {
CPUs *int `yaml:"cpus"`
Memory *string `yaml:"memory"`
CPUs *int `yaml:"cpus"`
Memory *string `yaml:"memory"`
AdditionalDirectories []AdditionalDirectory `yaml:"additional_directories,omitempty"`
}

// Nerdctl is a copy from github.com/containerd/nerdctl/cmd/nerdctl/main.go
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/lima_config_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/spf13/afero"
"github.com/xorcare/pointer"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -43,6 +44,12 @@ func (lca *limaConfigApplier) Apply() error {

limaCfg.CPUs = lca.cfg.CPUs
limaCfg.Memory = lca.cfg.Memory
limaCfg.Mounts = []limayaml.Mount{}
davidhsingyuchen marked this conversation as resolved.
Show resolved Hide resolved
for _, ad := range lca.cfg.AdditionalDirectories {
ningziwen marked this conversation as resolved.
Show resolved Hide resolved
limaCfg.Mounts = append(limaCfg.Mounts, limayaml.Mount{
Location: *ad.Path, Writable: pointer.Bool(true),
})
}

limaCfgBytes, err := yaml.Marshal(limaCfg)
if err != nil {
Expand Down
34 changes: 33 additions & 1 deletion pkg/config/lima_config_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func TestDiskLimaConfigApplier_Apply(t *testing.T) {
require.NoError(t, err)

// limayaml.LimaYAML has a required "images" field which will also get marshaled
require.Equal(t, buf, []byte("images: []\ncpus: 4\nmemory: 2GiB\n"))
wantYaml := `images: []
cpus: 4
memory: 2GiB
`
require.Equal(t, wantYaml, string(buf))
},
want: nil,
},
Expand Down Expand Up @@ -81,6 +85,34 @@ func TestDiskLimaConfigApplier_Apply(t *testing.T) {
&yaml.TypeError{Errors: []string{"line 1: cannot unmarshal !!str `this is...` into limayaml.LimaYAML"}},
),
},
{
name: "lima config file with additional directories",
config: &Finch{
Memory: pointer.String("2GiB"),
CPUs: pointer.Int(4),
AdditionalDirectories: []AdditionalDirectory{{pointer.String("/Volumes")}},
},
path: "/lima.yaml",
mockSvc: func(fs afero.Fs, l *mocks.Logger) {
err := afero.WriteFile(fs, "/lima.yaml", []byte("memory: 4GiB\ncpus: 8"), 0o600)
require.NoError(t, err)
},
postRunCheck: func(t *testing.T, fs afero.Fs) {
buf, err := afero.ReadFile(fs, "/lima.yaml")
require.NoError(t, err)

// limayaml.LimaYAML has a required "images" field which will also get marshaled
wantYaml := `images: []
davidhsingyuchen marked this conversation as resolved.
Show resolved Hide resolved
cpus: 4
memory: 2GiB
mounts:
- location: /Volumes
writable: true
`
require.Equal(t, wantYaml, string(buf))
},
want: nil,
},
}

for _, tc := range testCases {
Expand Down