Skip to content

Commit

Permalink
refactor: move GetRelease to setup package
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornplusplus committed Dec 4, 2023
1 parent 03f077a commit 67d2f7d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 70 deletions.
67 changes: 1 addition & 66 deletions cmd/chisel/cmd_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package main
import (
"github.com/jessevdk/go-flags"

"fmt"
"os"
"regexp"
"strings"

"github.com/canonical/chisel/internal/archive"
"github.com/canonical/chisel/internal/cache"
"github.com/canonical/chisel/internal/setup"
Expand Down Expand Up @@ -54,7 +49,7 @@ func (cmd *cmdCut) Execute(args []string) error {
sliceKeys[i] = sliceKey
}

release, _, err := getRelease(cmd.Release)
release, _, err := setup.GetRelease(cmd.Release)
if err != nil {
return err
}
Expand Down Expand Up @@ -86,63 +81,3 @@ func (cmd *cmdCut) Execute(args []string) error {
TargetDir: cmd.RootDir,
})
}

// TODO These need testing, and maybe moving into a common file.

var releaseExp = regexp.MustCompile(`^([a-z](?:-?[a-z0-9]){2,})-([0-9]+(?:\.?[0-9])+)$`)

func parseReleaseInfo(release string) (label, version string, err error) {
match := releaseExp.FindStringSubmatch(release)
if match == nil {
return "", "", fmt.Errorf("invalid release reference: %q", release)
}
return match[1], match[2], nil
}

func readReleaseInfo() (label, version string, err error) {
data, err := os.ReadFile("/etc/lsb-release")
if err == nil {
const labelPrefix = "DISTRIB_ID="
const versionPrefix = "DISTRIB_RELEASE="
for _, line := range strings.Split(string(data), "\n") {
switch {
case strings.HasPrefix(line, labelPrefix):
label = strings.ToLower(line[len(labelPrefix):])
case strings.HasPrefix(line, versionPrefix):
version = line[len(versionPrefix):]
}
if label != "" && version != "" {
return label, version, nil
}
}
}
return "", "", fmt.Errorf("cannot infer release via /etc/lsb-release, see the --release option")
}

// getRelease returns the release and release label (e.g. ubuntu-22.04 or
// /path/to/release/dir/ if a directory was passed as input).
func getRelease(releaseStr string) (release *setup.Release, releaseLabel string, err error) {
if strings.Contains(releaseStr, "/") {
release, err = setup.ReadRelease(releaseStr)
releaseLabel = releaseStr
} else {
var label, version string
if releaseStr == "" {
label, version, err = readReleaseInfo()
} else {
label, version, err = parseReleaseInfo(releaseStr)
}
if err != nil {
return nil, "", err
}
release, err = setup.FetchRelease(&setup.FetchOptions{
Label: label,
Version: version,
})
releaseLabel = label + "-" + version
}
if err != nil {
return nil, "", err
}
return release, releaseLabel, nil
}
5 changes: 1 addition & 4 deletions cmd/chisel/cmd_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (cmd *cmdFind) Execute(args []string) error {
return fmt.Errorf("no search term specified")
}

release, releaseLabel, err := getRelease(cmd.Release)
release, releaseLabel, err := setup.GetRelease(cmd.Release)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,9 +91,6 @@ func fuzzyMatchSlice(slice *setup.Slice, query string) bool {
// findSlices goes through the release searching for any slices that match
// the query string. It returns a list of slices that match the query.
func findSlices(release *setup.Release, query string) (slices []*setup.Slice, err error) {
if release == nil {
return nil, fmt.Errorf("cannot find slice: invalid release")
}
for _, pkg := range release.Packages {
for _, slice := range pkg.Slices {
if slice != nil && fuzzyMatchSlice(slice, query) {
Expand Down
60 changes: 60 additions & 0 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,63 @@ func Select(release *Release, slices []SliceKey) (*Selection, error) {

return selection, nil
}

// TODO These need testing

var releaseExp = regexp.MustCompile(`^([a-z](?:-?[a-z0-9]){2,})-([0-9]+(?:\.?[0-9])+)$`)

func parseReleaseInfo(release string) (label, version string, err error) {
match := releaseExp.FindStringSubmatch(release)
if match == nil {
return "", "", fmt.Errorf("invalid release reference: %q", release)
}
return match[1], match[2], nil
}

func readReleaseInfo() (label, version string, err error) {
data, err := os.ReadFile("/etc/lsb-release")
if err == nil {
const labelPrefix = "DISTRIB_ID="
const versionPrefix = "DISTRIB_RELEASE="
for _, line := range strings.Split(string(data), "\n") {
switch {
case strings.HasPrefix(line, labelPrefix):
label = strings.ToLower(line[len(labelPrefix):])
case strings.HasPrefix(line, versionPrefix):
version = line[len(versionPrefix):]
}
if label != "" && version != "" {
return label, version, nil
}
}
}
return "", "", fmt.Errorf("cannot infer release via /etc/lsb-release, see the --release option")
}

// GetRelease returns the release and release label (e.g. ubuntu-22.04 or
// /path/to/release/dir/ if a directory was passed as input).
func GetRelease(releaseStr string) (release *Release, releaseLabel string, err error) {
if strings.Contains(releaseStr, "/") {
release, err = ReadRelease(releaseStr)
releaseLabel = releaseStr
} else {
var label, version string
if releaseStr == "" {
label, version, err = readReleaseInfo()
} else {
label, version, err = parseReleaseInfo(releaseStr)
}
if err != nil {
return nil, "", err
}
release, err = FetchRelease(&FetchOptions{
Label: label,
Version: version,
})
releaseLabel = label + "-" + version
}
if err != nil {
return nil, "", err
}
return release, releaseLabel, nil
}

0 comments on commit 67d2f7d

Please sign in to comment.