From 716d8b8293b6a8cbd259e8d386f137941b24b014 Mon Sep 17 00:00:00 2001 From: Max Erenberg Date: Wed, 15 Nov 2023 23:09:07 -0500 Subject: [PATCH] remove some dead code --- pkg/cloudstack/cloudstack.go | 6 +-- pkg/distros/almalinux.go | 2 +- pkg/distros/debian.go | 2 +- pkg/distros/fedora.go | 2 +- pkg/distros/template_manager.go | 67 +-------------------------------- 5 files changed, 8 insertions(+), 71 deletions(-) diff --git a/pkg/cloudstack/cloudstack.go b/pkg/cloudstack/cloudstack.go index c1933af..e04f91f 100644 --- a/pkg/cloudstack/cloudstack.go +++ b/pkg/cloudstack/cloudstack.go @@ -229,7 +229,7 @@ func (client *CloudstackClient) getZoneId() (string, error) { data := &responseWrapper.Response checkErrorInfo(&data.ErrorInfo) if data.Count != 1 { - return "", errors.New(fmt.Sprintf("Expected 1 zone for '%s'; got %d", zoneName, data.Count)) + return "", fmt.Errorf("Expected 1 zone for '%s'; got %d", zoneName, data.Count) } zoneId := data.Zone[0].Id client.cachedZoneId = zoneId @@ -261,7 +261,7 @@ func (client *CloudstackClient) getServiceOfferingId() (string, error) { data := &responseWrapper.Response checkErrorInfo(&data.ErrorInfo) if data.Count != 1 { - return "", errors.New(fmt.Sprintf("Expected 1 service offering for '%s'; got %d", serviceOfferingName, data.Count)) + return "", fmt.Errorf("Expected 1 service offering for '%s'; got %d", serviceOfferingName, data.Count) } serviceOfferingId := data.ServiceOffering[0].Id client.cachedServiceOfferingId = serviceOfferingId @@ -294,7 +294,7 @@ func (client *CloudstackClient) GetOsTypeId(osDescription string) (string, error data := &responseWrapper.Response checkErrorInfo(&data.ErrorInfo) if data.Count != 1 { - return "", errors.New(fmt.Sprintf("Expected 1 OS type for '%s'; got %d", osDescription, data.Count)) + return "", fmt.Errorf("Expected 1 OS type for '%s'; got %d", osDescription, data.Count) } return data.OsType[0].Id, nil } diff --git a/pkg/distros/almalinux.go b/pkg/distros/almalinux.go index c4ebfd5..4aaee7f 100644 --- a/pkg/distros/almalinux.go +++ b/pkg/distros/almalinux.go @@ -79,7 +79,7 @@ func (mgr *AlmaLinuxTemplateManager) CommandToUpdatePackageCache() []string { } var almaLinuxYumRepoBaseUrlPattern *regexp.Regexp = regexp.MustCompile( - "^(?Phttps?://)[A-Za-z0-9./-]+(?P/almalinux/\\$releasever/[A-Za-z0-9./$-]+)$", + `^(?Phttps?://)[A-Za-z0-9./-]+(?P/almalinux/\$releasever/[A-Za-z0-9./$-]+)$`, ) func (mgr *AlmaLinuxTemplateManager) transformAlmaLinuxYumRepoBaseUrl(url string) string { diff --git a/pkg/distros/debian.go b/pkg/distros/debian.go index dc6f7cb..c062752 100644 --- a/pkg/distros/debian.go +++ b/pkg/distros/debian.go @@ -35,7 +35,7 @@ func NewDebianTemplateManager(cfg *config.Config) *DebianTemplateManager { func (mgr *DebianTemplateManager) GetLatestVersion() (version string, codename string, err error) { // We're only interested in the major version (integer part) - versionPattern := regexp.MustCompile("^Version: (\\d+)(?:\\.\\d)?$") + versionPattern := regexp.MustCompile(`^Version: (\d+)(?:\.\d+)?$`) codenamePattern := regexp.MustCompile("^Codename: ([a-z-]+)$") resp, err := http.Get("https://mirror.csclub.uwaterloo.ca/debian/dists/stable/InRelease") if err != nil { diff --git a/pkg/distros/fedora.go b/pkg/distros/fedora.go index 370a031..74b370d 100644 --- a/pkg/distros/fedora.go +++ b/pkg/distros/fedora.go @@ -33,7 +33,7 @@ func NewFedoraTemplateManager(cfg *config.Config) *FedoraTemplateManager { return &fedoraTemplateManager } -var numberSlashPattern *regexp.Regexp = regexp.MustCompile("^\\d+/$") +var numberSlashPattern *regexp.Regexp = regexp.MustCompile(`^\d+/$`) func isHyperlink(node *html.Node) bool { return node.Type == html.ElementNode && node.Data == "a" && diff --git a/pkg/distros/template_manager.go b/pkg/distros/template_manager.go index 42a9f44..38f19a2 100644 --- a/pkg/distros/template_manager.go +++ b/pkg/distros/template_manager.go @@ -7,9 +7,7 @@ import ( "io" "net/http" "os" - "regexp" "strings" - "text/template" "github.com/rs/zerolog" "libguestfs.org/guestfs" @@ -28,14 +26,6 @@ func getResource(filename string) []byte { return data } -func getTemplateResource(filename string) *template.Template { - tmpl, err := template.ParseFS(res, "resources/"+filename) - if err != nil { - panic(err) - } - return tmpl -} - // A TemplateManager downloads and modifies VM templates for a distro. type TemplateManager struct { cfg *config.Config @@ -184,7 +174,7 @@ func (mgr *TemplateManager) getGuestfsMountedHandle(filename string) (handle *gu return } if len(partitions) != 1 { - return nil, errors.New(fmt.Sprintf("Expected 1 root partition, found %d", len(partitions))) + return nil, fmt.Errorf("Expected 1 root partition, found %d", len(partitions)) } rootPartition := partitions[0] log.Debug().Msg(fmt.Sprintf("Mounting root filesystem %s on /", rootPartition)) @@ -215,7 +205,7 @@ func getSelinuxType(handle *guestfs.Guestfs) (selinuxType string, err error) { return } if len(lines) != 1 { - err = errors.New(fmt.Sprintf("Expected 1 line containing SELINUXTYPE, found %d", len(lines))) + err = fmt.Errorf("Expected 1 line containing SELINUXTYPE, found %d", len(lines)) return } selinuxType = strings.Split(lines[0], "=")[1] @@ -252,28 +242,6 @@ func (mgr *TemplateManager) selinuxRelabelDirectories(handle *guestfs.Guestfs) ( return } -func (mgr *TemplateManager) maskSystemdUnit(handle *guestfs.Guestfs, unit string) error { - mgr.logger.Debug().Msg("Masking systemd unit " + unit) - return handle.Ln_sf("/dev/null", "/etc/systemd/system/"+unit) -} - -func isSameFile(handle *guestfs.Guestfs, path1, path2 string) (bool, error) { - if path1 == path2 { - return true, nil - } - stat1, err := handle.Stat(path1) - if err != nil { - return false, fmt.Errorf("Could not stat %s: %w", path1, err) - } - stat2, err := handle.Stat(path2) - if err != nil { - return false, fmt.Errorf("Could not stat %s: %w", path2, err) - } - return stat1.Ino == stat2.Ino, nil -} - -var systemdDirPrefixes = []string{"/etc", "/lib"} - func (mgr *TemplateManager) logAndRunCommand(handle *guestfs.Guestfs, args []string) (string, error) { mgr.logger.Debug().Msg("Running command `" + strings.Join(args, " ") + "`") return handle.Command(args) @@ -604,37 +572,6 @@ func (mgr *TemplateManager) setDhclientOptions(handle *guestfs.Guestfs) (err err return } -func getNamedRegexGroup(re *regexp.Regexp, submatches []string, groupName string) string { - var value string - for i, subexpName := range re.SubexpNames() { - if subexpName == groupName { - value = submatches[i] - break - } - } - if value == "" { - panic("Could not find regex group " + groupName) - } - return value -} - -func getNumAugeasComments(handle *guestfs.Guestfs, parentNode string) (numComments int, err error) { - keyPaths, err := handle.Aug_ls(parentNode) - if err != nil { - return 0, fmt.Errorf("aug_ls(%s) failed: %w", parentNode, err) - } - for _, keyPath := range keyPaths { - key, err := handle.Aug_label(keyPath) - if err != nil { - return 0, fmt.Errorf("aug_label(%s) failed: %w", keyPath, err) - } - if strings.HasPrefix(key, "#comment") { - numComments += 1 - } - } - return -} - func addAugeasComment( handle *guestfs.Guestfs, numExistingComments int, parentNode string, comment string, ) error {