Skip to content

Commit

Permalink
incus: Add support for environment file (.env)
Browse files Browse the repository at this point in the history
Closes #1084

Signed-off-by: Brian Ketelsen <[email protected]>
  • Loading branch information
bketelsen authored and stgraber committed Aug 8, 2024
1 parent e055564 commit afcdcec
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
35 changes: 24 additions & 11 deletions cmd/incus/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ import (
type cmdCreate struct {
global *cmdGlobal

flagConfig []string
flagDevice []string
flagEphemeral bool
flagNetwork string
flagProfile []string
flagStorage string
flagTarget string
flagType string
flagNoProfiles bool
flagEmpty bool
flagVM bool
flagConfig []string
flagDevice []string
flagEnvironmentFile string
flagEphemeral bool
flagNetwork string
flagProfile []string
flagStorage string
flagTarget string
flagType string
flagNoProfiles bool
flagEmpty bool
flagVM bool
}

func (c *cmdCreate) Command() *cobra.Command {
Expand All @@ -51,6 +52,7 @@ incus create images:ubuntu/22.04 u1 < config.yaml
cmd.Flags().StringArrayVarP(&c.flagProfile, "profile", "p", nil, i18n.G("Profile to apply to the new instance")+"``")
cmd.Flags().StringArrayVarP(&c.flagDevice, "device", "d", nil, i18n.G("New key/value to apply to a specific device")+"``")
cmd.Flags().BoolVarP(&c.flagEphemeral, "ephemeral", "e", false, i18n.G("Ephemeral instance"))
cmd.Flags().StringVar(&c.flagEnvironmentFile, "environment-file", "", i18n.G("Include environment variables from file")+"``")
cmd.Flags().StringVarP(&c.flagNetwork, "network", "n", "", i18n.G("Network name")+"``")
cmd.Flags().StringVarP(&c.flagStorage, "storage", "s", "", i18n.G("Storage pool name")+"``")
cmd.Flags().StringVarP(&c.flagType, "type", "t", "", i18n.G("Instance type")+"``")
Expand Down Expand Up @@ -223,6 +225,17 @@ func (c *cmdCreate) create(conf *config.Config, args []string, launch bool) (inc
configMap = map[string]string{}
}

if c.flagEnvironmentFile != "" {
envMap, err := readEnvironmentFile(c.flagEnvironmentFile)
if err != nil {
return nil, "", err
}

for k, v := range envMap {
configMap["environment."+k] = v
}
}

for _, entry := range c.flagConfig {
key, value, found := strings.Cut(entry, "=")
if !found {
Expand Down
30 changes: 30 additions & 0 deletions cmd/incus/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,36 @@ func getConfig(args ...string) (map[string]string, error) {
return values, nil
}

func readEnvironmentFile(path string) (map[string]string, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf(i18n.G("Can't read from environment file: %w"), err)
}

// Split the file into lines.
lines := strings.Split(string(content), "\n")

// Create a map to store the key value pairs.
envMap := make(map[string]string)

// Iterate over the lines.
for _, line := range lines {
if line == "" {
continue
}

pieces := strings.SplitN(line, "=", 2)
value := ""
if len(pieces) > 1 {
value = pieces[1]
}

envMap[pieces[0]] = value
}

return envMap, nil
}

func usage(name string, args ...string) string {
if len(args) == 0 {
return name
Expand Down

0 comments on commit afcdcec

Please sign in to comment.