package main import ( "encoding/json" "fmt" "os" ) type Config struct { MappingFilePath string MappingRules ImportRulesMappings } type ImportRulesMappings struct { Mappings []struct { Protocol string `json:"protocol"` VanityUrl string `json:"vanity_url"` RealUrl string `json:"real_url"` } `json:"mappings"` } type ImportRuleStruct struct { VanityUrl string Proto string RepoUrl string } // isFile - check if fp is a valid file func isFile(fp string) bool { info, err := os.Stat(fp) if os.IsNotExist(err) || !info.Mode().IsRegular() { return false } return true } // load mapping file func (c *Config) LoadMappingFile(fp string) error { var mapping ImportRulesMappings mappingFilePath := fp if len(c.MappingFilePath) == 0 { ok := isFile(mappingFilePath) if !ok { return fmt.Errorf("%s is not found", mappingFilePath) } } mappingFile, err := os.Open(mappingFilePath) if err != nil { return err } defer mappingFile.Close() err = json.NewDecoder(mappingFile).Decode(&mapping) if err != nil { return err } c.MappingRules = mapping if len(c.MappingFilePath) == 0 { c.MappingFilePath = mappingFilePath } return nil }