Skip to content

Commit

Permalink
fix: remove redundant type, map and function
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornplusplus committed Aug 7, 2024
1 parent d1bafd9 commit 6479279
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 142 deletions.
53 changes: 20 additions & 33 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/canonical/chisel/internal/control"
"github.com/canonical/chisel/internal/deb"
"github.com/canonical/chisel/internal/pgputil"
"github.com/canonical/chisel/internal/setup"
)

type Archive interface {
Expand Down Expand Up @@ -43,12 +44,6 @@ func Open(options *Options) (Archive, error) {
if err != nil {
return nil, err
}
if options.Pro != "" {
err = validatePro(options.Pro)
if err != nil {
return nil, err
}
}
return openUbuntu(options)
}

Expand Down Expand Up @@ -139,41 +134,36 @@ const ubuntuURL = "http://archive.ubuntu.com/ubuntu/"
const ubuntuPortsURL = "http://ports.ubuntu.com/ubuntu-ports/"

var proArchiveInfo = map[string]struct {
baseURL, label string
BaseURL, Label string
}{
"fips": {
baseURL: "https://esm.ubuntu.com/fips/ubuntu/",
label: "UbuntuFIPS",
setup.ProFIPS: {
BaseURL: "https://esm.ubuntu.com/fips/ubuntu/",
Label: "UbuntuFIPS",
},
"fips-updates": {
baseURL: "https://esm.ubuntu.com/fips-updates/ubuntu/",
label: "UbuntuFIPSUpdates",
setup.ProFIPSUpdates: {
BaseURL: "https://esm.ubuntu.com/fips-updates/ubuntu/",
Label: "UbuntuFIPSUpdates",
},
"apps": {
baseURL: "https://esm.ubuntu.com/apps/ubuntu/",
label: "UbuntuESMApps",
setup.ProApps: {
BaseURL: "https://esm.ubuntu.com/apps/ubuntu/",
Label: "UbuntuESMApps",
},
"infra": {
baseURL: "https://esm.ubuntu.com/infra/ubuntu/",
label: "UbuntuESM",
setup.ProInfra: {
BaseURL: "https://esm.ubuntu.com/infra/ubuntu/",
Label: "UbuntuESM",
},
}

// archiveURL returns the archive base URL depending on the "pro" value and
// selected architecture "arch".
func archiveURL(pro, arch string) string {
if pro == "" {
if arch == "amd64" || arch == "i386" {
return ubuntuURL
}
return ubuntuPortsURL
}
return proArchiveInfo[pro].baseURL
}

func validatePro(pro string) error {
if _, ok := proArchiveInfo[pro]; !ok {
return fmt.Errorf("unknown pro value: %s", pro)
}
return nil
return proArchiveInfo[pro].BaseURL
}

func openUbuntu(options *Options) (Archive, error) {
Expand All @@ -193,13 +183,10 @@ func openUbuntu(options *Options) (Archive, error) {
Dir: options.CacheDir,
},
pubKeys: options.PubKeys,
baseURL: archiveURL(options.Pro, options.Arch),
}

if archive.baseURL == "" {
archive.baseURL = archiveURL(options.Pro, options.Arch)
}

if options.Pro != "" && archive.creds == nil {
if options.Pro != "" {
creds, err := findCredentials(archive.baseURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -279,7 +266,7 @@ func (index *ubuntuIndex) fetchRelease() error {
// Parse the appropriate section for the type of archive.
label := "Ubuntu"
if index.archive.options.Pro != "" {
label = proArchiveInfo[index.archive.options.Pro].label
label = proArchiveInfo[index.archive.options.Pro].Label
}
section := ctrl.Section(label)
if section == nil {
Expand Down
35 changes: 7 additions & 28 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (s *httpSuite) TestArchiveLabels(c *C) {
_, err = archive.Open(&options)
c.Assert(err, IsNil)

s.prepareArchiveAdjustRelease("jammy", "22.04", "amd64", []string{"main", "universe"}, setLabel("ThirdParty"))
s.prepareArchiveAdjustRelease("jammy", "22.04", "amd64", []string{"main", "universe"}, setLabel("Unknown"))

options = archive.Options{
Label: "ubuntu",
Expand All @@ -335,28 +335,7 @@ func (s *httpSuite) TestArchiveLabels(c *C) {
}

_, err = archive.Open(&options)
c.Assert(err, ErrorMatches, `.*\bno Ubuntu section`)
}

var proArchiveInfo = map[string]struct {
baseURL, label string
}{
"fips": {
baseURL: "https://esm.ubuntu.com/fips/ubuntu/",
label: "UbuntuFIPS",
},
"fips-updates": {
baseURL: "https://esm.ubuntu.com/fips-updates/ubuntu/",
label: "UbuntuFIPSUpdates",
},
"apps": {
baseURL: "https://esm.ubuntu.com/apps/ubuntu/",
label: "UbuntuESMApps",
},
"infra": {
baseURL: "https://esm.ubuntu.com/infra/ubuntu/",
label: "UbuntuESM",
},
c.Assert(err, ErrorMatches, `corrupted archive InRelease file: no Ubuntu section`)
}

func (s *httpSuite) TestProArchives(c *C) {
Expand All @@ -372,15 +351,15 @@ func (s *httpSuite) TestProArchives(c *C) {

confFile := filepath.Join(credsDir, "credentials")
contents := ""
for _, info := range proArchiveInfo {
contents += fmt.Sprintf("machine %s login foo password bar\n", info.baseURL)
for _, info := range archive.ProArchiveInfo {
contents += fmt.Sprintf("machine %s login foo password bar\n", info.BaseURL)
}
err := os.WriteFile(confFile, []byte(contents), 0600)
c.Assert(err, IsNil)

for pro, info := range proArchiveInfo {
s.base = info.baseURL
s.prepareArchiveAdjustRelease("jammy", "22.04", "amd64", []string{"main", "universe"}, setLabel(info.label))
for pro, info := range archive.ProArchiveInfo {
s.base = info.BaseURL
s.prepareArchiveAdjustRelease("jammy", "22.04", "amd64", []string{"main", "universe"}, setLabel(info.Label))

options := archive.Options{
Label: "ubuntu",
Expand Down
2 changes: 2 additions & 0 deletions internal/archive/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ type Credentials = credentials

var FindCredentials = findCredentials
var FindCredentialsInDir = findCredentialsInDir

var ProArchiveInfo = proArchiveInfo
22 changes: 10 additions & 12 deletions internal/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ type Release struct {
DefaultArchive string
}

const (
ProNone = ""
ProFIPS = "fips"
ProFIPSUpdates = "fips-updates"
ProApps = "apps"
ProInfra = "infra"
)

// Archive is the location from which binary packages are obtained.
type Archive struct {
Name string
Expand Down Expand Up @@ -353,16 +361,6 @@ type yamlRelease struct {
V1PubKeys map[string]yamlPubKey `yaml:"v1-public-keys"`
}

type ProValue string

const (
ProNone ProValue = ""
ProFIPS ProValue = "fips"
ProFIPSUpdates ProValue = "fips-updates"
ProApps ProValue = "apps"
ProInfra ProValue = "infra"
)

const (
MaxArchivePriority = 1000
MinArchivePriority = -1000
Expand All @@ -374,7 +372,7 @@ type yamlArchive struct {
Components []string `yaml:"components"`
Default bool `yaml:"default"`
Priority int `yaml:"priority"`
Pro ProValue `yaml:"pro"`
Pro string `yaml:"pro"`
PubKeys []string `yaml:"public-keys"`
// V1PubKeys is used for compatibility with format "chisel-v1".
V1PubKeys []string `yaml:"v1-public-keys"`
Expand Down Expand Up @@ -543,7 +541,7 @@ func parseRelease(baseDir, filePath string, data []byte) (*Release, error) {
Version: details.Version,
Suites: details.Suites,
Components: details.Components,
Pro: string(details.Pro),
Pro: details.Pro,
Priority: details.Priority,
PubKeys: archiveKeys,
}
Expand Down
69 changes: 0 additions & 69 deletions internal/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,75 +1330,6 @@ var slicerTests = []slicerTest{{
content.list("/foo-bar/")
`,
},
}, {
summary: "Pro archives",
slices: []setup.SliceKey{{"test-package", "myslice"}},
archives: map[string]*testArchive{
"ubuntu": {},
"fips": {},
"fips-updates": {},
"apps": {},
"infra": {},
},
release: map[string]string{
"chisel.yaml": `
format: chisel-v1
archives:
ubuntu:
version: 20.04
components: [main]
suites: [focal]
priority: 10
v1-public-keys: [test-key]
fips:
version: 20.04
components: [main]
suites: [focal]
pro: fips
priority: 20
v1-public-keys: [test-key]
fips-updates:
version: 20.04
components: [main]
suites: [focal-updates]
pro: fips-updates
priority: 21
v1-public-keys: [test-key]
apps:
version: 20.04
components: [main]
suites: [focal-apps-security]
pro: apps
priority: 16
v1-public-keys: [test-key]
infra:
version: 20.04
components: [main]
suites: [focal-infra-security]
pro: infra
priority: 15
v1-public-keys: [test-key]
v1-public-keys:
test-key:
id: ` + testKey.ID + `
armor: |` + "\n" + testutil.PrefixEachLine(testKey.PubKeyArmor, "\t\t\t\t\t\t") + `
`,
"slices/mydir/test-package.yaml": `
package: test-package
slices:
myslice:
contents:
/dir/nested/file:
`,
},
filesystem: map[string]string{
"/dir/": "dir 0755",
"/dir/nested/": "dir 0755",
"/dir/nested/file": "file 0644 84237a05",
},
report: map[string]string{
"/dir/nested/file": "file 0644 84237a05 {test-package_myslice}",
},
}, {
summary: "Multiple slices of same package",
slices: []setup.SliceKey{
Expand Down

0 comments on commit 6479279

Please sign in to comment.