From e38a4b3d9bb7d4e7ff64d7190c2091de1491778e Mon Sep 17 00:00:00 2001 From: Daniele Rondina Date: Wed, 20 May 2020 08:31:55 +0200 Subject: [PATCH] Use DefaultPackageSanitized struct for write specs --- cmd/tree/bump.go | 3 +- pkg/package/package.go | 3 +- pkg/spectooling/definition.go | 106 ++++++++++++++++++++++++++++++++++ pkg/tree/recipes.go | 3 +- 4 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 pkg/spectooling/definition.go diff --git a/cmd/tree/bump.go b/cmd/tree/bump.go index 5c695f8d..a2c612a0 100644 --- a/cmd/tree/bump.go +++ b/cmd/tree/bump.go @@ -22,6 +22,7 @@ import ( //"sort" . "github.com/mudler/luet/pkg/logger" + spectooling "github.com/mudler/luet/pkg/spectooling" tree "github.com/mudler/luet/pkg/tree" "github.com/spf13/cobra" @@ -54,7 +55,7 @@ func NewTreeBumpCommand() *cobra.Command { } if toStdout { - data, err := pack.Yaml() + data, err := spectooling.NewDefaultPackageSanitized(&pack).Yaml() if err != nil { Fatal("Error on yaml conversion: " + err.Error()) } diff --git a/pkg/package/package.go b/pkg/package/package.go index 415ddc3f..05cd6c03 100644 --- a/pkg/package/package.go +++ b/pkg/package/package.go @@ -26,11 +26,12 @@ import ( "strconv" "strings" + version "github.com/mudler/luet/pkg/versioner" + gentoo "github.com/Sabayon/pkgs-checker/pkg/gentoo" "github.com/crillab/gophersat/bf" "github.com/ghodss/yaml" "github.com/jinzhu/copier" - version "github.com/mudler/luet/pkg/versioner" "github.com/pkg/errors" ) diff --git a/pkg/spectooling/definition.go b/pkg/spectooling/definition.go new file mode 100644 index 00000000..6cce7224 --- /dev/null +++ b/pkg/spectooling/definition.go @@ -0,0 +1,106 @@ +// Copyright © 2019-2020 Ettore Di Giacinto , +// Daniele Rondina +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, see . + +package spectooling + +import ( + pkg "github.com/mudler/luet/pkg/package" + + "gopkg.in/yaml.v2" +) + +type DefaultPackageSanitized struct { + Name string `json:"name" yaml:"name"` + Version string `json:"version" yaml:"version"` + Category string `json:"category" yaml:"category"` + UseFlags []string `json:"use_flags,omitempty" yaml:"use_flags,omitempty"` + PackageRequires []*DefaultPackageSanitized `json:"requires,omitempty" yaml:"requires,omitempty"` + PackageConflicts []*DefaultPackageSanitized `json:"conflicts,omitempty" yaml:"conflicts,omitempty"` + IsSet bool `json:"set,omitempty" yaml:"set,omitempty"` + Provides []*DefaultPackageSanitized `json:"provides,omitempty" yaml:"provides,omitempty"` + + // Path is set only internally when tree is loaded from disk + Path string `json:"path,omitempty" yaml:"path,omitempty"` + + Description string `json:"description,omitempty" yaml:"description,omitempty"` + Uri []string `json:"uri,omitempty" yaml:"uri,omitempty"` + License string `json:"license,omitempty" yaml:"license,omitempty"` + + Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` +} + +func NewDefaultPackageSanitized(p pkg.Package) *DefaultPackageSanitized { + ans := &DefaultPackageSanitized{ + Name: p.GetName(), + Version: p.GetVersion(), + Category: p.GetCategory(), + UseFlags: p.GetUses(), + IsSet: p.Flagged(), + Path: p.GetPath(), + Description: p.GetDescription(), + Uri: p.GetURI(), + License: p.GetLicense(), + Labels: p.GetLabels(), + } + + if p.GetRequires() != nil && len(p.GetRequires()) > 0 { + ans.PackageRequires = []*DefaultPackageSanitized{} + for _, r := range p.GetRequires() { + // I avoid recursive call of NewDefaultPackageSanitized + ans.PackageRequires = append(ans.PackageRequires, + &DefaultPackageSanitized{ + Name: r.Name, + Version: r.Version, + Category: r.Category, + }, + ) + } + } + + if p.GetConflicts() != nil && len(p.GetConflicts()) > 0 { + ans.PackageConflicts = []*DefaultPackageSanitized{} + for _, c := range p.GetConflicts() { + // I avoid recursive call of NewDefaultPackageSanitized + ans.PackageConflicts = append(ans.PackageConflicts, + &DefaultPackageSanitized{ + Name: c.Name, + Version: c.Version, + Category: c.Category, + }, + ) + } + } + + if p.GetProvides() != nil && len(p.GetProvides()) > 0 { + ans.Provides = []*DefaultPackageSanitized{} + for _, prov := range p.GetProvides() { + // I avoid recursive call of NewDefaultPackageSanitized + ans.Provides = append(ans.Provides, + &DefaultPackageSanitized{ + Name: prov.Name, + Version: prov.Version, + Category: prov.Category, + }, + ) + } + } + + return ans +} + +func (p *DefaultPackageSanitized) Yaml() ([]byte, error) { + return yaml.Marshal(p) +} diff --git a/pkg/tree/recipes.go b/pkg/tree/recipes.go index 93b3d02c..57ae6834 100644 --- a/pkg/tree/recipes.go +++ b/pkg/tree/recipes.go @@ -26,6 +26,7 @@ import ( "path/filepath" pkg "github.com/mudler/luet/pkg/package" + spectooling "github.com/mudler/luet/pkg/spectooling" "github.com/pkg/errors" ) @@ -42,7 +43,7 @@ type Recipe struct { } func WriteDefinitionFile(p pkg.Package, definitionFilePath string) error { - data, err := p.Yaml() + data, err := spectooling.NewDefaultPackageSanitized(p).Yaml() if err != nil { return err }