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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,20 @@ The `run` command has a `-v` option for volume mounts. See `Volume flags` under

Finch has a simple and extensible configuration. A configuration file at `${HOME}/.finch/finch.yaml` will be generated on first run. Currently, this config file has options for system resource limits for the underlying virtual machine. These default limits are generated dynamically based on the resources available on the host system, but can be changed by manually editing the config file.

Currently, the options are:

* 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).

An example `finch.yaml` looks like this:

```yaml
# CPUs: the amount of vCPU to dedicate to the virtual machine. (required)
cpus: 4
# Memory: the amount of memory to dedicate to the virtual machine. (required)
memory: 4GiB
# AdditionalDirectories: 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. (optional)
additional_directories:
# the path of each additional directory.
- path: /Volumes
```

Expand Down
5 changes: 2 additions & 3 deletions e2e/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"

"github.com/lima-vm/lima/pkg/limayaml"
"github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -109,7 +108,7 @@ var testConfig = func(o *option.Option, installed bool) {
var limaCfg limayaml.LimaYAML
err = yaml.Unmarshal(cfgBuf, &limaCfg)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(reflect.ValueOf(limaCfg).FieldByName("CPUs").IsValid()).Should(gomega.BeTrue())
gomega.Expect(limaCfg.CPUs != nil).Should(gomega.BeTrue())
davidhsingyuchen marked this conversation as resolved.
Show resolved Hide resolved
gomega.Expect(*limaCfg.Memory).Should(gomega.Equal("6GiB"))
})

Expand All @@ -124,7 +123,7 @@ var testConfig = func(o *option.Option, installed bool) {
var limaCfg limayaml.LimaYAML
err = yaml.Unmarshal(cfgBuf, &limaCfg)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(reflect.ValueOf(limaCfg).FieldByName("CPUs").IsValid()).Should(gomega.BeTrue())
gomega.Expect(limaCfg.CPUs != nil).Should(gomega.BeTrue())
gomega.Expect(*limaCfg.Memory).Should(gomega.MatchRegexp(`\dGiB`))
})

Expand Down
7 changes: 5 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ type AdditionalDirectory struct {

// 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 are 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.
AdditionalDirectories []AdditionalDirectory `yaml:"additional_directories,omitempty"`
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/config/lima_config_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"github.com/xorcare/pointer"
Expand Down Expand Up @@ -44,12 +45,11 @@ func TestDiskLimaConfigApplier_Apply(t *testing.T) {
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: []
cpus: 4
memory: 2GiB
`
require.Equal(t, wantYaml, string(buf))
var limaCfg limayaml.LimaYAML
err = yaml.Unmarshal(buf, &limaCfg)
require.NoError(t, err)
require.Equal(t, 4, *limaCfg.CPUs)
require.Equal(t, "2GiB", *limaCfg.Memory)
},
want: nil,
},
Expand Down Expand Up @@ -110,6 +110,14 @@ mounts:
writable: true
`
require.Equal(t, wantYaml, string(buf))
var limaCfg limayaml.LimaYAML
err = yaml.Unmarshal(buf, &limaCfg)
require.NoError(t, err)
require.Equal(t, 4, *limaCfg.CPUs)
require.Equal(t, "2GiB", *limaCfg.Memory)
require.Equal(t, 1, len(limaCfg.Mounts))
require.Equal(t, "/Volumes", limaCfg.Mounts[0].Location)
require.Equal(t, true, *limaCfg.Mounts[0].Writable)
},
want: nil,
},
Expand Down