Skip to content

Commit

Permalink
refactor: rename setup.GetRelease to readOrFetchRelease
Browse files Browse the repository at this point in the history
The function is moved back to the `main` package and is declared in
cmd/chisel/helpers.go file.
  • Loading branch information
rebornplusplus committed Dec 8, 2023
1 parent 26fa232 commit cb77d8b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cmd/chisel/cmd_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (cmd *cmdCut) Execute(args []string) error {
sliceKeys[i] = sliceKey
}

release, _, err := setup.GetRelease(cmd.Release)
release, _, err := readOrFetchRelease(cmd.Release)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion 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 := setup.GetRelease(cmd.Release)
release, releaseLabel, err := readOrFetchRelease(cmd.Release)
if err != nil {
return err
}
Expand Down
73 changes: 73 additions & 0 deletions cmd/chisel/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"os"
"regexp"
"strings"

"github.com/canonical/chisel/internal/setup"
)

// 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")
}

// readOrFetchRelease takes a release branch name or a release directory path.
// It fetches or reads the chisel-release depending on the nature of input and
// returns the release and release label.
// If the input is empty, it tries to read the release label from the host
// system and fetch the chisel-release accordingly.
func readOrFetchRelease(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
}
60 changes: 0 additions & 60 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,63 +623,3 @@ 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 cb77d8b

Please sign in to comment.