github.com/jfrog/jfrog-cli-core/v2@v2.52.0/common/spec/distributionrules.go (about) 1 package spec 2 3 import ( 4 "encoding/json" 5 "github.com/jfrog/jfrog-client-go/utils/distribution" 6 7 "github.com/jfrog/jfrog-client-go/utils/errorutils" 8 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 9 ) 10 11 type DistributionRules struct { 12 DistributionRules []DistributionRule `json:"distribution_rules,omitempty"` 13 } 14 15 type DistributionRule struct { 16 SiteName string `json:"site_name,omitempty"` 17 CityName string `json:"city_name,omitempty"` 18 CountryCodes []string `json:"country_codes,omitempty"` 19 } 20 21 func (distributionRules *DistributionRules) Get(index int) *DistributionRule { 22 if index < len(distributionRules.DistributionRules) { 23 return &distributionRules.DistributionRules[index] 24 } 25 return new(DistributionRule) 26 } 27 28 func (distributionRule *DistributionRule) ToDistributionCommonParams() *distribution.DistributionCommonParams { 29 return &distribution.DistributionCommonParams{ 30 SiteName: distributionRule.SiteName, 31 CityName: distributionRule.CityName, 32 CountryCodes: distributionRule.CountryCodes, 33 } 34 } 35 36 func (distributionRule *DistributionRule) IsEmpty() bool { 37 return distributionRule.SiteName == "" && distributionRule.CityName == "" && len(distributionRule.CountryCodes) == 0 38 } 39 40 func CreateDistributionRulesFromFile(distributionSpecPath string) (*DistributionRules, error) { 41 content, err := fileutils.ReadFile(distributionSpecPath) 42 if err != nil { 43 return nil, errorutils.CheckError(err) 44 } 45 distributionRules := new(DistributionRules) 46 err = json.Unmarshal(content, distributionRules) 47 if err != nil { 48 return nil, errorutils.CheckError(err) 49 } 50 return distributionRules, nil 51 }