2024-05-03 22:58:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-05-06 07:17:11 +00:00
|
|
|
type Config struct {
|
|
|
|
MappingFilePath string
|
|
|
|
MappingRules ImportRulesMappings
|
|
|
|
}
|
2024-05-03 22:58:49 +00:00
|
|
|
|
2024-05-06 07:17:11 +00:00
|
|
|
type ImportRulesMappings struct {
|
2024-05-03 22:58:49 +00:00
|
|
|
Mappings []struct {
|
|
|
|
Protocol string `json:"protocol"`
|
|
|
|
VanityUrl string `json:"vanity_url"`
|
|
|
|
RealUrl string `json:"real_url"`
|
|
|
|
} `json:"mappings"`
|
|
|
|
}
|
|
|
|
|
2024-05-06 07:17:11 +00:00
|
|
|
type ImportRuleStruct struct {
|
|
|
|
VanityUrl string
|
|
|
|
Proto string
|
|
|
|
RepoUrl string
|
|
|
|
}
|
|
|
|
|
2024-05-03 22:58:49 +00:00
|
|
|
// 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
|
2024-05-06 07:17:11 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-05-03 22:58:49 +00:00
|
|
|
}
|
2024-05-06 07:17:11 +00:00
|
|
|
mappingFile, err := os.Open(mappingFilePath)
|
2024-05-03 22:58:49 +00:00
|
|
|
if err != nil {
|
2024-05-06 07:17:11 +00:00
|
|
|
return err
|
2024-05-03 22:58:49 +00:00
|
|
|
}
|
2024-05-06 07:17:11 +00:00
|
|
|
defer mappingFile.Close()
|
2024-05-03 22:58:49 +00:00
|
|
|
|
2024-05-06 07:17:11 +00:00
|
|
|
err = json.NewDecoder(mappingFile).Decode(&mapping)
|
2024-05-03 22:58:49 +00:00
|
|
|
if err != nil {
|
2024-05-06 07:17:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.MappingRules = mapping
|
|
|
|
if len(c.MappingFilePath) == 0 {
|
|
|
|
c.MappingFilePath = mappingFilePath
|
2024-05-03 22:58:49 +00:00
|
|
|
}
|
2024-05-06 07:17:11 +00:00
|
|
|
return nil
|
2024-05-03 22:58:49 +00:00
|
|
|
}
|