From 794c5984a2a893dc9e662efbae84d21d9cfcc20f Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Mon, 9 Nov 2020 18:16:22 +0100 Subject: [PATCH] Add pack command --- cmd/pack.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 cmd/pack.go diff --git a/cmd/pack.go b/cmd/pack.go new file mode 100644 index 00000000..9bb9e58e --- /dev/null +++ b/cmd/pack.go @@ -0,0 +1,89 @@ +// Copyright © 2019 Ettore Di Giacinto +// +// 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 cmd + +import ( + "os" + "path/filepath" + "time" + + helpers "github.com/mudler/luet/cmd/helpers" + "github.com/mudler/luet/pkg/compiler" + . "github.com/mudler/luet/pkg/config" + . "github.com/mudler/luet/pkg/logger" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var packCmd = &cobra.Command{ + Use: "pack ", + Short: "pack a custom package", + Long: `pack and creates metadata directly from a source path`, + PreRun: func(cmd *cobra.Command, args []string) { + viper.BindPFlag("destination", cmd.Flags().Lookup("destination")) + viper.BindPFlag("compression", cmd.Flags().Lookup("compression")) + viper.BindPFlag("source", cmd.Flags().Lookup("source")) + }, + Run: func(cmd *cobra.Command, args []string) { + sourcePath := viper.GetString("source") + + dst := viper.GetString("destination") + compressionType := viper.GetString("compression") + concurrency := LuetCfg.GetGeneral().Concurrency + + if len(args) != 1 { + Fatal("You must specify a package name") + } + + packageName := args[0] + + p, err := helpers.ParsePackageStr(packageName) + if err != nil { + Fatal("Invalid package string ", packageName, ": ", err.Error()) + } + + spec := &compiler.LuetCompilationSpec{Package: p} + artifact := compiler.NewPackageArtifact(filepath.Join(dst, p.GetFingerPrint()+".package.tar")) + artifact.SetCompressionType(compiler.CompressionImplementation(compressionType)) + err = artifact.Compress(sourcePath, concurrency) + if err != nil { + Fatal("failed compressing ", packageName, ": ", err.Error()) + } + artifact.SetCompileSpec(spec) + filelist, err := artifact.FileList() + if err != nil { + Fatal("failed generating file list for ", packageName, ": ", err.Error()) + } + artifact.SetFiles(filelist) + artifact.GetCompileSpec().GetPackage().SetBuildTimestamp(time.Now().String()) + err = artifact.WriteYaml(dst) + if err != nil { + Fatal("failed writing metadata yaml file for ", packageName, ": ", err.Error()) + } + }, +} + +func init() { + path, err := os.Getwd() + if err != nil { + Fatal(err) + } + packCmd.Flags().String("source", path, "Source folder") + packCmd.Flags().String("destination", path, "Destination folder") + packCmd.Flags().String("compression", "gzip", "Compression alg: none, gzip") + + RootCmd.AddCommand(packCmd) +}