@@ -7,6 +7,8 @@ package bootloose
77import (
88 "fmt"
99 "os"
10+ "path/filepath"
11+ "strings"
1012
1113 "github.com/k0sproject/bootloose/pkg/cluster"
1214 "github.com/k0sproject/bootloose/pkg/config"
@@ -17,6 +19,7 @@ import (
1719type configCreateOptions struct {
1820 override bool
1921 config config.Config
22+ volumes []string
2023}
2124
2225func NewConfigCreateCommand () * cobra.Command {
@@ -50,6 +53,8 @@ func NewConfigCreateCommand() *cobra.Command {
5053 containerCmd := & opts .config .Machines [0 ].Spec .Cmd
5154 cmd .Flags ().StringVarP (containerCmd , "cmd" , "d" , * containerCmd , "The command to execute on the container" )
5255
56+ cmd .Flags ().StringSliceVarP (& opts .volumes , "volume" , "v" , nil , "Volumes to mount in the container" )
57+
5358 return cmd
5459}
5560
@@ -72,6 +77,45 @@ func (opts *configCreateOptions) create(cmd *cobra.Command, args []string) error
7277 if configExists (cfgFile ) && ! opts .override {
7378 return fmt .Errorf ("configuration file at %s already exists" , cfgFile )
7479 }
80+ for _ , v := range opts .volumes {
81+ volume , err := parseVolume (v )
82+ if err != nil {
83+ return err
84+ }
85+ for _ , machine := range opts .config .Machines {
86+ machine .Spec .Volumes = append (machine .Spec .Volumes , volume )
87+ }
88+ }
7589 return cluster .Save (cfgFile )
7690}
7791
92+ // volume flags can be in the form of:
93+ // -v /host/path:/container/path (bind mount)
94+ // -v volume:/container/path (volume mount)
95+ // or contain the permissions field:
96+ // -v /host/path:/container/path:ro (bind mount (read only))
97+ // -v volume:/container/path:rw (volume mount (read write))
98+ func parseVolume (v string ) (config.Volume , error ) {
99+ if v == "" {
100+ return config.Volume {}, fmt .Errorf ("empty volume value" )
101+ }
102+ parts := strings .Split (v , ":" )
103+ if len (parts ) < 2 || len (parts ) > 3 {
104+ return config.Volume {}, fmt .Errorf ("invalid volume value: %v" , v )
105+ }
106+
107+ vol := config.Volume {}
108+ if filepath .IsAbs (parts [0 ]) {
109+ vol .Type = "bind"
110+ } else {
111+ vol .Type = "volume"
112+ }
113+
114+ if len (parts ) == 3 {
115+ vol .ReadOnly = parts [2 ] == "ro"
116+ }
117+
118+ vol .Source = parts [0 ]
119+ vol .Destination = parts [1 ]
120+ return vol , nil
121+ }
0 commit comments