From 68f00db202e400596d65182cb05a9308f4e839ae Mon Sep 17 00:00:00 2001 From: David Vogt Date: Fri, 17 Jun 2016 14:38:56 +0200 Subject: [PATCH 1/2] Print current source names (snapshot list & show) When renaming a snapshot that was referenced by another via snapshot merge, then the references are not updated. Consider the following: $ aptly snapshot create foo from repo xyz $ aptly snapshot merge bar foo $ aptly snapshot list List of snapshots: * [foo]: Snapshot from repo... * [bar]: Merged from sources: 'foo' $ aptly snapshot rename foo foo3 $ aptly snapshot list List of snapshots: * [foo3]: Snapshot from repo... * [bar]: Merged from sources: 'foo' As you can see, after renaming the "source" snapshot, it's stored description may refer to a snapshot that does no longer exist. This change always generates the description from the actual source snapshots, so it will point to the right (current) snapshot name, even after renames. --- cmd/snapshot_list.go | 6 +++++- cmd/snapshot_show.go | 12 +++++++++--- deb/snapshot.go | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cmd/snapshot_list.go b/cmd/snapshot_list.go index 06ba631c37..ad0c939ca6 100644 --- a/cmd/snapshot_list.go +++ b/cmd/snapshot_list.go @@ -28,7 +28,11 @@ func aptlySnapshotList(cmd *commander.Command, args []string) error { fmt.Printf("List of snapshots:\n") err = collection.ForEachSorted(sortMethodString, func(snapshot *deb.Snapshot) error { - fmt.Printf(" * %s\n", snapshot.String()) + descr, err := snapshot.DescriptionWithSources(collection) + if err != nil { + return err + } + fmt.Printf(" * [%s]: %s\n", snapshot.Name, descr) return nil }) diff --git a/cmd/snapshot_show.go b/cmd/snapshot_show.go index 6e275e0e20..421b1432d8 100644 --- a/cmd/snapshot_show.go +++ b/cmd/snapshot_show.go @@ -15,19 +15,25 @@ func aptlySnapshotShow(cmd *commander.Command, args []string) error { name := args[0] - snapshot, err := context.CollectionFactory().SnapshotCollection().ByName(name) + collection := context.CollectionFactory().SnapshotCollection() + + snapshot, err := collection.ByName(name) if err != nil { return fmt.Errorf("unable to show: %s", err) } - err = context.CollectionFactory().SnapshotCollection().LoadComplete(snapshot) + err = collection.LoadComplete(snapshot) + if err != nil { + return fmt.Errorf("unable to show: %s", err) + } + descr, err := snapshot.DescriptionWithSources(collection) if err != nil { return fmt.Errorf("unable to show: %s", err) } fmt.Printf("Name: %s\n", snapshot.Name) fmt.Printf("Created At: %s\n", snapshot.CreatedAt.Format("2006-01-02 15:04:05 MST")) - fmt.Printf("Description: %s\n", snapshot.Description) + fmt.Printf("Description: %s\n", descr) fmt.Printf("Number of packages: %d\n", snapshot.NumPackages()) withPackages := context.Flags().Lookup("with-packages").Value.Get().(bool) diff --git a/deb/snapshot.go b/deb/snapshot.go index f366e00e2b..102df4fdae 100644 --- a/deb/snapshot.go +++ b/deb/snapshot.go @@ -95,6 +95,23 @@ func (s *Snapshot) String() string { return fmt.Sprintf("[%s]: %s", s.Name, s.Description) } +// String returns description of snapshot, with current source names +func (s *Snapshot) DescriptionWithSources(collection *SnapshotCollection) (string, error) { + if s.SourceKind == "snapshot" { + sourceDescription := make([]string, len(s.SourceIDs)) + for i, s := range s.SourceIDs { + source, err := collection.ByUUID(s) + if err != nil { + return "", err + } + sourceDescription[i] = fmt.Sprintf("'%s'", source.Name) + } + return fmt.Sprintf("Merged from sources: %s", strings.Join(sourceDescription, ", ")), nil + } else { + return s.Description, nil + } +} + // NumPackages returns number of packages in snapshot func (s *Snapshot) NumPackages() int { return s.packageRefs.Len() From 21b3e9bfd5701b7a5ed319377e144a59eb4dc3b6 Mon Sep 17 00:00:00 2001 From: David Vogt Date: Mon, 20 Jun 2016 09:49:14 +0200 Subject: [PATCH 2/2] Print current source names (publish list) In the previous commit, we modified "snapshot list" and "snapshot show" to always output the current names of their source snapshots. In this change, the same is also done for "publish list". Now, the snapshots listed in "aptly publish list" are guaranteed to exist, even if they have been renamed: $ aptly publish list Published repositories: * ./stable [amd64] publishes {main: [foo]: Merged from sources: 'fblub', 'asdfasdf'} $ aptly snapshot rename asdfasdf foo23 Snapshot asdfasdf -> foo23 has been successfully renamed. $ aptly publish list Published repositories: * ./stable [amd64] publishes {main: [foo]: Merged from sources: 'fblub', 'foo23'} --- cmd/publish_list.go | 2 +- deb/publish.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/cmd/publish_list.go b/cmd/publish_list.go index 69e5b4c607..d6e6ef2eca 100644 --- a/cmd/publish_list.go +++ b/cmd/publish_list.go @@ -27,7 +27,7 @@ func aptlyPublishList(cmd *commander.Command, args []string) error { if raw { published = append(published, fmt.Sprintf("%s %s", repo.StoragePrefix(), repo.Distribution)) } else { - published = append(published, repo.String()) + published = append(published, repo.StringWithSources(context.CollectionFactory().SnapshotCollection())) } return nil }) diff --git a/deb/publish.go b/deb/publish.go index 98deb4c528..7f12ac5c7e 100644 --- a/deb/publish.go +++ b/deb/publish.go @@ -334,6 +334,51 @@ func (p *PublishedRepo) String() string { strings.Join(sources, ", ")) } +// String returns human-readable representation of PublishedRepo, with current source snapshot names +func (p *PublishedRepo) StringWithSources(collection *SnapshotCollection) string { + var sources = []string{} + + for _, component := range p.Components() { + var source string + + item := p.sourceItems[component] + if item.snapshot != nil { + sourceDesc, err := item.snapshot.DescriptionWithSources(collection) + if err != nil { + // Fallback + source = item.snapshot.String() + } + source = fmt.Sprintf("[%s]: %s", item.snapshot.Name, sourceDesc) + } else if item.localRepo != nil { + source = item.localRepo.String() + } else { + panic("no snapshot/localRepo") + } + + sources = append(sources, fmt.Sprintf("{%s: %s}", component, source)) + } + + var extra string + + if p.Origin != "" { + extra += fmt.Sprintf("origin: %s", p.Origin) + } + + if p.Label != "" { + if extra != "" { + extra += ", " + } + extra += fmt.Sprintf("label: %s", p.Label) + } + + if extra != "" { + extra = " (" + extra + ")" + } + + return fmt.Sprintf("%s/%s%s [%s] publishes %s", p.StoragePrefix(), p.Distribution, extra, strings.Join(p.Architectures, ", "), + strings.Join(sources, ", ")) +} + // StoragePrefix returns combined storage & prefix for the repo func (p *PublishedRepo) StoragePrefix() string { result := p.Prefix