package internal import ( "fmt" "strings" ) type Platform string const ( LinuxArmV7 Platform = "linux/arm/v7" LinuxArmV6 Platform = "linux/arm/v6" LinuxArm64 Platform = "linux/arm64" LinuxAmd64 Platform = "linux/amd64" ) type Platforms []Platform var AllPlatforms = Platforms{LinuxArmV7, LinuxArmV6, LinuxArm64, LinuxAmd64} type PlatformsValue struct { *Platforms changed bool } var isIncluded = func(opts Platforms, val Platform) bool { for _, opt := range opts { if val == opt { return true } } return false } // Append adds the specified value to the end of the flag value list. func (platforms *PlatformsValue) Append(value string) error { if !isIncluded(AllPlatforms, Platform(value)) { return fmt.Errorf("%s is not included in %s", value, AllPlatforms.Join(", ")) } *platforms.Platforms = append(*platforms.Platforms, Platform(value)) return nil } // Replace will fully overwrite any data currently in the flag value list. func (platforms *PlatformsValue) Replace(values []string) error { *platforms.Platforms = []Platform{} for _, value := range values { if !isIncluded(AllPlatforms, Platform(value)) { return fmt.Errorf("%s is not included in %s", value, AllPlatforms.Join(", ")) } *platforms.Platforms = append(*platforms.Platforms, Platform(value)) } return nil } // GetSlice returns the flag value list as an array of strings. func (platforms *PlatformsValue) GetSlice() []string { var platformsStr []string for _, platform := range *platforms.Platforms { platformsStr = append(platformsStr, string(platform)) } return platformsStr } func (platforms PlatformsValue) String() string { return "[" + platforms.Platforms.Join(",") + "]" } func (platforms *PlatformsValue) Set(values string) error { var valuesP []Platform for _, value := range strings.Split(values, ",") { valueP := Platform(value) if !isIncluded(AllPlatforms, valueP) { return fmt.Errorf("%s is not included in %s", value, AllPlatforms.Join(", ")) } if !platforms.changed || !isIncluded(*platforms.Platforms, valueP) { valuesP = append(valuesP, valueP) } } if !platforms.changed { *platforms.Platforms = valuesP platforms.changed = true } else { *platforms.Platforms = append(*platforms.Platforms, valuesP...) } return nil } func (platforms *PlatformsValue) Type() string { return "platformSlice" } func (platforms *Platforms) Join(sep string) string { value := PlatformsValue{Platforms: platforms} return strings.Join(value.GetSlice(), sep) }