Skip to content

Commit

Permalink
Use new API for mounting files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszo committed Dec 20, 2024
1 parent 7a523f6 commit 04a006b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
46 changes: 23 additions & 23 deletions pkg/koyeb/flags_list/file_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import (
kerrors "github.com/koyeb/koyeb-cli/pkg/koyeb/errors"
)

type FlagFileMount struct {
type FlagFile struct {
BaseFlag
path string
permissions string
content string
}

func GetNewFileMountListFromFlags() func(values []string) ([]Flag[koyeb.FileMount], error) {
return func(values []string) ([]Flag[koyeb.FileMount], error) {
ret := make([]Flag[koyeb.FileMount], 0, len(values))
func GetNewFilestListFromFlags() func(values []string) ([]Flag[koyeb.File], error) {

Check failure on line 20 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File

Check failure on line 20 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File
return func(values []string) ([]Flag[koyeb.File], error) {

Check failure on line 21 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File
ret := make([]Flag[koyeb.File], 0, len(values))

Check failure on line 22 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File

for _, value := range values {
hc := &FlagFileMount{BaseFlag: BaseFlag{cliValue: value}}
hc := &FlagFile{BaseFlag: BaseFlag{cliValue: value}}
components := strings.Split(value, ":")

if strings.HasPrefix(components[0], "!") {
if len(components) > 1 {
return nil, &kerrors.CLIError{
What: "Error while configuring the service",
Why: fmt.Sprintf("unable to parse the file mount\"%s\"", hc.cliValue),
Why: fmt.Sprintf("unable to parse the file flag value \"%s\"", hc.cliValue),
Additional: []string{
"To remove a mounted file from the service, prefix the path with '!', e.g. '!path'",
"The source should not be specified to remove it from the service",
},
Orig: nil,
Solution: "Fix the file mount and try again",
Solution: "Fix the file flag value and try again",
}
}
hc.markedForDeletion = true
Expand All @@ -44,40 +44,40 @@ func GetNewFileMountListFromFlags() func(values []string) ([]Flag[koyeb.FileMoun
if len(components) != 2 && len(components) != 3 {
return nil, &kerrors.CLIError{
What: "Error while configuring the service",
Why: fmt.Sprintf("unable to parse the file mount\"%s\"", hc.cliValue),
Why: fmt.Sprintf("unable to parse the file flag value \"%s\"", hc.cliValue),
Additional: []string{
"File mount must be specified as SOURCE:PATH[:PERMISSIONS]",
"File flag value must be specified as SOURCE:PATH[:PERMISSIONS]",
"To remove a mounted file from the service, prefix the path with '!', e.g. '!path'",
},
Orig: nil,
Solution: "Fix the file mount and try again",
Solution: "Fix the file flag value and try again",
}
}
hc.path = components[1]
path := components[0]
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return nil, &kerrors.CLIError{
What: "Error while configuring the service",
Why: fmt.Sprintf("unable to parse the file mount \"%s\"", hc.cliValue),
Why: fmt.Sprintf("unable to parse the file flag value \"%s\"", hc.cliValue),
Additional: []string{
"File mount must be specified as SOURCE:PATH[:PERMISSIONS]",
"To remove a file mount from the service, prefix it with '!', e.g. '!path'",
"File flag value must be specified as SOURCE:PATH[:PERMISSIONS]",
"To remove a mounted file from the service, prefix the path with '!', e.g. '!path'",
},
Orig: nil,
Solution: "Fix the file mount and try again",
Solution: "Fix the file flag value and try again",
}
}
data, err := os.ReadFile(path)
if err != nil {
return nil, &kerrors.CLIError{
What: "Error while configuring the service",
Why: fmt.Sprintf("unable to read the file mount\"%s\"", hc.cliValue),
Why: fmt.Sprintf("unable to read the file flag value \"%s\"", hc.cliValue),
Additional: []string{
"File mount must be specified as SOURCE:PATH[:PERMISSIONS]",
"File flag value must be specified as SOURCE:PATH[:PERMISSIONS]",
"To remove a file mount from the service, prefix it with '!', e.g. '!path'",
},
Orig: nil,
Solution: "Fix the file mount and try again",
Solution: "Fix the file flag value and try again",
}
}
hc.content = string(data)
Expand All @@ -89,13 +89,13 @@ func GetNewFileMountListFromFlags() func(values []string) ([]Flag[koyeb.FileMoun
if len(permissions) != 4 {
return nil, &kerrors.CLIError{
What: "Error while configuring the service",
Why: fmt.Sprintf("unable to parse the file mount\"%s\"", hc.cliValue),
Why: fmt.Sprintf("unable to parse the file flag value \"%s\"", hc.cliValue),
Additional: []string{
"File mount permission must be specified as SOURCE:PATH:PERMISSIONS",
"To remove a file mount from the service, prefix it with '!', e.g. '!path'",
},
Orig: nil,
Solution: "Fix the permissions in file mount and try again",
Solution: "Fix the permissions in file flag value and try again",
}
}
hc.permissions = permissions
Expand All @@ -106,18 +106,18 @@ func GetNewFileMountListFromFlags() func(values []string) ([]Flag[koyeb.FileMoun
}
}

func (f *FlagFileMount) IsEqualTo(hc koyeb.FileMount) bool {
func (f *FlagFile) IsEqualTo(hc koyeb.File) bool {

Check failure on line 109 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File
return hc.GetPath() == f.path
}

func (f *FlagFileMount) UpdateItem(hc *koyeb.FileMount) {
func (f *FlagFile) UpdateItem(hc *koyeb.File) {

Check failure on line 113 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File
hc.Content = &f.content
hc.Path = &f.path
hc.Permissions = &f.permissions
}

func (f *FlagFileMount) CreateNewItem() *koyeb.FileMount {
item := koyeb.NewFileMountWithDefaults()
func (f *FlagFile) CreateNewItem() *koyeb.File{

Check failure on line 119 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.File
item := koyeb.NewFileWithDefaults()

Check failure on line 120 in pkg/koyeb/flags_list/file_mounts.go

View workflow job for this annotation

GitHub Actions / check

undefined: koyeb.NewFileWithDefaults (typecheck)
f.UpdateItem(item)
return item
}
16 changes: 8 additions & 8 deletions pkg/koyeb/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ func (h *ServiceHandler) addServiceDefinitionFlagsForAllSources(flags *pflag.Fla
"To delete a volume, use !VOLUME, for example --volume '!myvolume'\n",
)
flags.StringSlice(
"mount-config-file",
"file",
nil,
"Mount a copy of local file as a file in the service container using the format LOCAL_FILE:PATH:[PERMISSIONS] for example --mount-file /etc/data.yaml:/etc/data.yaml:0644\n"+
"To delete a file mount, use !PATH, for example --mount-file !/etc/data.yaml\n",
"Mount a copy of local file as a file in the service container using the format LOCAL_FILE:PATH:[PERMISSIONS] for example --file /etc/data.yaml:/etc/data.yaml:0644\n"+
"To delete a file mount, use !PATH, for example --file !/etc/data.yaml\n",
)

// Configure aliases: for example, allow user to use --port instead of --ports
Expand Down Expand Up @@ -556,11 +556,11 @@ func (h *ServiceHandler) parseServiceDefinitionFlags(ctx *CLIContext, flags *pfl
}
definition.SetVolumes(volumes)

fileMounts, err := h.parseFileMounts(ctx, flags, definition.FileMounts)
files, err := h.parseFiles(ctx, flags, definition.Files)
if err != nil {
return err
}
definition.SetFileMounts(fileMounts)
definition.SetFiles(files)

return nil
}
Expand Down Expand Up @@ -1754,9 +1754,9 @@ func (h *ServiceHandler) parseVolumes(ctx *CLIContext, flags *pflag.FlagSet, cur
return parseListFlags("volumes", flags_list.GetNewVolumeListFromFlags(wrappedResolveVolumeId), flags, currentVolumes)
}

// Parse --mount-config-file
func (h *ServiceHandler) parseFileMounts(ctx *CLIContext, flags *pflag.FlagSet, currentFileMounts []koyeb.FileMount) ([]koyeb.FileMount, error) {
return parseListFlags("mount-config-file", flags_list.GetNewFileMountListFromFlags(), flags, currentFileMounts)
// Parse --file
func (h *ServiceHandler) parseFiles(ctx *CLIContext, flags *pflag.FlagSet, currentFiles []koyeb.File) ([]koyeb.File, error) {
return parseListFlags("file", flags_list.GetNewFilestListFromFlags(), flags, currentFiles)
}

// DeploymentStrategy is a type alias for koyeb.DeploymentStrategyType which implements the pflag.Value interface.
Expand Down

0 comments on commit 04a006b

Please sign in to comment.