Move the code for Ubuntu support into its own file

This will reduce the size of the src/pkg/utils/utils.go file and make it
easier to specify which part of the code base is maintained by whom.

https://github.com/containers/toolbox/pull/1639
This commit is contained in:
Debarshi Ray 2025-05-06 02:37:05 +02:00
parent c794e183da
commit 7a413e66b9
3 changed files with 83 additions and 58 deletions

1
.github/CODEOWNERS vendored
View File

@ -8,3 +8,4 @@
/images/rhel @debarshiray @olivergs
/images/ubuntu @Jmennius
/src/pkg/utils/arch.go @Foxboron
/src/pkg/utils/ubuntu.go @Jmennius

82
src/pkg/utils/ubuntu.go Normal file
View File

@ -0,0 +1,82 @@
/*
* Copyright © 2023 2025 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"strconv"
"strings"
"unicode/utf8"
"github.com/sirupsen/logrus"
)
func getDefaultReleaseUbuntu() (string, error) {
release, err := getHostVersionID()
if err != nil {
return "", err
}
return release, nil
}
func getFullyQualifiedImageUbuntu(image, release string) string {
imageFull := "quay.io/toolbx/" + image
return imageFull
}
func parseReleaseUbuntu(release string) (string, error) {
releaseParts := strings.Split(release, ".")
if len(releaseParts) != 2 {
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}
releaseYear, err := strconv.Atoi(releaseParts[0])
if err != nil {
logrus.Debugf("Parsing release year %s as an integer failed: %s", releaseParts[0], err)
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}
if releaseYear < 4 {
return "", &ParseReleaseError{"The release year must be 4 or more."}
}
releaseYearLen := utf8.RuneCountInString(releaseParts[0])
if releaseYearLen > 2 {
return "", &ParseReleaseError{"The release year cannot have more than two digits."}
} else if releaseYear < 10 && releaseYearLen == 2 {
return "", &ParseReleaseError{"The release year cannot have a leading zero."}
}
releaseMonth, err := strconv.Atoi(releaseParts[1])
if err != nil {
logrus.Debugf("Parsing release month %s as an integer failed: %s", releaseParts[1], err)
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}
if releaseMonth < 1 {
return "", &ParseReleaseError{"The release month must be between 01 and 12."}
} else if releaseMonth > 12 {
return "", &ParseReleaseError{"The release month must be between 01 and 12."}
}
releaseMonthLen := utf8.RuneCountInString(releaseParts[1])
if releaseMonthLen != 2 {
return "", &ParseReleaseError{"The release month must have two digits."}
}
return release, nil
}

View File

@ -28,7 +28,6 @@ import (
"strings"
"syscall"
"time"
"unicode/utf8"
"github.com/acobaugh/osrelease"
"github.com/containers/toolbox/pkg/shell"
@ -328,15 +327,6 @@ func getDefaultReleaseForDistro(distro string) (string, error) {
return release, nil
}
func getDefaultReleaseUbuntu() (string, error) {
release, err := getHostVersionID()
if err != nil {
return "", err
}
return release, nil
}
func GetEnvOptionsForPreservedVariables() []string {
logrus.Debug("Creating list of environment variables to forward")
@ -397,11 +387,6 @@ func GetFullyQualifiedImageFromDistros(image, release string) (string, error) {
return "", fmt.Errorf("failed to resolve image %s", image)
}
func getFullyQualifiedImageUbuntu(image, release string) string {
imageFull := "quay.io/toolbx/" + image
return imageFull
}
// GetGroupForSudo returns the name of the sudoers group.
//
// Some distros call it 'sudo' (eg. Ubuntu) and some call it 'wheel' (eg. Fedora).
@ -712,49 +697,6 @@ func parseRelease(distro, release string) (string, error) {
return release, err
}
func parseReleaseUbuntu(release string) (string, error) {
releaseParts := strings.Split(release, ".")
if len(releaseParts) != 2 {
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}
releaseYear, err := strconv.Atoi(releaseParts[0])
if err != nil {
logrus.Debugf("Parsing release year %s as an integer failed: %s", releaseParts[0], err)
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}
if releaseYear < 4 {
return "", &ParseReleaseError{"The release year must be 4 or more."}
}
releaseYearLen := utf8.RuneCountInString(releaseParts[0])
if releaseYearLen > 2 {
return "", &ParseReleaseError{"The release year cannot have more than two digits."}
} else if releaseYear < 10 && releaseYearLen == 2 {
return "", &ParseReleaseError{"The release year cannot have a leading zero."}
}
releaseMonth, err := strconv.Atoi(releaseParts[1])
if err != nil {
logrus.Debugf("Parsing release month %s as an integer failed: %s", releaseParts[1], err)
return "", &ParseReleaseError{"The release must be in the 'YY.MM' format."}
}
if releaseMonth < 1 {
return "", &ParseReleaseError{"The release month must be between 01 and 12."}
} else if releaseMonth > 12 {
return "", &ParseReleaseError{"The release month must be between 01 and 12."}
}
releaseMonthLen := utf8.RuneCountInString(releaseParts[1])
if releaseMonthLen != 2 {
return "", &ParseReleaseError{"The release month must have two digits."}
}
return release, nil
}
// PathExists wraps around os.Stat providing a nice interface for checking an existence of a path.
func PathExists(path string) bool {
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {