Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add info command #101

Merged
merged 43 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
944cd6c
refactor: move release reading logic to a function
rebornplusplus Oct 16, 2023
58e47df
feat: add YAML marshalling logic for Package, Slice
rebornplusplus Oct 18, 2023
541e1ff
feat: add info command
rebornplusplus Oct 18, 2023
8e28abb
test(cmd/info): add tests for the info command
rebornplusplus Oct 19, 2023
b886f47
fix: change long names and address feedback from Ben
rebornplusplus Oct 20, 2023
c866c95
fix: address feedback from Cris
rebornplusplus Oct 20, 2023
5161cc2
fix: address feedback from Alberto
rebornplusplus Nov 28, 2023
5ca6ac8
test(cmd/info): compare YAMLs after parsing
rebornplusplus Nov 29, 2023
7f4f48b
refactor: move ``GetRelease`` to setup package
rebornplusplus Dec 4, 2023
afb4820
Revert "refactor: move ``GetRelease`` to setup package"
rebornplusplus Dec 8, 2023
fe2e425
refactor: rename ``getRelease`` to ``readOrFetchRelease``
rebornplusplus Dec 8, 2023
e19b440
fix: address review comments from Alberto
rebornplusplus Mar 8, 2024
0fed951
Merge 'main' into feat/cmd/info
rebornplusplus Mar 8, 2024
89e05a9
test: fix tests due to new changes in schema
rebornplusplus Mar 8, 2024
e3c2a30
test: refactor setup_test: YAML marshalling
rebornplusplus Mar 12, 2024
5bb0b70
Merge 'main' into feat/cmd/info
rebornplusplus Mar 12, 2024
eb20870
test: drop unnecessary comment
rebornplusplus Mar 12, 2024
fdecad7
chore: update spread test to latest release changes
letFunny Jul 12, 2024
cbe7c37
change slice to base-passwd_data
letFunny Jul 15, 2024
831e200
Merge 'upstream/main' into feat/cmd/info
rebornplusplus Jul 25, 2024
245e3c0
fix: contents in "flow" style
rebornplusplus Jul 26, 2024
d050ab4
fix: change marshalling logic per review
rebornplusplus Jul 29, 2024
3e5a7ed
test: re-indent stdout in cmd_info_test.go
rebornplusplus Jul 29, 2024
7c7c081
test: change test summary
rebornplusplus Jul 29, 2024
60a8c74
test: match naming style with existing slicer_test.go
rebornplusplus Jul 29, 2024
ad969f9
test(spread): add spread task for info
rebornplusplus Jul 30, 2024
6f999bd
Merge remote 'letfunny/fix-spread-tests-ca-certificates' into feat/cm…
rebornplusplus Jul 30, 2024
1524536
fix: remove redundant nil checks
rebornplusplus Jul 31, 2024
674140d
test: update spread test
rebornplusplus Jul 31, 2024
e9e23b3
test(info): re-structure cmd_info tests
rebornplusplus Aug 1, 2024
fda125b
test: fix typo on chisel bin path
rebornplusplus Aug 1, 2024
c7a8546
test: install yq as a dep in spread test
rebornplusplus Aug 1, 2024
e8639dd
test: download yq binary instead of go install
rebornplusplus Aug 1, 2024
b85eb2a
fix: address review comments
rebornplusplus Aug 6, 2024
e7cf89e
empty commit
rebornplusplus Aug 6, 2024
72ffb2d
Merge remote-tracking branch 'upstream/main'
rebornplusplus Aug 6, 2024
c3afbd4
Merge remote-tracking branch 'upstream/main'
rebornplusplus Sep 12, 2024
41a9777
fix: minor changes regarding docs and order per review
rebornplusplus Sep 19, 2024
25e11ba
fix: address review comments
rebornplusplus Sep 19, 2024
35cee63
empty commit to trigger the workflows
rebornplusplus Sep 20, 2024
3d792c1
fix: address review comments
rebornplusplus Sep 24, 2024
939cbd6
fix: minor changes requested by Alberto
rebornplusplus Sep 24, 2024
ed480dd
fix: avoid unnecessary type casting
rebornplusplus Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions cmd/chisel/cmd_cut.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,7 @@ func (cmd *cmdCut) Execute(args []string) error {
sliceKeys[i] = sliceKey
}

var release *setup.Release
var err error
if strings.Contains(cmd.Release, "/") {
release, err = setup.ReadRelease(cmd.Release)
} else {
var label, version string
if cmd.Release == "" {
label, version, err = readReleaseInfo()
} else {
label, version, err = parseReleaseInfo(cmd.Release)
}
if err != nil {
return err
}
release, err = setup.FetchRelease(&setup.FetchOptions{
Label: label,
Version: version,
})
}
release, _, err := getRelease(cmd.Release)
if err != nil {
return err
}
Expand Down Expand Up @@ -136,3 +118,31 @@ func readReleaseInfo() (label, version string, err error) {
}
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) {
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
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
}
4 changes: 4 additions & 0 deletions cmd/chisel/cmd_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ var helpCategories = []helpCategory{{
Label: "Action",
Description: "make things happen",
Commands: []string{"cut"},
}, {
Label: "Inspection",
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
Description: "inspect values",
Commands: []string{"info"},
}}

var (
Expand Down
139 changes: 139 additions & 0 deletions cmd/chisel/cmd_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"fmt"
"strconv"
"strings"

"github.com/jessevdk/go-flags"
"gopkg.in/yaml.v3"

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

var shortInfoHelp = "Show information about package slices"
var longInfoHelp = `
The info command shows detailed information about package slices.

It accepts a whitespace-separated list of strings. The list can be
composed of package names, slice names, or a combination of both. The
default output format is YAML. When multiple package or slice names
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
are provided, the output is a list of YAML documents, separated by
three dashes ("---").

Slice definitions are shown verbatim according to their definition in
the Chisel releases. For example, globs are not expanded.
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
`

var infoDescs = map[string]string{
"release": "Chisel release branch or directory",
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
}

type infoCmd struct {
Release string `long:"release" value-name:"<branch|dir>"`

Positional struct {
Queries []string `positional-arg-name:"<pkg|slice>" required:"yes"`
} `positional-args:"yes"`
}

func init() {
addCommand("info", shortInfoHelp, longInfoHelp, func() flags.Commander { return &infoCmd{} }, infoDescs, nil)
}

func (cmd *infoCmd) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}

release, _, err := getRelease(cmd.Release)
if err != nil {
return err
}

packages, notFound := selectPackageSlices(release, cmd.Positional.Queries)

for i, pkg := range packages {
data, err := yaml.Marshal(pkg)
if err != nil {
return err
}
if i > 0 {
fmt.Fprintln(Stdout, "---")
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
}
fmt.Fprint(Stdout, string(data))
}

if len(notFound) > 0 {
for i := range notFound {
notFound[i] = strconv.Quote(notFound[i])
}
return fmt.Errorf("no slice definitions found for: " + strings.Join(notFound, ", "))
}

return nil
}

// selectPackageSlices takes in a release and a list of query strings
// of package names and/or slice names, and returns a list of packages
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
// containing the found slices. It also returns a list of query
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
// strings that were not found.
func selectPackageSlices(release *setup.Release, queries []string) (packages []*setup.Package, notFound []string) {
var pkgOrder []string
pkgSlices := make(map[string][]string)
allPkgSlices := make(map[string]bool)

sliceExists := func(key setup.SliceKey) bool {
if _, ok := release.Packages[key.Package]; !ok {
return false
}
if _, ok := release.Packages[key.Package].Slices[key.Slice]; !ok {
return false
}
return true
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
}
for _, query := range queries {
var pkg, slice string
if strings.Contains(query, "_") {
key, err := setup.ParseSliceKey(query)
if err != nil || !sliceExists(key) {
notFound = append(notFound, query)
continue
}
pkg, slice = key.Package, key.Slice
} else {
if _, ok := release.Packages[query]; !ok {
notFound = append(notFound, query)
continue
}
pkg = query
}
if len(pkgSlices[pkg]) == 0 && !allPkgSlices[pkg] {
pkgOrder = append(pkgOrder, pkg)
}
if slice == "" {
allPkgSlices[pkg] = true
} else {
pkgSlices[pkg] = append(pkgSlices[pkg], slice)
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
}
}

for _, pkgName := range pkgOrder {
var pkg *setup.Package
if allPkgSlices[pkgName] {
pkg = release.Packages[pkgName]
} else {
releasePkg := release.Packages[pkgName]
pkg = &setup.Package{
Name: releasePkg.Name,
Archive: releasePkg.Archive,
Slices: make(map[string]*setup.Slice),
}
for _, sliceName := range pkgSlices[pkgName] {
pkg.Slices[sliceName] = releasePkg.Slices[sliceName]
}
}
packages = append(packages, pkg)
}
return
rebornplusplus marked this conversation as resolved.
Show resolved Hide resolved
}
Loading
Loading